UNPKG

@wcj/generate-password

Version:

Generate Password is a generating random and unique passwords.

88 lines (84 loc) 3.24 kB
/**! * @wcj/generate-password v1.0.4 * Generate Password is a generating random and unique passwords. * * Copyright (c) 2023 kenny wong <wowohoo@qq.com> * https://jaywcjlove.github.io/generate-password * Licensed under the MIT license */ 'use strict'; var LOWERCASE = 'abcdefghijklmnopqrstuvwxyz'; var UPPERCASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; var NUMERIC = '0123456789'; var SPECIAL_CHARACTER = '!@#$%^&*()_+~`|}{\\[\\]:;?>,.<-=\\/'; /** Create a random password */ function generate(opts) { if (opts === void 0) { opts = {}; } var _a = opts.lowerCase, lowerCase = _a === void 0 ? true : _a, _b = opts.upperCase, upperCase = _b === void 0 ? true : _b, _c = opts.numeric, numeric = _c === void 0 ? true : _c, _d = opts.special, special = _d === void 0 ? true : _d, _e = opts.length, length = _e === void 0 ? 10 : _e; var password = ''; if (!lowerCase && !upperCase && !numeric && !special) { return password; } while (password.length < length) { var entity1 = Math.ceil(LOWERCASE.length * Math.random() * Math.random()) - 1; var entity2 = Math.ceil(NUMERIC.length * Math.random() * Math.random()) - 1; var entity3 = Math.ceil(SPECIAL_CHARACTER.length * Math.random() * Math.random()) - 1; var entity4 = Math.ceil(UPPERCASE.length * Math.random() * Math.random()) - 1; if (lowerCase && password.length < length) { password += LOWERCASE.charAt(entity1); } if (upperCase && password.length < length) { password += UPPERCASE.charAt(entity4); } if (numeric && password.length < length) { password += NUMERIC.charAt(entity2); } if (special && password.length < length) { password += SPECIAL_CHARACTER.charAt(entity3); } } return password.trim(); } /** Create a random set of passwords */ function generateMultiple(length, opts) { if (length === void 0) { length = 10; } var result = []; for (var i = 0; i < length; i++) { result.push(generate(opts)); } return result; } /** * symbols pass with lowercase and uppercase letters, numbers and special characters * @return [0~4] * * `4` Strong :) Now it's safe! * `3` Medium level. Enter more symbols! * `2` Very Weak! It's easy to crack! * `1` It's easy to crack! */ function validate(password) { if (password === void 0) { password = ''; } // Create an array and push all possible values that you want in password var matchedCase = new Array(); matchedCase.push("[".concat(SPECIAL_CHARACTER, "]")); // Special Charector matchedCase.push('[A-Z]'); // Uppercase Alpabates matchedCase.push('[0-9]'); // Numbers matchedCase.push('[a-z]'); // Lowercase Alphabates // Check the conditions var ctr = 0; for (var i = 0; i < matchedCase.length; i++) { if (new RegExp(matchedCase[i]).test(password)) { ctr++; } } return ctr; } exports.LOWERCASE = LOWERCASE; exports.NUMERIC = NUMERIC; exports.SPECIAL_CHARACTER = SPECIAL_CHARACTER; exports.UPPERCASE = UPPERCASE; exports.generate = generate; exports.generateMultiple = generateMultiple; exports.validate = validate; //# sourceMappingURL=index.js.map