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 +});