Skip to content

Instantly share code, notes, and snippets.

@deanacus
Created October 23, 2019 10:59
Show Gist options
  • Save deanacus/73d63c49c74b548bebdd2d877cae9a15 to your computer and use it in GitHub Desktop.
Save deanacus/73d63c49c74b548bebdd2d877cae9a15 to your computer and use it in GitHub Desktop.
const lower = 'abcdefghijklmnopqrstuvwxyz';
const upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const numbers = '0123456780';
const symbols = '!@#$%^&*_+-/~?'
/**
* Generate a password from any number os groups of strings
*
* @example generatePassword([upper, symbols], 26)
*
* @param {Array} options Array of strings to generate password from
* @param {Number} length Length of desired password
*
* @return {String}
*/
const generatePassword = (options = [], length=12) => {
const str = [].concat(...options).join('');
let password = '';
for (let i = 0; i < length; i++) {
password += str[Math.floor(Math.random() * str.length)]
}
return password;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment