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 )
随机生成指定的任意位数字符串
最新推荐文章于 2025-02-02 13:21:48 发布
文章介绍了一个JavaScript函数库,用于生成去除了特定字符的随机字符串,并在生成后检查是否已存在,如果存在则重新生成,同时记录生成的代码点。
1100

被折叠的 条评论
为什么被折叠?



