diff --git a/lib/badwords.js b/lib/badwords.js index 9091708..3990c41 100644 --- a/lib/badwords.js +++ b/lib/badwords.js @@ -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 @@ -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]); } /** diff --git a/test/options.js b/test/options.js new file mode 100644 index 0000000..cada98f --- /dev/null +++ b/test/options.js @@ -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 *******'); + }); + + + }); +});