Skip to content

Commit 995ea1e

Browse files
committed
feat(profane): support profane phrases and well as words
When testing a string for a profane word, or phrase, it needs to validate against the entire string instead of just each word in a string. This allows for testing against word combinations instead of just single word checking.
1 parent 37b16f8 commit 995ea1e

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

lib/badwords.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@ var Filter = (function() {
2525
* @param {string} string - String to evaluate for profanity.
2626
*/
2727
Filter.prototype.isProfane = function isProfane(string) {
28-
return string
29-
.split(/\b/)
30-
.map(function(w) {
31-
return w.toLowerCase().replace(this.regex, '');
28+
return this.list
29+
.filter(function (word) {
30+
const wordExp = new RegExp(`\\b${word.replace(/(\W)/g, '\\$1')}\\b`, 'gi');
31+
return !this.exclude.includes(word) && wordExp.test(string);
3232
}, this)
33-
.filter(this.isProfaneLike, this)
3433
.shift() || false;
3534
};
3635

test/isProfane.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ describe('filter', function(){
3434

3535
it('Should tokenize words according to regex word boundaries', function() {
3636
assert(filter.isProfane("that person is an\nasshole"));
37-
})
37+
});
38+
39+
it('Should detect bad word phrases', function () {
40+
filter.addWords('oh no');
41+
assert(filter.isProfane("oh no! this is profane!"));
42+
});
3843
});
3944
});

0 commit comments

Comments
 (0)