From 4c515b991da90a5c27740dfa0070d7c4da4aaa25 Mon Sep 17 00:00:00 2001 From: Jeffrey Ukutegbe <46923496+JeffCorp@users.noreply.github.com> Date: Fri, 11 Feb 2022 14:08:56 +0100 Subject: [PATCH 1/4] Update badwords.js Checking in between words --- lib/badwords.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/badwords.js b/lib/badwords.js index 3990c41..8fd2749 100644 --- a/lib/badwords.js +++ b/lib/badwords.js @@ -35,6 +35,9 @@ class Filter { const wordExp = new RegExp(`\\b${word.replace(/(\W)/g, '\\$1')}\\b`, 'gi'); return !this.exclude.includes(word.toLowerCase()) && wordExp.test(string); }) + .length > 0 || this.list.filter((word) => { + return string.toLowerCase().includes(word.toLowerCase()) + }) .length > 0 || false; } @@ -85,4 +88,4 @@ class Filter { } } -module.exports = Filter; \ No newline at end of file +module.exports = Filter; From e9f070bc460bc5d09f876112b94274faa14d7e3c Mon Sep 17 00:00:00 2001 From: Smile Phoenix Date: Thu, 26 Jan 2023 04:06:17 -0800 Subject: [PATCH 2/4] feat: append isAnyProfane function against the exclude array --- lib/badwords.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/badwords.js b/lib/badwords.js index 8fd2749..e0a7e7b 100644 --- a/lib/badwords.js +++ b/lib/badwords.js @@ -36,11 +36,25 @@ class Filter { return !this.exclude.includes(word.toLowerCase()) && wordExp.test(string); }) .length > 0 || this.list.filter((word) => { - return string.toLowerCase().includes(word.toLowerCase()) + return string.toLowerCase().includes(word.toLowerCase()) + }) + .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. From ab2032275d8e1e7cb4799e920acc87ab9cb1121b Mon Sep 17 00:00:00 2001 From: Smile Phoenix Date: Thu, 26 Jan 2023 04:09:55 -0800 Subject: [PATCH 3/4] feat: append testcase of isAnyProfane function --- test/isProfane.js | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) 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!")); + }); + }); }); From c2fa26367492a206876f7d6eaee3f9cfad64832d Mon Sep 17 00:00:00 2001 From: Smile Phoenix Date: Thu, 26 Jan 2023 04:41:22 -0800 Subject: [PATCH 4/4] fix: omit to return true when the word include badword --- lib/badwords.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/badwords.js b/lib/badwords.js index e0a7e7b..bc59845 100644 --- a/lib/badwords.js +++ b/lib/badwords.js @@ -35,10 +35,7 @@ class Filter { const wordExp = new RegExp(`\\b${word.replace(/(\W)/g, '\\$1')}\\b`, 'gi'); return !this.exclude.includes(word.toLowerCase()) && wordExp.test(string); }) - .length > 0 || this.list.filter((word) => { - return string.toLowerCase().includes(word.toLowerCase()) - }) - .length > 0 || false; + .length > 0 || false; } /**