diff --git a/lib/badwords.js b/lib/badwords.js index 3990c41..bc59845 100644 --- a/lib/badwords.js +++ b/lib/badwords.js @@ -38,6 +38,20 @@ class Filter { .length > 0 || false; } + /** + * Determine if a string contains profane language against the exclude array. + * @param {string} string - String to evaluate for profanity. + */ + isAnyProfane(string) { + return this.list + .filter((word) => { + const wordExp = new RegExp(`\\b${word.replace(/(\W)/g, '\\$1')}\\b`, 'gi'); + return wordExp.test(string); + }) + .length > 0 || false; + } + + /** * Replace a word with placeHolder characters; * @param {string} string - String to replace. @@ -85,4 +99,4 @@ class Filter { } } -module.exports = Filter; \ No newline at end of file +module.exports = Filter; diff --git a/test/isProfane.js b/test/isProfane.js index 208b961..5aac07b 100644 --- a/test/isProfane.js +++ b/test/isProfane.js @@ -3,36 +3,36 @@ var Filter = require('../lib/badwords.js'), filter = new Filter(), assert = require('better-assert'); -describe('filter', function(){ - describe('isProfane',function(){ - it("Should detect a bad word and return a boolean value",function(){ +describe('filter', function () { + describe('isProfane', function () { + it("Should detect a bad word and return a boolean value", function () { assert(filter.isProfane("ash0le")); }); - it("Should return false when no bad word is detected",function(){ + it("Should return false when no bad word is detected", function () { assert(filter.isProfane("wife") === false); }); - it("Should be able to detect a bad word in a sentence",function(){ + it("Should be able to detect a bad word in a sentence", function () { assert(filter.isProfane("that person is an ash0le")); }); - it('Filters out special characters appropriately', function() { + it('Filters out special characters appropriately', function () { assert(filter.isProfane("You're an asshole^ you are")); }); - it('Should detect filtered words from badwords-list', function(){ + it('Should detect filtered words from badwords-list', function () { assert(filter.isProfane('willies')); }); - it('Should detect filtered words regardless of type case', function() { + it('Should detect filtered words regardless of type case', function () { var filter = new Filter({ list: ['Test'] }); assert(filter.isProfane('test')); }); - it('Should tokenize words according to regex word boundaries', function() { + it('Should tokenize words according to regex word boundaries', function () { assert(filter.isProfane("that person is an\nasshole")); }); @@ -40,5 +40,16 @@ describe('filter', function(){ filter.addWords('oh no'); assert(filter.isProfane("oh no! this is profane!")); }); + + it('Should not detect bad word when remove the bad word', function () { + filter.removeWords('willies'); + assert(filter.isProfane("willies! this is not a profane!") == false); + }); + + it('Should detect bad word regardless remove the bad word isAnyProfane function', function () { + filter.removeWords('willies'); + assert(filter.isAnyProfane("willies! this is not a profane!")); + }); + }); });