forked from web-mech/badwords
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbadwords.js
49 lines (42 loc) · 1.51 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
var Filter = (function() {
function Filter(options) {
options = options || {};
this.list = options.emptyList && [] || Array.prototype.concat.apply(require('./lang.json').words, [require('badwords-list').array, options.list || []]);
this.placeHolder = options.placeHolder || '*';
this.regex = options.regex || /[^a-zA-z0-9|\$|\@]|\^/g;
}
Filter.prototype.isProfane = function isProfane(string) {
var words = string.split(' ');
for (var j = 0; j < words.length; j++) {
var word = words[j].toLowerCase().replace(this.regex, '');
if (~this.list.indexOf(word)) {
return true;
}
}
return false;
};
Filter.prototype.replaceWord = function replaceWord(string) {
return string.replace(this.regex, '').replace(/\w/g, this.placeHolder);
};
Filter.prototype.clean = function clean(string) {
return string.split(' ').map(function(word) {
return this.isProfane(word) ? this.replaceWord(word) : word;
}.bind(this)).join(' ');
};
Filter.prototype.addWords = function addWords(words) {
words = (words instanceof Array) ? words : [words];
this.list = this.list.concat(words);
};
Filter.prototype.removeWords = function removeWords() {
words = Array.prototype.slice.call(arguments);
words.map(function(word) {
return this.list.indexOf(word);
}, this).filter(function(index) {
return !!~index;
}).forEach(function(index){
this.list.splice(index, 1);
}, this);
};
return Filter;
})();
module.exports = Filter;