From 235121d54559424d1c2d7fbf7d8dcb7d7e0e8e18 Mon Sep 17 00:00:00 2001 From: webmech Date: Mon, 18 Feb 2019 21:46:45 -0500 Subject: [PATCH] fix(*): update removeWords functionality to be case-insensitive https://github.com/web-mech/badwords/issues/50 --- lib/badwords.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/badwords.js b/lib/badwords.js index bee2989..9091708 100644 --- a/lib/badwords.js +++ b/lib/badwords.js @@ -31,7 +31,7 @@ class Filter { return this.list .filter((word) => { const wordExp = new RegExp(`\\b${word.replace(/(\W)/g, '\\$1')}\\b`, 'gi'); - return !this.exclude.includes(word) && wordExp.test(string); + return !this.exclude.includes(word.toLowerCase()) && wordExp.test(string); }) .length > 0 || false; } @@ -65,11 +65,13 @@ class Filter { this.list.push(...words); - words.forEach((word) => { - if (this.exclude.includes(word)) { - this.exclude.splice(this.exclude.indexOf(word), 1); - } - }); + words + .map(word => word.toLowerCase()) + .forEach((word) => { + if (this.exclude.includes(word)) { + this.exclude.splice(this.exclude.indexOf(word), 1); + } + }); } /** @@ -77,7 +79,7 @@ class Filter { * @param {...string} word - Word(s) to add to whitelist. */ removeWords() { - this.exclude.push(...Array.from(arguments)); + this.exclude.push(...Array.from(arguments).map(word => word.toLowerCase())); } }