From d05edc0484be35d541210e5ba929e23f82daaf7b Mon Sep 17 00:00:00 2001 From: Brian Reavis Date: Fri, 4 Dec 2020 12:01:07 -0800 Subject: [PATCH] Don't crash w/strings with no word boundaries (fixes https://github.com/web-mech/badwords/issues/93) "TypeError: Cannot read property '0' of null" --- lib/badwords.js | 6 ++++-- test/filter.js | 14 ++++++++++---- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/badwords.js b/lib/badwords.js index 3990c41..4b2bf94 100644 --- a/lib/badwords.js +++ b/lib/badwords.js @@ -53,9 +53,11 @@ class Filter { * @param {string} string - Sentence to filter. */ clean(string) { + const joinMatch = this.splitRegex.exec(string); + const joinString = (joinMatch && joinMatch[0]) || ''; return string.split(this.splitRegex).map((word) => { return this.isProfane(word) ? this.replaceWord(word) : word; - }).join(this.splitRegex.exec(string)[0]); + }).join(joinString); } /** @@ -85,4 +87,4 @@ class Filter { } } -module.exports = Filter; \ No newline at end of file +module.exports = Filter; diff --git a/test/filter.js b/test/filter.js index 813d86d..a0827c0 100644 --- a/test/filter.js +++ b/test/filter.js @@ -38,10 +38,16 @@ describe('filter', function(){ xit('Should filter words that are derivatives of words from the filter blacklist', function() { assert(filter.clean('shitshit') === '********'); - }); + }); - it('Shouldn\'t filter words that aren\'t profane.', function() { + it('Shouldn\'t filter words that aren\'t profane.', function() { assert(filter.clean('hello there') === 'hello there'); - }); + }); + + it('Should handle strings with no word boundaries', function() { + assert(filter.clean('') === ''); + assert(filter.clean('.') === '.'); + assert(filter.clean('🙂') === '🙂'); + }); }); -}); \ No newline at end of file +});