function randomStr(strLength = 6, repeat = false, ignoreCase = false) {
let validCharacter = "0123456789";
const aIndex = "a".charCodeAt();
const AIndex = "A".charCodeAt();
for (let i = aIndex; i < aIndex + 26; i++) {
validCharacter += String.fromCharCode(i);
}
for (let i = AIndex; i < AIndex + 26; i++) {
validCharacter += String.fromCharCode(i);
}
validCharacter = validCharacter.split("");
let result = '';
if (repeat) {
for (let i = 0; i < strLength; i++) {
result += validCharacter[Math.floor(Math.random() * validCharacter.length)];
}
} else {
ignoreCase && strLength > 36 ? strLength = 36 : void 0;
ignoreCase && validCharacter.splice(36, 26);
!ignoreCase && strLength > 62 ? strLength = 62 : void 0;
while (result.length < strLength) {
const newCharacterIdx = Math.floor(Math.random() * validCharacter.length);
const newCharacter = validCharacter.splice(newCharacterIdx, 1)[0];
result += newCharacter;
}
}
return result;
}
console.log(randomStr(64, false, false));