@andzh777/swear-filter
Version:
Bad word filter.
137 lines (136 loc) • 5.58 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Filter = exports.Method = exports.Lang = void 0;
const ru_json_1 = __importDefault(require("../data/ru.json"));
const en_json_1 = __importDefault(require("../data/en.json"));
const lv_json_1 = __importDefault(require("../data/lv.json"));
var Lang;
(function (Lang) {
Lang["ENGLISH"] = "en";
Lang["RUSSIAN"] = "ru";
Lang["LATVIAN"] = "lv";
})(Lang = exports.Lang || (exports.Lang = {}));
var Method;
(function (Method) {
Method[Method["CLASSIC"] = 0] = "CLASSIC";
Method[Method["STRICT"] = 1] = "STRICT";
})(Method = exports.Method || (exports.Method = {}));
class Filter {
defaultOpts = {
langs: [Lang.ENGLISH],
replacer: '█',
method: Method.CLASSIC,
};
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
methods = new Map();
constructor() {
this.methods
.set(Method.CLASSIC, (word, langs) => {
if (langs.includes(Lang.RUSSIAN) &&
!ru_json_1.default.exclude.includes(word) &&
ru_json_1.default.include.filter((w) => word === w || word.includes(w)).length != 0) {
return [Lang.RUSSIAN, true];
}
if (langs.includes(Lang.ENGLISH) &&
!en_json_1.default.exclude.includes(word) &&
en_json_1.default.include.filter((w) => word === w || word.includes(w)).length != 0) {
return [Lang.ENGLISH, true];
}
if (langs.includes(Lang.LATVIAN) &&
!lv_json_1.default.exclude.includes(word) &&
lv_json_1.default.include.filter((w) => word === w || word.includes(w)).length != 0) {
return [Lang.LATVIAN, true];
}
return ['', false];
})
.set(Method.STRICT, (word, langs) => {
const results = [];
if (langs.includes(Lang.RUSSIAN) && !ru_json_1.default.exclude.includes(word)) {
if (ru_json_1.default.include.filter((w) => word === w || word.includes(w)).length != 0) {
return [Lang.RUSSIAN, true];
}
results.push(this.found(word, ru_json_1.default));
}
if (langs.includes(Lang.ENGLISH) && !en_json_1.default.exclude.includes(word)) {
if (en_json_1.default.include.filter((w) => word === w || word.includes(w)).length != 0) {
return [Lang.ENGLISH, true];
}
results.push(this.found(word, en_json_1.default));
}
if (langs.includes(Lang.LATVIAN) && !lv_json_1.default.exclude.includes(word)) {
if (lv_json_1.default.include.filter((w) => word === w || word.includes(w)).length != 0) {
return [Lang.LATVIAN, true];
}
results.push(this.found(word, lv_json_1.default));
}
return ['', results.includes(true)];
});
}
/**
* @param { string } string Query string to filter
* @param { AutomodOptions } options Filter options { langs: Lang[], replacer: "█", method: Method }
* @returns { AutomodReply } Output data { clean: boolean, output: string, ... }
*/
filter(string = '', options = this.defaultOpts) {
const { langs, replacer, method } = { ...this.defaultOpts, ...options };
let reply = {
time: Date.now(),
input: string,
output: string,
matches: [],
involvedLanguages: [],
clean: true,
};
reply = this.clean(reply, { langs, replacer, method });
reply.time = Date.now() - reply.time;
return reply;
}
clean(reply, options) {
const isProfane = this.methods.get(options.method);
for (const word of reply.output.split(/ /)) {
const [language, result] = isProfane
? isProfane(word, options.langs)
: ['', false];
if (result) {
reply.matches.push(word);
reply.clean = false;
reply.output = reply.output.replace(word, options.replacer.repeat(word.length));
if (language && !reply.involvedLanguages.includes(language)) {
reply.involvedLanguages.push(language);
}
}
}
return reply;
}
found(string, lang, index = 0) {
const replacers = lang.replacers[string.charAt(index)];
if (replacers) {
const results = [];
for (let i = 0; i < replacers.length; i++) {
const newString = string.replace(string.charAt(index), replacers[i]);
if (lang.include.filter((w) => newString === w || newString.includes(w)).length != 0) {
return true;
}
if (index + 1 == string.length) {
return false;
}
else {
results.push(this.found(newString, lang, index + 1));
}
}
return results.includes(true);
}
else {
if (index + 1 == string.length) {
return false;
}
else {
return this.found(string, lang, index + 1);
}
}
}
}
exports.Filter = Filter;