@2toad/profanity
Advanced tools
Comparing version 1.2.0 to 1.3.0
@@ -1,2 +0,2 @@ | ||
export * from './profanity'; | ||
export * from './profanity-options'; | ||
export * from "./profanity"; | ||
export * from "./profanity-options"; |
export declare class List { | ||
words: string[]; | ||
onListChanged: Function; | ||
onListChanged: () => void; | ||
get empty(): boolean; | ||
constructor(onListChanged: Function); | ||
constructor(onListChanged: () => void); | ||
loadFile(filename: string): void; | ||
@@ -7,0 +7,0 @@ removeWords(words: string[]): void; |
@@ -14,8 +14,8 @@ "use strict"; | ||
loadFile(filename) { | ||
const file = fs_1.readFileSync(filename, 'utf8'); | ||
this.words = file.split('\n').filter(x => x); | ||
const file = fs_1.readFileSync(filename, "utf8"); | ||
this.words = file.split("\n").filter((x) => x); | ||
this.onListChanged(); | ||
} | ||
removeWords(words) { | ||
this.words = this.words.filter(x => !words.includes(x)); | ||
this.words = this.words.filter((x) => !words.includes(x)); | ||
this.onListChanged(); | ||
@@ -22,0 +22,0 @@ } |
@@ -7,3 +7,3 @@ "use strict"; | ||
this.wholeWord = true; | ||
this.grawlix = '@#$%&!'; | ||
this.grawlix = "@#$%&!"; | ||
} | ||
@@ -10,0 +10,0 @@ } |
@@ -1,3 +0,3 @@ | ||
import { ProfanityOptions } from './profanity-options'; | ||
import { List } from './list'; | ||
import { ProfanityOptions } from "./profanity-options"; | ||
import { List } from "./list"; | ||
export declare class Profanity { | ||
@@ -4,0 +4,0 @@ options: ProfanityOptions; |
@@ -12,3 +12,3 @@ "use strict"; | ||
this.blacklist = new list_1.List(() => this.buildRegex()); | ||
this.blacklist.loadFile(path_1.resolve(__dirname, 'words.txt')); | ||
this.blacklist.loadFile(path_1.resolve(__dirname, "words.txt")); | ||
} | ||
@@ -29,5 +29,5 @@ exists(text) { | ||
buildRegex() { | ||
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'); | ||
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"); | ||
} | ||
@@ -34,0 +34,0 @@ } |
@@ -5,14 +5,14 @@ "use strict"; | ||
const _1 = require("."); | ||
describe('Profanity', () => { | ||
describe('exists (wholeWord = true)', () => { | ||
it('should return true when profanity exists in a sentence', () => { | ||
chai_1.expect(_1.profanity.exists('I like big butts and I cannot lie')).to.equal(true); | ||
describe("Profanity", () => { | ||
describe("exists (wholeWord = true)", () => { | ||
it("should return true when profanity exists in a sentence", () => { | ||
chai_1.expect(_1.profanity.exists("I like big butts and I cannot lie")).to.equal(true); | ||
}); | ||
it('should return true when profanity exists as a single word', () => { | ||
chai_1.expect(_1.profanity.exists('butt')).to.equal(true); | ||
it("should return true when profanity exists as a single word", () => { | ||
chai_1.expect(_1.profanity.exists("butt")).to.equal(true); | ||
}); | ||
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 arsenic?')).to.equal(false); | ||
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 arsenic?")).to.equal(false); | ||
}); | ||
it('should return true when profanity exists within multiple lines', () => { | ||
it("should return true when profanity exists within multiple lines", () => { | ||
chai_1.expect(_1.profanity.exists(` | ||
@@ -24,14 +24,14 @@ Nothing profane on line 1. | ||
}); | ||
it('should return false when profanity does not exist', () => { | ||
chai_1.expect(_1.profanity.exists('I like big glutes and I cannot lie')).to.equal(false); | ||
it("should return false when profanity does not exist", () => { | ||
chai_1.expect(_1.profanity.exists("I like big glutes and I cannot lie")).to.equal(false); | ||
}); | ||
}); | ||
describe('exists (wholeWord = false)', () => { | ||
describe("exists (wholeWord = false)", () => { | ||
const options = new _1.ProfanityOptions(); | ||
options.wholeWord = false; | ||
const customProfanity = new _1.Profanity(options); | ||
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 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 part of a word, within multiple lines', () => { | ||
it("should return true when profanity is part of a word, within multiple lines", () => { | ||
chai_1.expect(customProfanity.exists(` | ||
@@ -43,19 +43,19 @@ Nothing profane on line 1. | ||
}); | ||
it('should return false when profanity does not exist', () => { | ||
chai_1.expect(customProfanity.exists('I like big glutes and I cannot lie')).to.equal(false); | ||
it("should return false when profanity does not exist", () => { | ||
chai_1.expect(customProfanity.exists("I like big glutes and I cannot lie")).to.equal(false); | ||
}); | ||
it('should return true when profanity exists as part of a single word', () => { | ||
chai_1.expect(customProfanity.exists('arsenic')).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); | ||
}); | ||
}); | ||
describe('censor', () => { | ||
it('should replace profanity with grawlix in a sentence', () => { | ||
const censored = _1.profanity.censor('I like big butts and I cannot lie'); | ||
describe("censor", () => { | ||
it("should replace profanity with grawlix in a sentence", () => { | ||
const censored = _1.profanity.censor("I like big butts and I cannot lie"); | ||
chai_1.expect(censored.includes(_1.profanity.options.grawlix)).to.equal(true); | ||
}); | ||
it('should remove profanity from a sentence', () => { | ||
const censored = _1.profanity.censor('I like big butts (aka arses) and I cannot lie'); | ||
it("should remove profanity from a sentence", () => { | ||
const censored = _1.profanity.censor("I like big butts (aka arses) and I cannot lie"); | ||
chai_1.expect(_1.profanity.exists(censored)).to.equal(false); | ||
}); | ||
it('should remove profanity from multiple lines', () => { | ||
it("should remove profanity from multiple lines", () => { | ||
const censored = _1.profanity.censor(` | ||
@@ -68,76 +68,76 @@ Nothing profane on line 1. | ||
}); | ||
it('should not alter sentence without profanity', () => { | ||
const original = 'I like big glutes and I cannot lie'; | ||
it("should not alter sentence without profanity", () => { | ||
const original = "I like big glutes and I cannot lie"; | ||
const censored = _1.profanity.censor(original); | ||
chai_1.expect(censored).to.equal(original); | ||
}); | ||
it('should remove profanity when profanity exists as a single word', () => { | ||
const censored = _1.profanity.censor('butt'); | ||
it("should remove profanity when profanity exists as a single word", () => { | ||
const censored = _1.profanity.censor("butt"); | ||
chai_1.expect(_1.profanity.exists(censored)).to.equal(false); | ||
}); | ||
}); | ||
describe('addWords', () => { | ||
it('should add a single word to the list of profane words', () => { | ||
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); | ||
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', () => { | ||
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); | ||
customProfanity.addWords(["aardvark", "zebra"]); | ||
chai_1.expect(customProfanity.exists("Should we censor the word aardvark and zebra?")).to.equal(true); | ||
}); | ||
}); | ||
describe('removeWords', () => { | ||
it('should remove a single word from the list of profane words', () => { | ||
describe("removeWords", () => { | ||
it("should remove a single word from the list of profane words", () => { | ||
const customProfanity = new _1.Profanity(); | ||
customProfanity.removeWords(['butts']); | ||
chai_1.expect(customProfanity.exists('I like big butts and I cannot lie')).to.equal(false); | ||
customProfanity.removeWords(["butts"]); | ||
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', () => { | ||
it("should remove mulitple words from the list of profane words", () => { | ||
const customProfanity = new _1.Profanity(); | ||
customProfanity.removeWords(['butts', 'arses']); | ||
chai_1.expect(customProfanity.exists('I like big butts (aka arses) and I cannot lie')).to.equal(false); | ||
customProfanity.removeWords(["butts", "arses"]); | ||
chai_1.expect(customProfanity.exists("I like big butts (aka arses) and I cannot lie")).to.equal(false); | ||
}); | ||
}); | ||
describe('Whitelist (wholeWord = true)', () => { | ||
it('should whitelist a single word', () => { | ||
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); | ||
customProfanity.whitelist.addWords(["butt"]); | ||
chai_1.expect(customProfanity.exists("Should we censor the word butt?")).to.equal(false); | ||
}); | ||
it('should whitelist multiple words', () => { | ||
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); | ||
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)', () => { | ||
describe("Whitelist (wholeWord = false)", () => { | ||
const options = new _1.ProfanityOptions(); | ||
options.wholeWord = false; | ||
it('should whitelist a single word', () => { | ||
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); | ||
customProfanity.whitelist.addWords(["buttocks"]); | ||
chai_1.expect(customProfanity.exists("Should we censor the word buttocks?")).to.equal(false); | ||
}); | ||
it('should whitelist multiple words', () => { | ||
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); | ||
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', () => { | ||
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); | ||
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', () => { | ||
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); | ||
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); | ||
}); | ||
@@ -144,0 +144,0 @@ }); |
{ | ||
"name": "@2toad/profanity", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "A JavaScript profanity filter", | ||
@@ -9,3 +9,3 @@ "homepage": "https://github.com/2Toad/Profanity", | ||
"engines": { | ||
"node": "14.15.0" | ||
"node": ">=10.23.0" | ||
}, | ||
@@ -16,3 +16,4 @@ "main": "dist/index.js", | ||
"dev": "tsc -w", | ||
"lint": "tslint -c tslint.json -p tsconfig.json", | ||
"lint": "npx eslint src", | ||
"prettier": "npx prettier --write .", | ||
"test": "mocha -r ts-node/register src/**/*.spec.ts", | ||
@@ -46,10 +47,26 @@ "prepare": "npm run build", | ||
"@types/chai": "^4.2.14", | ||
"@types/mocha": "^8.0.4", | ||
"@types/node": "^14.14.7", | ||
"@types/mocha": "^8.2.0", | ||
"@types/node": "^14.14.14", | ||
"@typescript-eslint/eslint-plugin": "^4.10.0", | ||
"@typescript-eslint/parser": "^4.10.0", | ||
"chai": "^4.2.0", | ||
"eslint": "^7.15.0", | ||
"husky": "^4.3.6", | ||
"lint-staged": "^10.5.3", | ||
"mocha": "^8.2.1", | ||
"ts-node": "^9.0.0", | ||
"tslint": "^6.1.3", | ||
"typescript": "^4.0.5" | ||
"prettier": "2.2.1", | ||
"ts-node": "^9.1.1", | ||
"typescript": "^4.1.3" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
} | ||
}, | ||
"lint-staged": { | ||
"*.ts": [ | ||
"eslint --cache --fix", | ||
"prettier --write" | ||
] | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
38
42968
13
481