Skip to content

Commit

Permalink
Merge branch 'feature/esnext'
Browse files Browse the repository at this point in the history
  • Loading branch information
web-mech committed Oct 23, 2018
2 parents 77cc887 + 1ab43a8 commit 4e9aba5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
25 changes: 12 additions & 13 deletions lib/badwords.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var localList = require('./lang.json').words;
var baseList = require('badwords-list').array;
var Filter = (function() {
var Filter = (function () {
/**
* Filter constructor.
* @constructor
Expand All @@ -25,12 +25,11 @@ var Filter = (function() {
* @param {string} string - String to evaluate for profanity.
*/
Filter.prototype.isProfane = function isProfane(string) {
return string
.split(/\b/)
.map(function(w) {
return w.toLowerCase().replace(this.regex, '');
return this.list
.filter(function (word) {
const wordExp = new RegExp(`\\b${word.replace(/(\W)/g, '\\$1')}\\b`, 'gi');
return !this.exclude.includes(word) && wordExp.test(string);
}, this)
.filter(this.isProfaneLike, this)
.shift() || false;
};

Expand All @@ -48,10 +47,10 @@ var Filter = (function() {
}

return this.list
.map(function(w) {
.map(function (w) {
return new RegExp('^' + w.replace(/(\W)/g, '\\$1') + '$', 'gi');
}, this)
.reduce(function(outcome, wordExp) {
.reduce(function (outcome, wordExp) {
return outcome || wordExp.test(word);
}, false);
};
Expand All @@ -69,21 +68,21 @@ var Filter = (function() {
* @param {string} string - Sentence to filter.
*/
Filter.prototype.clean = function clean(string) {
return string.split(/\b/).map(function(word) {
return string.split(/\b/).map(function (word) {
return this.isProfane(word) ? this.replaceWord(word) : word;
}.bind(this)).join('');
};

/**
* Add words to blacklist filter / remove words from whitelist filter
* @param {(string|string[])}
*/
*/
Filter.prototype.addWords = function addWords(words) {
words = (words instanceof Array) ? words : [words];
this.list = this.list.concat(words);

words.forEach(function(word) {
if(!!~this.exclude.indexOf(word)) {
words.forEach(function (word) {
if (!!~this.exclude.indexOf(word)) {
this.exclude.splice(this.exclude.indexOf(word), 1);
}
}, this);
Expand All @@ -92,7 +91,7 @@ var Filter = (function() {
/**
* Add words to whitelist filter
* @param {...string} word - Word to add to whitelist.
*/
*/
Filter.prototype.removeWords = function removeWords() {
var words = Array.prototype.slice.call(arguments);
this.exclude.push.apply(this.exclude, words);
Expand Down
7 changes: 6 additions & 1 deletion test/isProfane.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ describe('filter', function(){

it('Should tokenize words according to regex word boundaries', function() {
assert(filter.isProfane("that person is an\nasshole"));
})
});

it('Should detect bad word phrases', function () {
filter.addWords('oh no');
assert(filter.isProfane("oh no! this is profane!"));
});
});
});

0 comments on commit 4e9aba5

Please sign in to comment.