-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (70 loc) · 2.36 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const randomWords = require('random-words');
const RePasswordGeneratorTypes = {
EASY_TO_SAY: 'EASY_TO_SAY',
EASY_TO_READ: 'EASY_TO_READ',
ALL_CHARS: 'ALL_CHARS',
WORDS: 'WORDS'
};
const characterSets = {
lowercase: 'abcdefghijklmnopqrstuvwxyz',
uppercase: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
number: '0123456789',
symbol: '~!@#$%&*?^',
};
class RePasswordGenerator {
constructor(options) {
this.options = options || {};
this.words = this.options.words || 3; // optional
this.passwordlength = this.options.passwordlength || 12; // default 12 char value
this.separator = this.options.separator || '-';
this.type = this.options.type || 'WORDS'; // words (random-words) , EASY_TO_SAY, easyEASY_TO_READtoread, ALL_CHARS
this.case = this.options.case || 'lower'; // lower, camel
this.addNumbers = this.options.addNumbers || false;
this.addSymbols = this.options.addSymbols || false;
}
generate() {
if (this.type == 'WORDS') {
const words = randomWords({ exactly: this.words });
return words.join(this.separator);
}
return this.generatePassword(this.passwordlength, this.type, (this.case == 'lower'), (this.case == 'upper'), this.addNumbers, this.addSymbols);
}
generatePassword(length, type, lower, upper, number, symbol) {
let generatedPassword = '';
if (!lower && !upper && !number && !symbol) {
return '';
}
const charSets = [];
if (lower) {
charSets.push(characterSets.lowercase);
}
if (upper) {
if (type === RePasswordGeneratorTypes.EASY_TO_READ) {
charSets.push(characterSets.uppercase.replace(/I|O/g, ''));
} else {
charSets.push(characterSets.uppercase);
}
}
if (number) {
if (type === RePasswordGeneratorTypes.EASY_TO_READ) {
charSets.push(characterSets.number.replace(/1|0/g, ''));
} else {
charSets.push(characterSets.number);
}
}
if (symbol) {
charSets.push(characterSets.symbol);
}
for (let i = 0; i < length; i++) {
const charSetIndex = Math.floor(Math.random() * charSets.length);
const chars = charSets[charSetIndex];
generatedPassword += chars[Math.floor(Math.random() * chars.length)];
}
console.log(`test ${generatedPassword}`)
return generatedPassword.slice(0, length);
}
}
module.exports = {
RePasswordGenerator,
RePasswordGeneratorTypes
};