word-sensor
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

WordSensor

🚀 WordSensor is a simple and lightweight word filtering library for JavaScript/TypeScript. It helps you detect, replace, or remove forbidden words from text effortlessly.

✨ Features

  • 🔍 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.

📦 Installation

npm install word-sensor

or

yarn add word-sensor

🚀 Usage

Import and Initialize

import { WordSensor } from "word-sensor";

const sensor = new WordSensor(["badword", "offensive"], "*", true, true);

🔹 Replacing Forbidden Words

const result = sensor.filter("This is a badword test.");
console.log(result); // "This is a ******* test."

🔹 Custom Masking

sensor.addWord("rude", "###");
const result = sensor.filter("You are rude!");
console.log(result); // "You are ###!"

🔹 Removing Forbidden Words

const result = sensor.filter("This is an offensive statement.", "remove");
console.log(result); // "This is an  statement."

🔹 Detecting Forbidden Words

const detectedWords = sensor.detect("This contains badword and offensive content.");
console.log(detectedWords); // ["badword", "offensive"]

🔹 Partial Masking

const result = sensor.filter("This is a badword test.", "replace", "partial");
console.log(result); // "This is a b*****d test."

🔹 Adding Multiple Words

sensor.addWords(["newword", "another"]);
const result = sensor.filter("This is a newword and another example.");
console.log(result); // "This is a ******* and ******* example."

🔹 Removing Words

sensor.removeWord("badword");
const result = sensor.filter("This is a badword test.");
console.log(result); // "This is a badword test." (No longer filtered)

🔹 Logging Detected Words

sensor.filter("badword here.");
console.log(sensor.getDetectionLogs()); // ["badword"]

🔹 Async Word Loading

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."

🔹 Case-Insensitive Matching

Enable case-insensitive word matching.

sensor.setCaseInsensitive(true);
const result = sensor.filter("This contains BADWORD and badword.");
console.log(result); // "This contains ******* and *******.

📜 License

This project is licensed under the MIT License.

👨‍💻 Author

Developed by Asrul Harahap. Contributions and feedback are welcome! 😊

Package Sidebar

Install

npm i word-sensor

Weekly Downloads

12

Version

1.0.5

License

MIT

Unpacked Size

25.8 kB

Total Files

8

Last publish

Collaborators

  • asruldev