Skip to content

Commit

Permalink
Don't crash w/strings with no word boundaries (fixes web-mech#93)
Browse files Browse the repository at this point in the history
"TypeError: Cannot read property '0' of null"
  • Loading branch information
brianreavis authored and callumooi committed Apr 19, 2021
1 parent f4d9dd2 commit e8b50a0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
6 changes: 4 additions & 2 deletions lib/badwords.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -85,4 +87,4 @@ class Filter {
}
}

module.exports = Filter;
module.exports = Filter;
14 changes: 10 additions & 4 deletions test/filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('🙂') === '🙂');
});
});
});
});

0 comments on commit e8b50a0

Please sign in to comment.