Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't crash w/strings with no word boundaries #95

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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('🙂') === '🙂');
});
});
});
});