UNPKG

password_with_constrains

Version:

This will generate random password as per as need

245 lines (223 loc) 10.2 kB
const shuffle = (string) => { var a = string.split(""), n = a.length; for (var i = n - 1; i > 0; i--) { var j = Math.floor(Math.random() * (i + 1)); var tmp = a[i]; a[i] = a[j]; a[j] = tmp; } return a.join(""); } const createPassword = (min, max, data) => { let _result = ``; let asciiArray = removeFalseValued(data); if (data['upperCase'] && data['upperCase'] >= 1) { let rand = Math.floor(Math.random() * data['upperCase']) + 1 for (let i = 0; i < rand; i++) { let _rand = Math.floor(Math.random() * (90 - 65 + 1) + 65); _result += String.fromCharCode(_rand); } } if (data['lowerCase'] && data['lowerCase'] >= 1) { let rand = Math.floor(Math.random() * data['lowerCase']) + 1 for (let i = 0; i < rand; i++) { let _rand = Math.floor(Math.random() * (122 - 97 + 1) + 97); _result += String.fromCharCode(_rand); } } if (data['specialCharacter'] && data['specialCharacter'] >= 1) { let rand = Math.floor(Math.random() * data['specialCharacter']) + 1; for (let i = 0; i < rand; i++) { let _rand = Math.floor(Math.random() * (47 - 33 + 1) + 33); _result += String.fromCharCode(_rand); } } if (data['numericValue'] && data['numericValue'] >= 1) { let rand = Math.floor(Math.random() * data['numericValue']) + 1 for (let i = 0; i < rand; i++) { let _rand = Math.floor(Math.random() * (57 - 48 + 1) + 48); _result += String.fromCharCode(_rand); } } if (_result.length < min) { let diff = min - _result.length; for (let i = 0; i < diff; i++) { // let _rand = Math.floor(Math.random() * asciiArray.length) + 1 let _rand = Math.floor(Math.random() * ((asciiArray.length - 1) - 0 + 1) + 0); // let rand = Math.floor(Math.random() * (126 - 33 + 1) + 33); _result += String.fromCharCode(asciiArray[_rand]); } let _diff = max - _result.length; let rand = Math.floor(Math.random() * _diff) + 1 for (let i = 0; i < rand; i++) { // let _rand = Math.floor(Math.random() * asciiArray.length) + 1 let _rand = Math.floor(Math.random() * ((asciiArray.length - 1) - 0 + 1) + 0); // let rand = Math.floor(Math.random() * (126 - 33 + 1) + 33); _result += String.fromCharCode(asciiArray[_rand]); } } return _result; } const validations = (array) => { let status = true; let errors = {}; let totalMax = 0; if (array[0] > array[1]) { errors['min-max'] = { message: `min should be less than max` } status = false; } if (array[0]) { if (typeof array[0] !== "number") { errors.minLen = { message: `min length expected to be number received ${typeof array[0]}` } status = false; } } if (array[1]) { if (typeof array[1] !== "number") { errors.maxLen = { message: `max length expected to be number received ${typeof array[1]}` } status = false; } } if (array[2]) { if (typeof array[2] !== "object") { errors.validationObj = { message: `validation object expected to be object received ${typeof array[2]}` } status = false; } if (status) { const keys = Object.keys(array[2]); keys.map(key => { if (!['upperCase', 'lowerCase', 'specialCharacter', 'numericValue'].includes(key)) { errors[key] = { message: `invalid ${key} expected to be one of [upperCase, lowerCase, specialCharacter,numericValue]` } status = false; } }) if (status) { keys.map((key, _index) => { if (array[2][key]) { let objectKeysArray = Object.keys(array[2][key]); let objectValueArray = Object.values(array[2][key]); objectKeysArray.map((_key, index) => { if (!['min', 'max', 'required'].includes(_key)) { errors[keys[_index]] = { message: `invalid key ${_key} in ${key} expected to be one of [min, max, required]` } status = false; } if (objectKeysArray[index] === 'required' && typeof objectValueArray[index] !== "boolean") { errors[`${keys[_index]}_${_key}`] = { message: `invalid value ${_key} : ${objectValueArray[index]} expected to be boolean but received ${typeof objectValueArray[index]} ` } status = false; } if (objectKeysArray[index] === 'min' && typeof objectValueArray[index] !== "number") { errors[`${keys[_index]}_${_key}`] = { message: `invalid value ${_key} : ${objectValueArray[index]} expected to be number but received ${typeof objectValueArray[index]} ` } status = false; } if (objectKeysArray[index] === 'max' && typeof objectValueArray[index] !== "number") { errors[`${keys[_index]}_${_key}`] = { message: `invalid value ${_key} : ${objectValueArray[index]} expected to be number but received ${typeof objectValueArray[index]} ` } status = false; } }) if (objectValueArray[objectKeysArray.indexOf('min')] < 0) { errors[`${key}_min`] = { message: `min should not be negative.` } status = false; } if (objectValueArray[objectKeysArray.indexOf('max') >= 100]) { errors[`${key}_max`] = { message: `max should not less than 100.` } status = false; } if (objectValueArray[objectKeysArray.indexOf('min')] > objectValueArray[objectKeysArray.indexOf('max')]) { errors[`${key}_min_max`] = { message: `min should be less than max` } status = false; } else if (objectValueArray[objectKeysArray.indexOf('required')] === true) { totalMax += objectValueArray[objectKeysArray.indexOf('max')]; } } }) } if (totalMax > array[1]) { errors['total-max'] = { message: `total max ${totalMax} in every field should be less than or equal max length ${array[1]}` } status = false; } } } if (status) return [status, { upperCase: array[2]['upperCase']['required'] ? array[2]['upperCase']['min'] >= 0 ? array[2]['upperCase']['max'] : array[2]['upperCase']['min'] : null, lowerCase: array[2]['lowerCase']['required'] ? array[2]['lowerCase']['min'] >= 0 ? array[2]['lowerCase']['max'] : array[2]['lowerCase']['min'] : null, specialCharacter: array[2]['specialCharacter']['required'] ? array[2]['specialCharacter']['min'] >= 0 ? array[2]['specialCharacter']['max'] : array[2]['specialCharacter']['min'] : null, numericValue: array[2]['numericValue']['required'] ? array[2]['numericValue']['min'] >= 0 ? array[2]['numericValue']['max'] : array[2]['numericValue']['min'] : null, }]; return [status, errors]; } function passGenerator(minLen = 6, maxLen = 6, requiredFieldsObject = {}) { const _errors = {}; let _result = ``; const argumentsArr = [...arguments]; return new Promise((resolve, reject) => { const [status, result] = validations(argumentsArr); if (!status) return reject(result); const data = createPassword(minLen, maxLen, result); const realStr = shuffle(data); resolve(realStr); }) } const removeFalseValued = (data) => { let asciiArray = []; for (let i = 33; i <= 126; i++) { asciiArray.push(i); } if (data.numericValue === null) asciiArray = asciiArray.filter(val => { return (!(val > 47 && val < 58)) }) // REMOVE UPPERCASE VALUES. if (data.upperCase === null) asciiArray = asciiArray.filter(val => { return (!(val > 64 && val < 91)) }) // REMOVE LOWERCASE if (data.lowerCase === null) asciiArray = asciiArray.filter(val => { return (!(val > 96 && val < 123)) }) //REMOVE SPECIAL CHARACTER if (data.specialCharacter === null) asciiArray = asciiArray.filter(val => { return (!(val > 32 && val < 48) && !(val > 57 && val < 65) && !(val > 90 && val < 97) && !(val > 122)) }) return asciiArray; } module.exports = passGenerator;