UNPKG

deep-profanity-filter

Version:

A thorough profanity filter that considers most common circumventions. Works with your custom list of blocked and whitelisted words and phrases. Identifies and/or replaces bad words. Works with *wildcards* at *start and/or end* of words.

75 lines 3.01 kB
"use strict"; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.replaceChars = exports.grawlix = void 0; var grawlixSymbols = '%&$#?£@!'; /** * Transforms the full input string into so-called 'grawlix' (`%&$#?£@!`) characters. * @param inputString - The text we want to be turned into grawlix characters. * @returns The text, transformed into a mess of `%&$#?£@!` characters. * Simple whitespace will be preserved. */ var grawlix = function (inputString) { var outString = ''; var lastCharacter = ''; for (var i = 0; i < inputString.length; i++) { if (inputString.charAt(i).match(/[^a-zA-Z0-9]/g)) { outString += inputString.charAt(i); } else { var symbolIndex = inputString.charCodeAt(i) % grawlixSymbols.length; if (lastCharacter === grawlixSymbols[symbolIndex]) { symbolIndex = (symbolIndex + 1) % grawlixSymbols.length; } outString += grawlixSymbols[symbolIndex]; lastCharacter = grawlixSymbols[symbolIndex]; } } return outString; }; exports.grawlix = grawlix; /** * Replaces the full input strings with a repetition of the replacement character, but preserves * simple whitespace. * @param inputString - The text that we want to replace with the replacement character. * @param replacementCharacter - The character that we want to replace it with. If a string of * `length > 1` is provided, it will only use the first character. * @returns The text, transformed into a repetition of the replacement character, but with * simple whitespaces being preserved. */ var replaceChars = function (inputString, replacementCharacter) { var e_1, _a; var replaceChar = replacementCharacter[0]; var outString = ''; try { for (var inputString_1 = __values(inputString), inputString_1_1 = inputString_1.next(); !inputString_1_1.done; inputString_1_1 = inputString_1.next()) { var char = inputString_1_1.value; if (char.match(/[^a-zA-Z0-9]/g)) { outString += char; } else { outString += replaceChar; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (inputString_1_1 && !inputString_1_1.done && (_a = inputString_1.return)) _a.call(inputString_1); } finally { if (e_1) throw e_1.error; } } return outString; }; exports.replaceChars = replaceChars; //# sourceMappingURL=replace_input.js.map