Files
reader-star/apps/admin/src/utils/date.ts

49 lines
1.1 KiB
TypeScript
Raw Normal View History

import path from 'path-browserify'
export function getCurrentYear(): Date {
return new Date()
}
/**
*
* @param min
* @param max
* @returns
*/
export function getTrueRandomInt(min: number, max: number) {
const range = max - min + 1
const maxSafe = 0xFFFFFFFF // 32位最大无符号整数 (2^32 - 1)
let randomValue = 0
do {
const buffer = new Uint32Array(1)
window.crypto.getRandomValues(buffer)
randomValue = (buffer[0]! / (maxSafe + 1)) * range // [0, range)
} while (randomValue >= range) // 拒绝采样避免偏差
return Math.floor(randomValue) + min
}
/**
*
*/
export function browserPathJoin(base: string, ...paths: string[]) {
const [protocol, ...rest] = base.split('://')
if (rest.length > 0) {
const pathPart = rest.join('://').replace(/\/+/g, '/')
return `${protocol}://${path.join(pathPart, ...paths)}`
}
return path.join(base, ...paths)
}
/**
* tick后执行
*/
export function nextTickSleep() {
return new Promise((resolve) => {
nextTick(() => {
resolve(true)
})
})
}