Random-Thing/generateRandomString.js
2024-06-08 03:08:19 -04:00

51 lines
No EOL
1.2 KiB
JavaScript

function generateRandomString(length, ...modes) { // generateRandomString(99, 1, 2, 3, 4, 5)
const modeCharacters = [
'0123456789', // Mode 1
'abcdefghijklmnopqrstuvwxyz', // Mode 2
'ABCDEFGHIJKLMNOPQRSTUVWXYZ', // Mode 3
'!@#$%^&*()-_=+[]{}|;:\'",<.>/?`~\\', // Mode 4
];
// ABot
const unicodeCharacters = (() => {
let result = '';
const characterList = '1234567890ABCDEF';
try {
for (let i = 0; i < length; i++) {
let chars = '';
for (let j = 0; j < 4; j++) {
chars += characterList[Math.floor(Math.random() * characterList.length)];
}
result += JSON.parse(`"\\u${chars}"`);
}
} catch (e) {
console.error(e.stack);
return '';
}
return result;
})();
modeCharacters.push(unicodeCharacters);
let characters = '';
modes.forEach(mode => {
if (mode >= 1 && mode <= 5) {
characters += modeCharacters[mode - 1];
}
});
if (characters.length === 0) {
return '';
}
let result = '';
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}