Skip to content

Commit

Permalink
Merge pull request #59 from p9f/split-regex
Browse files Browse the repository at this point in the history
Introduce splitRegex option.
  • Loading branch information
web-mech authored Oct 23, 2019
2 parents 3b6febd + 6c97852 commit 8886761
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/badwords.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ class Filter {
* @param {string} options.placeHolder - Character used to replace profane words.
* @param {string} options.regex - Regular expression used to sanitize words before comparing them to blacklist.
* @param {string} options.replaceRegex - Regular expression used to replace profane words with placeHolder.
* @param {string} options.splitRegex - Regular expression used to split a string into words.
*/
constructor(options = {}) {
Object.assign(this, {
list: options.emptyList && [] || Array.prototype.concat.apply(localList, [baseList, options.list || []]),
exclude: options.exclude || [],
splitRegex: options.splitRegex || /\b/,
placeHolder: options.placeHolder || '*',
regex: options.regex || /[^a-zA-Z0-9|\$|\@]|\^/g,
replaceRegex: options.replaceRegex || /\w/g
Expand Down Expand Up @@ -51,9 +53,9 @@ class Filter {
* @param {string} string - Sentence to filter.
*/
clean(string) {
return string.split(/\b/).map((word) => {
return string.split(this.splitRegex).map((word) => {
return this.isProfane(word) ? this.replaceWord(word) : word;
}).join('');
}).join(this.splitRegex.exec(string)[0]);
}

/**
Expand Down
24 changes: 24 additions & 0 deletions test/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require('assert');
var Filter = require('../lib/badwords.js'),
assert = require('better-assert');

describe('options', function() {
describe('split regex', function() {

it('default value', function() {
filter = new Filter();
filter.addWords('français');
assert(filter.clean('fucking asshole') == '******* *******');
assert(filter.clean('mot en français') == 'mot en français');
});

it('override value', function() {
filter = new Filter({splitRegex: / /});
filter.addWords('français');
assert(filter.clean('fucking asshole') == '******* *******');
assert(filter.clean('mot en français') == 'mot en *******');
});


});
});

0 comments on commit 8886761

Please sign in to comment.