Skip to content

Commit

Permalink
refactor(*): further utilize es2016 syntax
Browse files Browse the repository at this point in the history
converts the lib into a class, use of arrow fns. constructor defaults.
  • Loading branch information
web-mech committed Oct 23, 2018
1 parent 656b87c commit dd4632f
Showing 1 changed file with 36 additions and 58 deletions.
94 changes: 36 additions & 58 deletions lib/badwords.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var localList = require('./lang.json').words;
var baseList = require('badwords-list').array;
var Filter = (function () {
const localList = require('./lang.json').words;
const baseList = require('badwords-list').array;

class Filter {
/**
* Filter constructor.
* @constructor
Expand All @@ -11,93 +12,70 @@ var Filter = (function () {
* @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.
*/
function Filter(options) {
options = options || {};
this.list = options.emptyList && [] || Array.prototype.concat.apply(localList, [baseList, options.list || []]);
this.exclude = options.exclude || [];
this.placeHolder = options.placeHolder || '*';
this.regex = options.regex || /[^a-zA-Z0-9|\$|\@]|\^/g;
this.replaceRegex = options.replaceRegex || /\w/g;
constructor(options = {}) {
Object.assign(this, {
list: options.emptyList && [] || Array.prototype.concat.apply(localList, [baseList, options.list || []]),
exclude: options.exclude || [],
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.
*/
Filter.prototype.isProfane = function isProfane(string) {
isProfane(string) {
return this.list
.filter(function (word) {
.filter((word) => {
const wordExp = new RegExp(`\\b${word.replace(/(\W)/g, '\\$1')}\\b`, 'gi');
return !this.exclude.includes(word) && wordExp.test(string);
}, this)
.shift() || false;
};

/**
* Determine if a single word is profane or looks profane.
* @param {string} word - String to evaluate for profanity.
*/
Filter.prototype.isProfaneLike = function profaneLike(word) {
if (!!~this.exclude.indexOf(word)) {
return false;
}

if (!!~this.list.indexOf(word)) {
return true;
}

return this.list
.map(function (w) {
return new RegExp('^' + w.replace(/(\W)/g, '\\$1') + '$', 'gi');
}, this)
.reduce(function (outcome, wordExp) {
return outcome || wordExp.test(word);
}, false);
};
})
.length > 0 || false;
}

/**
* Replace a word with placeHolder characters;
* @param {string} string - String to replace.
*/
Filter.prototype.replaceWord = function replaceWord(string) {
return string.replace(this.regex, '').replace(this.replaceRegex, this.placeHolder);
};
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.
*/
Filter.prototype.clean = function clean(string) {
return string.split(/\b/).map(function (word) {
clean(string) {
return string.split(/\b/).map((word) => {
return this.isProfane(word) ? this.replaceWord(word) : word;
}.bind(this)).join('');
};

}).join('');
}
/**
* Add words to blacklist filter / remove words from whitelist filter
* @param {(string|string[])}
* Add word(s) to blacklist filter / remove words from whitelist filter
* @param {...string}
*/
Filter.prototype.addWords = function addWords() {
addWords() {
let words = Array.from(arguments);

this.list.push(...words);

words.forEach(function (word) {
words.forEach((word) => {
if (!!~this.exclude.indexOf(word)) {
this.exclude.splice(this.exclude.indexOf(word), 1);
}
}, this);
};

});
}
/**
* Add words to whitelist filter
* @param {(string|string[])} word - Word to add to whitelist.
* @param {...string} word - Word(s) to add to whitelist.
*/
Filter.prototype.removeWords = function removeWords() {
removeWords() {
this.exclude.push(...Array.from(arguments));
};

return Filter;
})();
}
}

module.exports = Filter;

0 comments on commit dd4632f

Please sign in to comment.