🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

node-sensitive-word-filtering

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-sensitive-word-filtering - npm Package Compare versions

Comparing version
1.0.4
to
1.0.5
+21
LICENSE
MIT License
Copyright (c) 2024 alisir
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+1
-1
{
"name": "node-sensitive-word-filtering",
"version": "1.0.4",
"version": "1.0.5",
"description": "A Node.js library for filtering sensitive words using a prefix tree (Trie).",

@@ -5,0 +5,0 @@ "main": "sensitive-word-trie.js",

+184
-9
# node-sensitive-word-filtering
基于 node 实现的敏感词过滤脚本(前缀树)
# 示例使用
[中文](#中文) | [English](#English)
---
## 中文
基于 Node.js 实现的敏感词过滤脚本,使用前缀树(Trie)结构,支持高效匹配、大小写忽略、空格忽略功能。
### 特性
- 🌟 高效的敏感词过滤(基于前缀树)。
- 🌟 支持大小写忽略匹配。
- 🌟 支持过滤时忽略空格。
- 🌟 支持动态添加、删除敏感词。
### 安装
使用 NPM 安装:
```bash
npm install node-sensitive-word-filtering
```
### 示例使用
#### JavaScript 示例
```js
const Trie = require('node-sensitive-word-filtering');
const trie = new Trie();
// 添加敏感词
trie.insert('敏感词1');
trie.insert('敏感词12');
const inputText = '这里包含敏感词1和其他内容。';
const filteredText = trie.filter(inputText);
// 过滤文本
const inputText = '敏 感 词1 和其他内容。';
const filteredText = trie.filter({
text: inputText,
ignoreCase: true, // 忽略大小写
ignoreSpaces: true, // 忽略空格
});
console.log(filteredText); // 输出: **** 和其他内容
```
console.log(filteredText); // 输出: 这里包含****和其他内容。
#### TypeScript 示例
const inputText2 = '灵敏';
const filteredText2 = trie.filter(inputText2);
console.log(inputText2); // 输出: 灵敏
```ts
import Trie from 'node-sensitive-word-filtering';
```
const trie = new Trie();
// 插入敏感词
trie.insert('SensitiveWord');
trie.insert('BadWord');
// 测试文本
const text = 'This text contains SensitiveWord and other stuff.';
const result = trie.filter({ text, ignoreCase: true });
console.log(result); // 输出: This text contains ************ and other stuff.
```
### 方法说明
**insert(word: string): void**
- 功能: 插入敏感词。
- 参数: word - 要插入的敏感词。
**contains(word: string): boolean**
- 功能: 检查敏感词是否存在。
- 参数: word - 要检查的敏感词。
- 返回值: 如果敏感词存在,返回 true,否则返回 false。
**delete(word: string): void**
- 功能: 删除敏感词。
- 参数: word - 要删除的敏感词。
**filter(options: { text: string; ignoreCase?: boolean; ignoreSpaces?: boolean }): string**
- 功能: 过滤文本中的敏感词。
- 参数:
- text: 要过滤的文本。
- ignoreCase: 是否忽略大小写(默认为 false)。
- ignoreSpaces: 是否忽略空格(默认为 false)。
- 返回值: 过滤后的文本。
## English
A sensitive word filtering script implemented in Node.js, using a prefix tree (Trie) structure. Supports efficient matching, case-insensitivity, and space-insensitivity.
### Features
🌟 Efficient sensitive word filtering (Trie-based).
🌟 Supports case-insensitive matching.
🌟 Supports ignoring spaces during filtering.
🌟 Dynamically add and remove sensitive words.
### Installation
Install via NPM:
```bash
npm install node-sensitive-word-filtering
```
### Example Usage
#### JavaScript Example
```js
const Trie = require('node-sensitive-word-filtering');
const trie = new Trie();
// Add sensitive words
trie.insert('SensitiveWord1');
trie.insert('SensitiveWord12');
// Filter text
const inputText = 'This contains SensitiveWord1 and some other text.';
const filteredText = trie.filter({
text: inputText,
ignoreCase: true, // Ignore case
ignoreSpaces: true, // Ignore spaces
});
console.log(filteredText); // Output: This contains **** and some other text.
```
#### TypeScript Example
```ts
import Trie from 'node-sensitive-word-filtering';
const trie = new Trie();
// Insert sensitive words
trie.insert('SensitiveWord');
trie.insert('BadWord');
// Test text
const text = 'This text contains SensitiveWord and other stuff.';
const result = trie.filter({ text, ignoreCase: true });
console.log(result); // Output: This text contains ************ and other stuff.
```
### Method Descriptions
**insert(word: string): void**
- Function: Insert a sensitive word.
- Parameters: word - The sensitive word to insert.
**contains(word: string): boolean**
- Function: Check if a sensitive word exists.
- Parameters: word - The sensitive word to check.
- Returns: true if the word exists, otherwise false.
**delete(word: string): void**
- Function: Delete a sensitive word.
- Parameters: word - The sensitive word to delete.
**filter(options: { text: string; ignoreCase?: boolean; ignoreSpaces?: boolean }): string**
- Function: Filter sensitive words in the text.
- Parameters:
- text: The text to filter.
- ignoreCase: Whether to ignore case (default false).
- ignoreSpaces: Whether to ignore spaces (default false).
- Returns: The filtered text.
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

@@ -38,9 +38,5 @@ class TrieNode {

if (!node) return false;
// 如果已经达到单词的末尾
if (depth === word.length) {
// 仅在节点确实是单词结尾时设置 isEndOfWord 为 false
if (node.isEndOfWord) {
node.isEndOfWord = false;
// 如果没有任何子节点,则可以删除此节点
return Object.keys(node.children).length === 0;

@@ -50,7 +46,4 @@ }

}
const char = word[depth];
const childNode = node.children[char];
// 递归删除子节点
const shouldDeleteChild = deleteRecursively(

@@ -61,6 +54,4 @@ childNode,

);
if (shouldDeleteChild) {
delete node.children[char];
// 如果当前节点不是单词结尾并且没有其他子节点,可以删除
return (

@@ -70,6 +61,4 @@ Object.keys(node.children).length === 0 && !node.isEndOfWord

}
return false;
};
deleteRecursively(this.root, word, 0);

@@ -83,3 +72,2 @@ }

let i = 0;
while (i < characters.length) {

@@ -89,3 +77,2 @@ let node = this.root;

let matchLength = 0;
while (j < characters.length) {

@@ -96,7 +83,5 @@ if (ignoreSpaces && characters[j] === ' ') {

}
if (!node.children[characters[j]]) {
break;
}
node = node.children[characters[j]];

@@ -108,3 +93,2 @@ j++;

}
if (matchLength > 0) {

@@ -118,3 +102,2 @@ resultArray.push('*'.repeat(matchLength));

}
return resultArray.join('');

@@ -121,0 +104,0 @@ }