-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (49 loc) · 1.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
var passwordGenerator = (function() {
function generatePassword (length, candidates) {
var result = "";
for (var i = 0; i < length; i++) {
var randomNum = Math.floor(Math.random() * candidates.length);
result += candidates.substring(randomNum, randomNum + 1);
}
return result;
}
function removeChars (candidates, chars) {
for (var i = 0; i < chars.length; i++) {
var character = chars[i];
candidates = candidates.replace(character, "");
}
return candidates;
}
function generate(options) {
var lowercase = "abcdefghiklmnopqrstuvwxyz";
var uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXTZ";
var numbers = "0123456789";
var punct = ".,-/#!$%^&*;:{}=-_`~()]";
var candidates = numbers + lowercase + uppercase + punct;
if (options) {
if (options.noCapitals) {
candidates = candidates.replace(uppercase, "");
}
if (options.noNumbers) {
candidates = candidates.replace(numbers, "");
}
if (options.noPunctuation) {
candidates = candidates.replace(punct, "");
}
if (options.noVowels) {
candidates = removeChars(candidates, "aeiouAEIOU");
}
if (options.noAmbiguous) {
candidates = removeChars(candidates, "B8G6I1l0OQDS5Z2");
}
}
if (options && options.length) {
return generatePassword(options.length, candidates);
}
return generatePassword(12, candidates);
}
return {
generate: generate
};
}());
module.exports = passwordGenerator;