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

Update badwords.js #142

Open
wants to merge 4 commits 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
16 changes: 15 additions & 1 deletion lib/badwords.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ class Filter {
.length > 0 || false;
}

/**
* Determine if a string contains profane language against the exclude array.
* @param {string} string - String to evaluate for profanity.
*/
isAnyProfane(string) {
return this.list
.filter((word) => {
const wordExp = new RegExp(`\\b${word.replace(/(\W)/g, '\\$1')}\\b`, 'gi');
return wordExp.test(string);
})
.length > 0 || false;
}


/**
* Replace a word with placeHolder characters;
* @param {string} string - String to replace.
Expand Down Expand Up @@ -85,4 +99,4 @@ class Filter {
}
}

module.exports = Filter;
module.exports = Filter;
29 changes: 20 additions & 9 deletions test/isProfane.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,53 @@ var Filter = require('../lib/badwords.js'),
filter = new Filter(),
assert = require('better-assert');

describe('filter', function(){
describe('isProfane',function(){
it("Should detect a bad word and return a boolean value",function(){
describe('filter', function () {
describe('isProfane', function () {
it("Should detect a bad word and return a boolean value", function () {
assert(filter.isProfane("ash0le"));
});

it("Should return false when no bad word is detected",function(){
it("Should return false when no bad word is detected", function () {
assert(filter.isProfane("wife") === false);
});

it("Should be able to detect a bad word in a sentence",function(){
it("Should be able to detect a bad word in a sentence", function () {
assert(filter.isProfane("that person is an ash0le"));
});

it('Filters out special characters appropriately', function() {
it('Filters out special characters appropriately', function () {
assert(filter.isProfane("You're an asshole^ you are"));
});

it('Should detect filtered words from badwords-list', function(){
it('Should detect filtered words from badwords-list', function () {
assert(filter.isProfane('willies'));
});

it('Should detect filtered words regardless of type case', function() {
it('Should detect filtered words regardless of type case', function () {
var filter = new Filter({
list: ['Test']
});
assert(filter.isProfane('test'));
});

it('Should tokenize words according to regex word boundaries', function() {
it('Should tokenize words according to regex word boundaries', function () {
assert(filter.isProfane("that person is an\nasshole"));
});

it('Should detect bad word phrases', function () {
filter.addWords('oh no');
assert(filter.isProfane("oh no! this is profane!"));
});

it('Should not detect bad word when remove the bad word', function () {
filter.removeWords('willies');
assert(filter.isProfane("willies! this is not a profane!") == false);
});

it('Should detect bad word regardless remove the bad word isAnyProfane function', function () {
filter.removeWords('willies');
assert(filter.isAnyProfane("willies! this is not a profane!"));
});

});
});