task-svr/src/common/Utils.ts
2024-08-22 15:02:01 +08:00

86 lines
3.5 KiB
TypeScript

import { BASE52_ALPHABET } from './Constants'
export const isObjectIdString = (str: string) => {
return /^[0-9a-fA-F]{24}$/.test(str)
}
// check if a string is a valid share code
// alphabet:'3fBCM8j17XNA9xYun4wmLWep2oHFlhPcgyEJskqOz6GK0UtV5ZRaDSvrTbidQI'
export const isValidShareCode = (str: string) => {
let reg = new RegExp(`^[${BASE52_ALPHABET}]{10,11}$`)
return reg.test(str)
}
export const isValidEthAddress = (str: string) => {
return /^0x[a-fA-F0-9]{40}$/.test(str)
}
export const isValidVoucherCode = (str: string) => {
let reg = new RegExp(`^[${BASE52_ALPHABET}]{12}$`)
return reg.test(str)
}
// check if a string is a valid day tag, like 20240408
export const isValidDayTag = (str: string) => {
return /^[0-9]{8}$/.test(str)
}
export const formatNumShow = (num: number) => {
if (num >= 10) {
return Math.round(num) + ''
} else if (num === 0) {
return '0'
} else {
return num.toFixed(1)
}
}
export const randomUserAgent = () => {
// 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
// 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
// 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.3',
// 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.97 Safari/537.3',
// 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Safari/537.3',
// 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
// 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
// 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
// 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
// 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
let n1 = Math.floor(Math.random() * 15) + 70
let n2 = Math.floor(Math.random() * 100)
let osName = Math.random() > 0.5 ? 'Windows NT 10.0' : 'Macintosh; Intel Mac OS X 10_15_7'
let s0 = `Mozilla/5.0 (${osName}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/`
let s2 = `.0.3029.`
let s3 = 'Safari/537.46'
return `${s0}${n1}${s2}${n2}${s3}`
}
export const randomIp = () => {
let n1 = Math.floor(Math.random() * 254)
let n2 = Math.floor(Math.random() * 254)
let n3 = Math.floor(Math.random() * 254)
let n4 = Math.floor(Math.random() * 254)
let ip1 = `${n1}.${n2}.${n3}.${n4}`
let ip2 = Math.random() > 0.5 ? '10.0.2.69' : '10.0.0.80'
return `${ip1}, ${ip2}`
}
// 0xd1c7bad0033678f3e347076e5db7290f990d2fcd69369a95918db6d8c6d87c3021fa2334da8cdacb99bdf311ff2087e1b7f5c8f4d8fdb0a653bfeae0a9ebbda41c
// get 132 random hex string with 0x prefix
export const randomSign = () => {
let hex = '0x'
for (let i = 0; i < 64; i++) {
hex += Math.floor(Math.random() * 16).toString(16)
hex += Math.floor(Math.random() * 16).toString(16)
}
hex += Math.random() > 0.5 ? '1b' : '1c'
return hex
}
export const hidePartString = (str: string, showNum: number = 8) => {
if (str.length <= showNum) {
return str
}
let len = str.length
let half = Math.floor(showNum / 2)
return str.slice(0, half) + '...' + str.slice(len - half)
}