UNPKG

@2toad/profanity

Version:

A JavaScript profanity filter

36 lines 1.29 kB
"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"); 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').filter(x => x); this.buildRegex(); } exists(text) { this.regex.lastIndex = 0; return this.regex.test(text); } censor(text) { return text.replace(this.regex, this.options.grawlix); } removeWords(words) { this.words = this.words.filter(x => !words.includes(x)); this.buildRegex(); } 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'); } } exports.Profanity = Profanity; exports.profanity = new Profanity(); exports.default = exports.profanity; //# sourceMappingURL=investigator.js.map