forked from beattv/badwords
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbadwords.js
107 lines (97 loc) · 3.06 KB
/
badwords.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const localList = require("./lang.json").words;
const baseList = require("badwords-list").array;
class Filter {
/**
* Filter constructor.
* @constructor
* @param {object} options - Filter instance options
* @param {boolean} options.emptyList - Instantiate filter with no blacklist
* @param {array} options.list - Instantiate filter with custom list
* @param {string} options.placeHolder - Character used to replace profane words.
* @param {string} options.regex - Regular expression used to sanitize words before comparing them to blacklist.
* @param {string} options.replaceRegex - Regular expression used to replace profane words with placeHolder.
* @param {string} options.splitRegex - Regular expression used to split a string into words.
*/
constructor(options = {}) {
Object.assign(this, {
list:
(options.emptyList && []) ||
Array.prototype.concat.apply(localList, [baseList, options.list || []]),
exclude: options.exclude || [],
splitRegex: options.splitRegex || /\b/,
placeHolder: options.placeHolder || "*",
regex: options.regex || /[^a-zA-Z0-9|\$|\@]|\^/g,
replaceRegex: options.replaceRegex || /\w/g,
});
}
/**
* Determine if a string contains profane language.
* @param {string} string - String to evaluate for profanity.
*/
isProfanity(string) {
return this.list.includes(string.toLowerCase());
}
/**
* Determine if a string contains profane language.
* @param {string} string - String to evaluate for profanity.
*/
containsProfanity(string) {
return (
this.list.filter((word) => {
const wordExp = new RegExp(
`\\b${word.replace(/(\W)/g, "\\$1")}\\b`,
"gi"
);
return (
!this.exclude.includes(word.toLowerCase()) && wordExp.test(string)
);
}).length > 0 || false
);
}
/**
* Replace a word with placeHolder characters;
* @param {string} string - String to replace.
*/
replaceWord(string) {
return string
.replace(this.regex, "")
.replace(this.replaceRegex, this.placeHolder);
}
/**
* Evaluate a string for profanity and return an edited version.
* @param {string} string - Sentence to filter.
*/
clean(string) {
return string
.split(" ")
.map((word) => {
return this.isProfane(word) ? this.replaceWord(word) : word;
})
.join(" ");
}
/**
* Add word(s) to blacklist filter / remove words from whitelist filter
* @param {...string} word - Word(s) to add to blacklist
*/
addWords() {
let words = Array.from(arguments);
this.list.push(...words);
words
.map((word) => word.toLowerCase())
.forEach((word) => {
if (this.exclude.includes(word)) {
this.exclude.splice(this.exclude.indexOf(word), 1);
}
});
}
/**
* Add words to whitelist filter
* @param {...string} word - Word(s) to add to whitelist.
*/
removeWords() {
this.exclude.push(
...Array.from(arguments).map((word) => word.toLowerCase())
);
}
}
module.exports = Filter;