A TypeScript package to filter and censor bad words with support for character mapping and custom word lists. Supports both Bahasa Indonesia and English bad words detection.
- π« Filter and detect bad words in text
- β Censorship mode to replace bad words with asterisks
- π Character mapping to catch obfuscated words (e.g., "@" β "a", "4" β "a")
- π‘οΈ Whitelisting to prevent false positives
- β Ability to add custom bad words
- πΊοΈ Extensible character mapping
- π Written in TypeScript with full type support
- π Supports both Bahasa Indonesia and English
npm install bad-word-filter
# or
yarn add bad-word-filterimport { createBadWordFilter } from 'bad-word-filter';
// Create a filter instance with default configuration
const filter = createBadWordFilter();
// Check for bad words (returns error message if found)
const result1 = filter.filterText("This is a bad text");
console.log(result1);
// { status: false, result: "Ban word detected!" }
// Censor bad words (replaces them with asterisks)
const result2 = filter.filterText("This is a bad text", true);
console.log(result2);
// { status: true, result: "This is a *** text" }You can create a filter with custom configuration:
const filter = createBadWordFilter({
banWords: ['custom', 'bad', 'words'],
characterMap: {
'@': 'a',
'4': 'a',
'$': 's',
'0': 'o'
}
});const filter = createBadWordFilter();
// Add more words to the ban list
filter.addBanWords(['word1', 'word2']);const filter = createBadWordFilter();
// Add more character mappings
filter.addCharacterMap({
'!': 'i',
'3': 'e'
});To prevent the filter from flagging legitimate words that may contain a banned word (e.g., "class" containing "ass"), you can use a whitelist.
const filter = createBadWordFilter({
banWords: ['ass'],
whitelist: ['class', 'assignment'] // These words will not be censored
});
// "class" and "assignment" will not be censored.
const result = filter.filterText("The class assignment is important.", true);
console.log(result);
// { status: true, result: "The class assignment is important." }
// You can also add words to the whitelist dynamically.
filter.addWhitelistWords(['classic']);The filter automatically handles obfuscated text using character mapping:
const filter = createBadWordFilter();
// Will detect "b@d" as "bad"
const result = filter.filterText("This is b@d");
console.log(result);
// { status: false, result: "Ban word detected!" }
// With censorship enabled
const censored = filter.filterText("This is b@d", true);
console.log(censored);
// { status: true, result: "This is ***" }Creates a new instance of the bad word filter.
config(optional): Configuration objectbanWords: Array of strings to be bannedcharacterMap: Object mapping special characters to their letter equivalentswhitelist: Array of strings to be exempt from filtering
An object with the following methods:
Filters text for bad words.
str: The input string to checkcensor(optional): If true, replaces bad words with asterisks. If false, returns error message when bad word is detected
{
status: boolean; // false if bad word detected (when censor=false), true if word found and censored (when censor=true)
result: string; // censored string or error message
}Adds new words to the ban list.
words: Array of strings to add to the ban list
Adds new words to the whitelist.
words: Array of strings to add to the whitelist
Adds new character mappings.
mappings: Object with character mappings
This project is licensed under GPLv2.