const ElementCodes = new Set();
/**
* 记录生成的code
*/
export const recordPointCode = (code: string) => {
ElementCodes.add(code);
};
/****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
const $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
const maxPos = $chars.length;
/**
* 随机生成指定的任意位数字符串
* @param {*} len
*/
export const randomString = (prefix: string | null, len = 32): string => {
const arr = [];
for (let i = 0; i < len; i++) {
arr.push($chars.charAt(Math.floor(Math.random() * maxPos)));
}
const pwd = (prefix ? prefix + '_' : '') + arr.join('');
if (ElementCodes.has(pwd)) {
return randomString(prefix, len);
}
// 记录码点code
recordPointCode(pwd);
return pwd;
};
//调用的地方
const title = randomString('test', 3);
console.log(title )
随机生成指定的任意位数字符串
最新推荐文章于 2024-09-03 08:22:58 发布