node-profanity-filter
Version:
A Node.js profanity filter with support for exact matching, obfuscated word detection, and customizable replacement.
74 lines • 1.76 kB
JavaScript
export class TrieNode {
children = {};
isWord = false;
}
export class Trie {
root = new TrieNode();
constructor(words = []) {
words.forEach(w => this.insert(w));
}
/**
* Insert a new word
* @param word
*/
insert(word) {
let node = this.root;
for (const char of word) {
node = node.children[char] ||= new TrieNode();
}
node.isWord = true;
}
/**
* Whether the exact word is matched
* @param word
* @returns
*/
contains(word) {
let node = this.root;
for (const char of word) {
node = node.children[char];
if (!node)
return false;
}
return node.isWord;
}
/**
* Whether the text includes any matches
* @param text
* @returns
*/
containsIn(text) {
for (let i = 0; i < text.length; i++) {
let node = this.root;
for (let j = i; j < text.length; j++) {
const char = text[j];
node = node.children[char];
if (!node)
break;
if (node.isWord)
return true;
}
}
return false;
}
/**
* Match the length
* @param text
* @param start
* @returns
*/
matchLengthAt(text, start) {
let node = this.root;
let maxLen = 0;
for (let i = start; i < text.length; i++) {
const char = text[i];
node = node.children[char];
if (!node)
break;
if (node.isWord)
maxLen = i - start + 1;
}
return maxLen;
}
}
//# sourceMappingURL=trie.js.map