Files
vite_front/hwe/ts/util/randStr.ts
T
2022-07-08 00:11:41 +09:00

25 lines
672 B
TypeScript

const charList = [
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z',
];
export function randStr(len: number): string {
const result = [];
const charListLen = charList.length;
let isStart = true;
while(len > 0){
const randChrIdx = Math.floor(Math.random() * charListLen);
if(isStart){
if(randChrIdx == 0){
continue;
}
isStart = false;
}
result.push(charList[randChrIdx]);
len -= 1;
}
return result.join('');
}