Skip to content

Commit e8b50a0

Browse files
brianreaviscallumooi
authored andcommitted
Don't crash w/strings with no word boundaries (fixes web-mech#93)
"TypeError: Cannot read property '0' of null"
1 parent f4d9dd2 commit e8b50a0

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

lib/badwords.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@ class Filter {
5353
* @param {string} string - Sentence to filter.
5454
*/
5555
clean(string) {
56+
const joinMatch = this.splitRegex.exec(string);
57+
const joinString = (joinMatch && joinMatch[0]) || '';
5658
return string.split(this.splitRegex).map((word) => {
5759
return this.isProfane(word) ? this.replaceWord(word) : word;
58-
}).join(this.splitRegex.exec(string)[0]);
60+
}).join(joinString);
5961
}
6062

6163
/**
@@ -85,4 +87,4 @@ class Filter {
8587
}
8688
}
8789

88-
module.exports = Filter;
90+
module.exports = Filter;

test/filter.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,16 @@ describe('filter', function(){
3838

3939
xit('Should filter words that are derivatives of words from the filter blacklist', function() {
4040
assert(filter.clean('shitshit') === '********');
41-
});
41+
});
4242

43-
it('Shouldn\'t filter words that aren\'t profane.', function() {
43+
it('Shouldn\'t filter words that aren\'t profane.', function() {
4444
assert(filter.clean('hello there') === 'hello there');
45-
});
45+
});
46+
47+
it('Should handle strings with no word boundaries', function() {
48+
assert(filter.clean('') === '');
49+
assert(filter.clean('.') === '.');
50+
assert(filter.clean('🙂') === '🙂');
51+
});
4652
});
47-
});
53+
});

0 commit comments

Comments
 (0)