@2toad/profanity
Advanced tools
Comparing version 1.0.3 to 1.1.0
import { ProfanityOptions } from './profanity-options'; | ||
import { List } from './list'; | ||
export declare class Profanity { | ||
options: ProfanityOptions; | ||
whitelist: List; | ||
private blacklist; | ||
private regex; | ||
words: string[]; | ||
options: ProfanityOptions; | ||
constructor(options?: ProfanityOptions); | ||
exists(text: string): boolean; | ||
censor(text: string): string; | ||
addWords(words: string[]): void; | ||
removeWords(words: string[]): void; | ||
addWords(words: string[]): void; | ||
private buildRegex; | ||
@@ -12,0 +14,0 @@ } |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const path_1 = require("path"); | ||
const fs_1 = require("fs"); | ||
const profanity_options_1 = require("./profanity-options"); | ||
const list_1 = require("./list"); | ||
class Profanity { | ||
constructor(options) { | ||
this.options = options || new profanity_options_1.ProfanityOptions(); | ||
const file = fs_1.readFileSync(path_1.resolve(__dirname, 'words.txt'), 'utf8'); | ||
this.words = file.split('\n'); | ||
this.buildRegex(); | ||
this.whitelist = new list_1.List(() => this.buildRegex()); | ||
this.blacklist = new list_1.List(() => this.buildRegex()); | ||
this.blacklist.loadFile(path_1.resolve(__dirname, 'words.txt')); | ||
} | ||
@@ -20,13 +20,12 @@ exists(text) { | ||
} | ||
addWords(words) { | ||
this.blacklist.addWords(words); | ||
} | ||
removeWords(words) { | ||
this.words = this.words.filter(x => !words.includes(x)); | ||
this.buildRegex(); | ||
this.blacklist.removeWords(words); | ||
} | ||
addWords(words) { | ||
this.words = this.words.concat(words); | ||
this.buildRegex(); | ||
} | ||
buildRegex() { | ||
const pattern = `${this.options.wholeWord ? '\\b' : ''}(${this.words.join('|')})${this.options.wholeWord ? '\\b' : ''}`; | ||
this.regex = new RegExp(pattern, 'ig'); | ||
const blacklistPattern = `${this.options.wholeWord ? '\\b' : ''}(${this.blacklist.words.join('|')})${this.options.wholeWord ? '\\b' : ''}`; | ||
const whitelistPattern = this.whitelist.empty ? '' : `(?!${this.whitelist.words.join('|')})`; | ||
this.regex = new RegExp(whitelistPattern + blacklistPattern, 'ig'); | ||
} | ||
@@ -33,0 +32,0 @@ } |
@@ -14,3 +14,3 @@ "use strict"; | ||
it('should return false when profanity is not a whole word in a sentence', () => { | ||
chai_1.expect(_1.profanity.exists('Should we censor the word buttArse?')).to.equal(false); | ||
chai_1.expect(_1.profanity.exists('Should we censor the word arsenic?')).to.equal(false); | ||
}); | ||
@@ -32,9 +32,9 @@ it('should return true when profanity exists within multiple lines', () => { | ||
const customProfanity = new _1.Profanity(options); | ||
it('should return true when profanity is not a whole word in a sentence', () => { | ||
chai_1.expect(customProfanity.exists('Should we censor the word buttArse?')).to.equal(true); | ||
it('should return true when profanity is part of a word in a sentence', () => { | ||
chai_1.expect(customProfanity.exists('Should we censor the word arsenic?')).to.equal(true); | ||
}); | ||
it('should return true when profanity is not a whole word, within multiple lines', () => { | ||
it('should return true when profanity is part of a word, within multiple lines', () => { | ||
chai_1.expect(customProfanity.exists(` | ||
Nothing profane on line 1. | ||
Censoring buttArse on line 2. | ||
Censoring arsenic on line 2. | ||
Nothing profane on line 3. | ||
@@ -46,4 +46,4 @@ `)).to.equal(true); | ||
}); | ||
it('should return true when concatenated profanity exists as a single word', () => { | ||
chai_1.expect(customProfanity.exists('buttArse')).to.equal(true); | ||
it('should return true when profanity exists as part of a single word', () => { | ||
chai_1.expect(customProfanity.exists('arsenic')).to.equal(true); | ||
}); | ||
@@ -78,5 +78,17 @@ }); | ||
}); | ||
describe('addWords', () => { | ||
it('should add a single word to the list of profane words', () => { | ||
const customProfanity = new _1.Profanity(); | ||
customProfanity.addWords(['aardvark']); | ||
chai_1.expect(customProfanity.exists('Should we censor the word aardvark?')).to.equal(true); | ||
}); | ||
it('should add mulitple words to the list of profane words', () => { | ||
const customProfanity = new _1.Profanity(); | ||
customProfanity.addWords(['aardvark', 'zebra']); | ||
chai_1.expect(customProfanity.exists('Should we censor the word aardvark and zebra?')).to.equal(true); | ||
}); | ||
}); | ||
describe('removeWords', () => { | ||
const customProfanity = new _1.Profanity(); | ||
it('should remove a single word from the list of profane words', () => { | ||
const customProfanity = new _1.Profanity(); | ||
customProfanity.removeWords(['butts']); | ||
@@ -86,2 +98,3 @@ chai_1.expect(customProfanity.exists('I like big butts and I cannot lie')).to.equal(false); | ||
it('should remove mulitple words from the list of profane words', () => { | ||
const customProfanity = new _1.Profanity(); | ||
customProfanity.removeWords(['butts', 'arses']); | ||
@@ -91,14 +104,45 @@ chai_1.expect(customProfanity.exists('I like big butts (aka arses) and I cannot lie')).to.equal(false); | ||
}); | ||
describe('addWords', () => { | ||
const customProfanity = new _1.Profanity(); | ||
it('should add a single word to the list of profane words', () => { | ||
customProfanity.addWords(['aardvark']); | ||
chai_1.expect(customProfanity.exists('Should we censor the word aardvark?')).to.equal(true); | ||
describe('Whitelist (wholeWord = true)', () => { | ||
it('should whitelist a single word', () => { | ||
const customProfanity = new _1.Profanity(); | ||
customProfanity.whitelist.addWords(['butt']); | ||
chai_1.expect(customProfanity.exists('Should we censor the word butt?')).to.equal(false); | ||
}); | ||
it('should add mulitple words to the list of profane words', () => { | ||
customProfanity.addWords(['aardvark', 'zebra']); | ||
chai_1.expect(customProfanity.exists('Should we censor the word aardvark and zebra?')).to.equal(true); | ||
it('should whitelist multiple words', () => { | ||
const customProfanity = new _1.Profanity(); | ||
customProfanity.whitelist.addWords(['butt', 'arse']); | ||
chai_1.expect(customProfanity.exists('Should we censor the word butt or arse?')).to.equal(false); | ||
}); | ||
}); | ||
describe('Whitelist (wholeWord = false)', () => { | ||
const options = new _1.ProfanityOptions(); | ||
options.wholeWord = false; | ||
it('should whitelist a single word', () => { | ||
const customProfanity = new _1.Profanity(options); | ||
customProfanity.whitelist.addWords(['buttocks']); | ||
chai_1.expect(customProfanity.exists('Should we censor the word buttocks?')).to.equal(false); | ||
}); | ||
it('should whitelist multiple words', () => { | ||
const customProfanity = new _1.Profanity(options); | ||
customProfanity.whitelist.addWords(['buttocks', 'arsenic']); | ||
chai_1.expect(customProfanity.exists('Should we censor the word buttocks or arsenic?')).to.equal(false); | ||
}); | ||
}); | ||
describe('Whitelist - removeWords', () => { | ||
it('should remove a single word from the whitelist', () => { | ||
const customProfanity = new _1.Profanity(); | ||
customProfanity.whitelist.addWords(['butts', 'arses']); | ||
chai_1.expect(customProfanity.exists('I like big butts and I cannot lie')).to.equal(false); | ||
customProfanity.whitelist.removeWords(['butts']); | ||
chai_1.expect(customProfanity.exists('I like big butts and I cannot lie')).to.equal(true); | ||
}); | ||
it('should remove multiple words from the whitelist', () => { | ||
const customProfanity = new _1.Profanity(); | ||
customProfanity.whitelist.addWords(['butts', 'arses']); | ||
chai_1.expect(customProfanity.exists('I like big butts (aka arses) and I cannot lie')).to.equal(false); | ||
customProfanity.whitelist.removeWords(['butts']); | ||
chai_1.expect(customProfanity.exists('I like big butts (aka arses) and I cannot lie')).to.equal(true); | ||
}); | ||
}); | ||
}); | ||
//# sourceMappingURL=profanity.spec.js.map |
{ | ||
"name": "@2toad/profanity", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "A JavaScript profanity filter", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/2Toad/Profanity", |
@@ -36,3 +36,3 @@ # Profanity | ||
## Options | ||
There are several configurable options you can set when you create an instance of the Profanity class: | ||
Create an instance of the Profanity class to change the default options: | ||
@@ -53,10 +53,10 @@ ``` | ||
``` | ||
profanity.exists('Two profane words joined: buttsAr5e'); | ||
profanity.exists('Arsenic is poisonous but not profane'); | ||
// false | ||
``` | ||
Setting this to `false`, results in: | ||
Setting this to `false`, results in partial word matches: | ||
``` | ||
profanity.exists('Two profane words joined: buttsAr5e'); | ||
// true | ||
profanity.exists('Arsenic is poisonous but not profane'); | ||
// true (matched on arse) | ||
``` | ||
@@ -80,10 +80,25 @@ | ||
Add words: | ||
``` | ||
profanity.addWords(['aardvark', 'zebra']); | ||
``` | ||
Remove words: | ||
``` | ||
profanity.removeWords(['butt', 'ar5e']); | ||
profanity.removeWords(['butt', 'arse']); | ||
``` | ||
Add words: | ||
## Whitelist | ||
The whitelist allows you to specify words that are always ignored by the profanity filter. | ||
>This can be useful if you want to turn partial word matching on (`wholeWord = true`), so combined words are caught (e.g., arselicker), while specific words you add to the whitelist are ignored (e.g., arsenic). | ||
Add words to the whitelist: | ||
``` | ||
profanity.addWords(['aardvark', 'zebra']); | ||
profanity.whitelist.addWords(['arsenic', 'buttress']); | ||
``` | ||
Remove words from the whitelist: | ||
``` | ||
profanity.whitelist.removeWords(['arsenic', 'buttress']); | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
43782
36
576
102
4