UNPKG

word-sensor

Version:

A simple word filtering library for JavaScript/TypeScript

124 lines (86 loc) • 2.99 kB
# 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 ```sh npm install word-sensor ``` or ```sh yarn add word-sensor ``` ## šŸš€ Usage ### Import and Initialize ```ts import { WordSensor } from "word-sensor"; const sensor = new WordSensor(["badword", "offensive"], "*", true, true); ``` ### šŸ”¹ Replacing Forbidden Words ```ts const result = sensor.filter("This is a badword test."); console.log(result); // "This is a ******* test." ``` ### šŸ”¹ Custom Masking ```ts sensor.addWord("rude", "###"); const result = sensor.filter("You are rude!"); console.log(result); // "You are ###!" ``` ### šŸ”¹ Removing Forbidden Words ```ts const result = sensor.filter("This is an offensive statement.", "remove"); console.log(result); // "This is an statement." ``` ### šŸ”¹ Detecting Forbidden Words ```ts const detectedWords = sensor.detect("This contains badword and offensive content."); console.log(detectedWords); // ["badword", "offensive"] ``` ### šŸ”¹ Partial Masking ```ts const result = sensor.filter("This is a badword test.", "replace", "partial"); console.log(result); // "This is a b*****d test." ``` ### šŸ”¹ Adding Multiple Words ```ts 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 ```ts 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 ```ts sensor.filter("badword here."); console.log(sensor.getDetectionLogs()); // ["badword"] ``` ### šŸ”¹ Async Word Loading Load forbidden words from an external API. ```ts 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. ```ts 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](https://github.com/asruldev). Contributions and feedback are welcome! 😊