49 lines
1.1 KiB
TypeScript
49 lines
1.1 KiB
TypeScript
|
|
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);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
}
|