word-sensor
Version:
A simple word filtering library for JavaScript/TypeScript
124 lines (86 loc) ⢠2.99 kB
Markdown
š **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.
## š¦ 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);
```
```ts
const result = sensor.filter("This is a badword test.");
console.log(result); // "This is a ******* test."
```
```ts
sensor.addWord("rude", "###");
const result = sensor.filter("You are rude!");
console.log(result); // "You are ###!"
```
```ts
const result = sensor.filter("This is an offensive statement.", "remove");
console.log(result); // "This is an statement."
```
```ts
const detectedWords = sensor.detect("This contains badword and offensive content.");
console.log(detectedWords); // ["badword", "offensive"]
```
```ts
const result = sensor.filter("This is a badword test.", "replace", "partial");
console.log(result); // "This is a b*****d test."
```
```ts
sensor.addWords(["newword", "another"]);
const result = sensor.filter("This is a newword and another example.");
console.log(result); // "This is a ******* and ******* example."
```
```ts
sensor.removeWord("badword");
const result = sensor.filter("This is a badword test.");
console.log(result); // "This is a badword test." (No longer filtered)
```
```ts
sensor.filter("badword here.");
console.log(sensor.getDetectionLogs()); // ["badword"]
```
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."
```
Enable case-insensitive word matching.
```ts
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](https://github.com/asruldev). Contributions and feedback are welcome! š