You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

word-sensor

Package Overview
Dependencies
Maintainers
0
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

word-sensor - npm Package Compare versions

Comparing version

to
1.0.2

42

package.json
{
"name": "word-sensor",
"version": "1.0.1",
"version": "1.0.2",
"description": "A simple word filtering library for JavaScript/TypeScript",
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc"
"build": "tsc",
"prepublishOnly": "npm run build",
"test": "jest"
},
"keywords": ["word-filter", "bad-words", "profanity"],
"keywords": [
"word-filter",
"bad-words",
"profanity"
],
"author": "Asrul Harahap",
"license": "ISC",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/asruldev/word-sensor.git"
},
"homepage": "https://github.com/asruldev/word-sensor",
"files": [
"dist/"
],
"exports": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs",
"default": "./dist/index.js"
},
"typesVersions": {
"*": {
"index.d.ts": [
"dist/index.d.ts"
]
}
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "node"
},
"devDependencies": {
"@types/jest": "^29.5.14",
"jest": "^29.7.0",
"ts-jest": "^29.2.6",
"typescript": "^5.7.3"
}
}

128

README.md
# WordSensor
## Description
`WordSensor` is a TypeScript utility that allows you to censor specific words in a text with options to replace them with a chosen character or remove them entirely.
🚀 **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
- ✅ **Supports censoring based on a forbidden words list**.
- ✅ **Can replace words with specific symbols (e.g., `*`, `#`, etc.)**.
- ✅ **Can remove forbidden words from text**.
- ✅ **Supports case-insensitive word matching**.
- ✅ **Allows custom replacement characters per word**.
- ✅ **Uses regular expressions for more accurate word matching**.
## ✨ Features
## Installation
Since this is a TypeScript utility, you can use it directly in your TypeScript project.
- 🔍 **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.
- ✅ Fully tested with Jest.
## 📦 Installation
```sh

@@ -21,70 +20,83 @@ npm install word-sensor

Or, simply copy the `WordSensor` class code into your project.
or
## Usage
```sh
yarn add word-sensor
```
```typescript
import { WordSensor } from "./WordSensor";
## 🚀 Usage
const sensor = new WordSensor(["badword", "rude"], "*");
### Import and Initialize
console.log(sensor.filter("This is a badword and it's rude!"));
// Output: This is a ******* and it's ****!
```ts
import { WordSensor } from "word-sensor";
sensor.addWord("test", "####");
console.log(sensor.filter("This is a test."));
// Output: This is a ####.
const sensor = new WordSensor(["badword", "offensive"], "*", true, true);
```
## API
### 🔹 Replacing Forbidden Words
### `new WordSensor(words: string[], maskChar: string = "*", caseInsensitive: boolean = true)`
Creates an instance of `WordSensor` with a list of words to be censored.
```ts
const result = sensor.filter("This is a badword test.");
console.log(result); // "This is a ******* test."
```
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `words` | `string[]` | `[]` | List of forbidden words. |
| `maskChar` | `string` | `*` | Character used for censoring. |
| `caseInsensitive` | `boolean` | `true` | Whether word matching is case-insensitive. |
### 🔹 Custom Masking
### `addWord(word: string, mask?: string): void`
Adds a new word to the censor list. If `mask` is provided, the word will be censored with that character.
```ts
sensor.addWord("rude", "###");
const result = sensor.filter("You are rude!");
console.log(result); // "You are ###!"
```
### `addWords(words: string[]): void`
Adds multiple words to the censor list.
### 🔹 Removing Forbidden Words
### `removeWord(word: string): void`
Removes a word from the censor list.
```ts
const result = sensor.filter("This is an offensive statement.", "remove");
console.log(result); // "This is an statement."
```
### `removeWords(words: string[]): void`
Removes multiple words from the censor list.
### 🔹 Detecting Forbidden Words
### `filter(text: string, mode: "replace" | "remove" = "replace"): string`
Filters text based on the forbidden words list.
```ts
const detectedWords = sensor.detect("This contains badword and offensive content.");
console.log(detectedWords); // ["badword", "offensive"]
```
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `text` | `string` | - | The text to be filtered. |
| `mode` | `"replace" | "remove"` | `"replace"` | Filtering mode: `replace` substitutes words with censor symbols, `remove` deletes words from the text. |
### 🔹 Partial Masking
## Advanced Examples
```typescript
const sensor = new WordSensor(["example", "test"], "*");
```ts
const result = sensor.filter("This is a badword test.", "replace", "partial");
console.log(result); // "This is a b*****d test."
```
console.log(sensor.filter("This is an example of a test."));
// Output: This is an ******* of a ****.
### 🔹 Adding Multiple Words
sensor.addWord("extra", "@#$");
console.log(sensor.filter("This is an extra example."));
// Output: This is an @#$ *******.
```ts
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("test");
console.log(sensor.filter("This is a test."));
// Output: This is a test.
### 🔹 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)
```
## License
This project is released under the MIT license.
### 🔹 Logging Detected Words
## Author
[Asrul Harahap](https://github.com/asruldev)
```ts
sensor.filter("badword here.");
console.log(sensor.getDetectionLogs()); // ["badword"]
```
## 📜 License
This project is licensed under the **MIT License**.
## 👨‍💻 Author
Developed by [Asrul Harahap](https://github.com/asruldev). Contributions and feedback are welcome! 😊