node-password-generator
Version:
A simple lightweight npm library for password generator. It allows you to create random unqiue password on the fly.
19 lines (18 loc) • 581 B
JavaScript
/**
* @param {string} characterList
* @param {number} length
* @returns string
*/
export default function passwordRandomizer(characterList, length) {
// If `characterList` is unset or empty string
if (!characterList) {
throw new Error('The `characterList` can not be empty');
}
// Initializing, password variable as empty string
let password = ``;
// Generate one password based on options
for (let i = 0; i < length; ++i) {
password += characterList[Math.round(Math.random() * characterList.length)];
}
return password;
}