🚀 WordSensor is a simple and lightweight word filtering library for JavaScript/TypeScript. It helps you detect, replace, or remove forbidden words from text effortlessly.
- 🔍 Detect prohibited words in text.
- 🚫 Replace forbidden words with a mask (full or partial masking).
- 🗑️ Remove forbidden words from text.
- 📜 Customizable word list and mask characters.
- 📝 Logging feature to track detected words.
- 📥 Async Word Loading from external APIs.
- 🔡 Case-Insensitive Matching for better detection.
- ✅ Fully tested with Jest.
npm install word-sensor
or
yarn add word-sensor
import { WordSensor } from "word-sensor";
const sensor = new WordSensor(["badword", "offensive"], "*", true, true);
const result = sensor.filter("This is a badword test.");
console.log(result); // "This is a ******* test."
sensor.addWord("rude", "###");
const result = sensor.filter("You are rude!");
console.log(result); // "You are ###!"
const result = sensor.filter("This is an offensive statement.", "remove");
console.log(result); // "This is an statement."
const detectedWords = sensor.detect("This contains badword and offensive content.");
console.log(detectedWords); // ["badword", "offensive"]
const result = sensor.filter("This is a badword test.", "replace", "partial");
console.log(result); // "This is a b*****d test."
sensor.addWords(["newword", "another"]);
const result = sensor.filter("This is a newword and another example.");
console.log(result); // "This is a ******* and ******* example."
sensor.removeWord("badword");
const result = sensor.filter("This is a badword test.");
console.log(result); // "This is a badword test." (No longer filtered)
sensor.filter("badword here.");
console.log(sensor.getDetectionLogs()); // ["badword"]
Load forbidden words from an external API.
await sensor.loadWordsFromAPI("https://api.example.com/forbidden-words");
const result = sensor.filter("This contains a forbidden word.");
console.log(result); // "This contains a ******** word."
Enable case-insensitive word matching.
sensor.setCaseInsensitive(true);
const result = sensor.filter("This contains BADWORD and badword.");
console.log(result); // "This contains ******* and *******.
This project is licensed under the MIT License.
Developed by Asrul Harahap. Contributions and feedback are welcome! 😊