UNPKG

simplepass-js

Version:
41 lines (37 loc) 1.24 kB
const data = require('./data').config; class Password { /** * Generate a password * @param {Number} length - password length * @param {Boolean} isNumbers - allow to contain numbers * @param {Number} specialsLevel - allow to contain a special characters * see more in data.js config * @return {String} password */ static generate (length = 8, isNumbers = 1, specialsLevel = 0) { let password = []; const randomPosition = this.random(2, 6); for (let i = 0; i < length; i++) { if (isNumbers && i != 0 && i % randomPosition == 0) { password.push(data.numbers[this.random(0, data.numbers.length)]); continue; } if (specialsLevel && i != 0 && i % (randomPosition + 1) == 0) { password.push(data["specials_" + [specialsLevel]][this.random(0, data["specials_" + [specialsLevel]].length)]); continue; } password.push(data.alphabet_en[this.random(0, data.alphabet_en.length)]); } return password.join(''); } /** * Return a random integer * @param {Number} min * @param {Number} max * @return {Number} random number */ static random (min, max) { return Math.floor(Math.random() * (max - min)) + min; } } module.exports = Password;