-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxkcd-nobad-password.js
53 lines (49 loc) · 1.45 KB
/
xkcd-nobad-password.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
const fs = require('fs');
const path = require('path');
const xkcdPassword = require('xkcd-z-password');
const {EventEmitter} = require('events');
class Badwords extends EventEmitter {
constructor({
files = [path.join(__dirname, 'badwords.txt')], encode = 'utf8'
} = {}) {
super()
this.badwordsRaw = new Set()
for (const file of files) this.add(
fs.readFileSync(file, encode)
.split(/\r?\n/g)
.filter(word => !!word && !/^(#|\/\/).+$/gm.test(word))
)
this.on('update', () => this.regen())
this.emit('update', this)
}
regen() {
this.re = new RegExp(`^(${[...this.badwordsRaw].join('|')})$`, 'gi')
}
add(...words) {
if (Array.isArray(words[0])) {
for (const word of words[0]) {
this.badwordsRaw.add(word)
}
} else {
for (const word of words) {
this.badwordsRaw.add(word)
}
}
this.emit('update', this)
this.emit('ready', this)
}
test(tx) {
return this.re.test(tx)
}
}
module.exports = exports = ({
filter = s => s.length >= minLength && s.length <= maxLength, autoInit = true,
numWords = 4, minLength = 5, maxLength = 8, badwords = new Badwords(),
wordList, wordFileZ, wordFile, caseSensitive, readableinputs
} = {}) => xkcdPassword.init({
numWords, minLength, maxLength, autoInit, filter(word) {
return filter(word) && !badwords.test(word)
},
wordList, wordFileZ, wordFile, caseSensitive, readableinputs
});
exports.Badwords = Badwords;