Comparing version 2.0.0 to 3.0.0
@@ -5,2 +5,17 @@ # Change Log | ||
<a name="3.0.0"></a> | ||
# [3.0.0](https://github.com/web-mech/badwords/compare/v2.0.0...v3.0.0) (2018-10-23) | ||
### Code Refactoring | ||
* utilize es6 spread in addWords/removeWords ([656b87c](https://github.com/web-mech/badwords/commit/656b87c)) | ||
### BREAKING CHANGES | ||
* changes the way addWords is used, no longer accepts a single array as a parameter unless used with the spread operator | ||
<a name="2.0.0"></a> | ||
@@ -7,0 +22,0 @@ # [2.0.0](https://github.com/web-mech/badwords/compare/v1.6.5...v2.0.0) (2018-10-23) |
@@ -1,4 +0,6 @@ | ||
var localList = require('./lang.json').words; | ||
var baseList = require('badwords-list').array; | ||
var Filter = (function () { | ||
const localList = require('./lang.json').words; | ||
const baseList = require('badwords-list').array; | ||
class Filter { | ||
/** | ||
@@ -14,9 +16,10 @@ * Filter constructor. | ||
*/ | ||
function Filter(options) { | ||
options = options || {}; | ||
this.list = options.emptyList && [] || Array.prototype.concat.apply(localList, [baseList, options.list || []]); | ||
this.exclude = options.exclude || []; | ||
this.placeHolder = options.placeHolder || '*'; | ||
this.regex = options.regex || /[^a-zA-Z0-9|\$|\@]|\^/g; | ||
this.replaceRegex = options.replaceRegex || /\w/g; | ||
constructor(options = {}) { | ||
Object.assign(this, { | ||
list: options.emptyList && [] || Array.prototype.concat.apply(localList, [baseList, options.list || []]), | ||
exclude: options.exclude || [], | ||
placeHolder: options.placeHolder || '*', | ||
regex: options.regex || /[^a-zA-Z0-9|\$|\@]|\^/g, | ||
replaceRegex: options.replaceRegex || /\w/g | ||
}) | ||
} | ||
@@ -28,40 +31,20 @@ | ||
*/ | ||
Filter.prototype.isProfane = function isProfane(string) { | ||
isProfane(string) { | ||
return this.list | ||
.filter(function (word) { | ||
.filter((word) => { | ||
const wordExp = new RegExp(`\\b${word.replace(/(\W)/g, '\\$1')}\\b`, 'gi'); | ||
return !this.exclude.includes(word) && wordExp.test(string); | ||
}, this) | ||
.shift() || false; | ||
}; | ||
}) | ||
.length > 0 || false; | ||
} | ||
/** | ||
* Determine if a single word is profane or looks profane. | ||
* @param {string} word - String to evaluate for profanity. | ||
*/ | ||
Filter.prototype.isProfaneLike = function profaneLike(word) { | ||
if (!!~this.exclude.indexOf(word)) { | ||
return false; | ||
} | ||
if (!!~this.list.indexOf(word)) { | ||
return true; | ||
} | ||
return this.list | ||
.map(function (w) { | ||
return new RegExp('^' + w.replace(/(\W)/g, '\\$1') + '$', 'gi'); | ||
}, this) | ||
.reduce(function (outcome, wordExp) { | ||
return outcome || wordExp.test(word); | ||
}, false); | ||
}; | ||
/** | ||
* Replace a word with placeHolder characters; | ||
* @param {string} string - String to replace. | ||
*/ | ||
Filter.prototype.replaceWord = function replaceWord(string) { | ||
return string.replace(this.regex, '').replace(this.replaceRegex, this.placeHolder); | ||
}; | ||
replaceWord(string) { | ||
return string | ||
.replace(this.regex, '') | ||
.replace(this.replaceRegex, this.placeHolder); | ||
} | ||
@@ -72,35 +55,33 @@ /** | ||
*/ | ||
Filter.prototype.clean = function clean(string) { | ||
return string.split(/\b/).map(function (word) { | ||
clean(string) { | ||
return string.split(/\b/).map((word) => { | ||
return this.isProfane(word) ? this.replaceWord(word) : word; | ||
}.bind(this)).join(''); | ||
}; | ||
}).join(''); | ||
} | ||
/** | ||
* Add words to blacklist filter / remove words from whitelist filter | ||
* @param {(string|string[])} | ||
* Add word(s) to blacklist filter / remove words from whitelist filter | ||
* @param {...string} word - Word(s) to add to blacklist | ||
*/ | ||
Filter.prototype.addWords = function addWords(words) { | ||
words = (words instanceof Array) ? words : [words]; | ||
this.list = this.list.concat(words); | ||
addWords() { | ||
let words = Array.from(arguments); | ||
words.forEach(function (word) { | ||
if (!!~this.exclude.indexOf(word)) { | ||
this.list.push(...words); | ||
words.forEach((word) => { | ||
if (this.exclude.includes(word)) { | ||
this.exclude.splice(this.exclude.indexOf(word), 1); | ||
} | ||
}, this); | ||
}; | ||
}); | ||
} | ||
/** | ||
* Add words to whitelist filter | ||
* @param {...string} word - Word to add to whitelist. | ||
* @param {...string} word - Word(s) to add to whitelist. | ||
*/ | ||
Filter.prototype.removeWords = function removeWords() { | ||
var words = Array.prototype.slice.call(arguments); | ||
this.exclude.push.apply(this.exclude, words); | ||
}; | ||
removeWords() { | ||
this.exclude.push(...Array.from(arguments)); | ||
} | ||
} | ||
return Filter; | ||
})(); | ||
module.exports = Filter; | ||
module.exports = Filter; |
{ | ||
"name": "bad-words", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "A javascript filter for bad words", | ||
@@ -32,3 +32,6 @@ "main": "./lib/badwords", | ||
"author": "Mike P.", | ||
"license": "MIT" | ||
"license": "MIT", | ||
"engines": { | ||
"node": ">=8.0.0" | ||
} | ||
} |
@@ -15,5 +15,3 @@ # bad-words | ||
``` | ||
npm install bad-words | ||
``` | ||
npm install bad-words | ||
@@ -52,6 +50,14 @@ ## Usage | ||
filter.addWords(['some', 'bad', 'word']); | ||
filter.addWords('some', 'bad', 'word'); | ||
filter.clean("some bad word!") //**** *** ****! | ||
//or use an array using the spread operator | ||
var newBadWords = ['some', 'bad', 'word']; | ||
filter.addWords(...newBadWords); | ||
filter.clean("some bad word!") //**** *** ****! | ||
//or | ||
@@ -74,7 +80,15 @@ | ||
```js | ||
var filter = new Filter(); | ||
let filter = new Filter(); | ||
filter.removeWords('hells'); | ||
filter.removeWords('hells' 'sadist'); | ||
filter.clean("some hells word!"); //some hells word! | ||
//or use an array using the spread operator | ||
let removeWords = ['hells', 'sadist']; | ||
filter.removeWords(...removeWords); | ||
filter.clean("some sadist hells word!"); //some sadist hells word! | ||
``` | ||
@@ -86,3 +100,3 @@ | ||
#### Filter | ||
#### constructor | ||
@@ -93,3 +107,3 @@ Filter constructor. | ||
- `options` **[object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** Filter instance options | ||
- `options` **[object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** Filter instance options (optional, default `{}`) | ||
- `options.emptyList` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Instantiate filter with no blacklist | ||
@@ -101,3 +115,3 @@ - `options.list` **[array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)** Instantiate filter with custom list | ||
##### isProfane | ||
#### isProfane | ||
@@ -110,12 +124,4 @@ Determine if a string contains profane language. | ||
##### isProfaneLike | ||
#### replaceWord | ||
Determine if a single word is profane or looks profane. | ||
**Parameters** | ||
- `word` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** String to evaluate for profanity. | ||
##### replaceWord | ||
Replace a word with placeHolder characters; | ||
@@ -127,3 +133,3 @@ | ||
##### clean | ||
#### clean | ||
@@ -136,11 +142,11 @@ Evaluate a string for profanity and return an edited version. | ||
##### addWords | ||
#### addWords | ||
Add words to blacklist filter / remove words from whitelist filter | ||
Add word(s) to blacklist filter / remove words from whitelist filter | ||
**Parameters** | ||
- `words` | ||
- `word` **...[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Word(s) to add to blacklist | ||
##### removeWords | ||
#### removeWords | ||
@@ -151,20 +157,8 @@ Add words to whitelist filter | ||
- `word` **...[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Word to add to whitelist. | ||
- `word` **...[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Word(s) to add to whitelist. | ||
## Testing | ||
``` | ||
npm test | ||
``` | ||
npm test | ||
## Release Notes | ||
- v1.1.0 / Mar 17 2015: Added soundex support for comparing words to things not in the list. | ||
- v1.2.0 / May 29 2015: Removed soundex logic which resulted in many false positives within the isProfane test. | ||
- v1.3.0 / Oct 1 2015: Updated local list and documentation. Added ability to pass a custom list of words during construction. | ||
- v1.4.0 / Sept 2 2016: Added removeWords feature. Added emptyList configuration parameter. | ||
- v1.4.1 / Sept 2 2016: Updated documentation. | ||
- v1.4.3 / Jan 21 2017: Add multilingual support for word filtering | ||
- v1.5.1 / April 14 2017: Patch for word tokenization. | ||
## License | ||
@@ -171,0 +165,0 @@ |
@@ -9,8 +9,15 @@ require('assert'); | ||
it('Should append words to the filter list.', function(){ | ||
filter.addWords('dog'); | ||
assert(filter.clean('Go dog go') === 'Go *** go'); | ||
filter.addWords('dog', 'go'); | ||
assert(filter.clean('Go dog go') === '** *** **'); | ||
}); | ||
it('Should append words to the filter using an array', () => { | ||
let addWords = ['go', 'dog']; | ||
filter = new Filter() | ||
filter.addWords(...addWords); | ||
assert(filter.clean('Go dog go') === '** *** **'); | ||
}); | ||
it('Should allow a list to be passed to the constructor', function() { | ||
var filter = new Filter({ | ||
filter = new Filter({ | ||
list: ['dog'] | ||
@@ -17,0 +24,0 @@ }); |
@@ -6,11 +6,17 @@ require('assert'); | ||
describe('filter', function(){ | ||
describe('removeWords',function(){ | ||
it("Should allow you to remove words from the filter blacklist and no longer filter them",function(){ | ||
describe('filter', () => { | ||
describe('removeWords',() => { | ||
it('Should allow you to remove words from the filter blacklist and no longer filter them', () => { | ||
filter.removeWords('hells'); | ||
assert(filter.clean('This is a hells good test') === 'This is a hells good test'); | ||
filter.addWords('hells'); | ||
assert(filter.clean('This is a hells good test') === 'This is a ***** good test'); | ||
}); | ||
it ('Should allow you to remove an array of words from the filter blacklist and no longer filter them', () => { | ||
let removingWords = ['hells', 'sadist']; | ||
filter = new Filter(); | ||
filter.removeWords(...removingWords); | ||
assert(filter.clean('This is a hells sadist test') === 'This is a hells sadist test'); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27889
754
176