ts-sensitivewords 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # TS-SensitiveWords
2
+
3
+ A TypeScript rewrite of [alex-my/js-sensitivewords](https://github.com/alex-my/js-sensitivewords).
@@ -0,0 +1,51 @@
1
+ //#region src/lib/sensitivewords.d.ts
2
+ declare class SensitiveWords {
3
+ private map;
4
+ /**
5
+ * Add blocking words.
6
+ *
7
+ * @param words the blocking words.
8
+ */
9
+ addWords(...words: string[]): void;
10
+ /**
11
+ * Check whether there's a blocking word in the content.
12
+ * @param content the content to match.
13
+ * @return `true` if matched any.
14
+ */
15
+ contains(content: string): boolean;
16
+ /**
17
+ * Find the blocking words in the content.
18
+ *
19
+ * @param content the content to match.
20
+ * @param startIndex the starting index of the content to match.
21
+ * @return the length of matched sensitive word starting at the given index, or 0 if not any matched.
22
+ */
23
+ check(content: string, startIndex?: number): number;
24
+ /**
25
+ * Replace the blocking words in the content with given mask.
26
+ *
27
+ * @param content the content to match.
28
+ * @param mask the mask to replace the blocking words, a single character is recommended (e.g., '*').
29
+ * @return the content with all blocking words replaced with masks.
30
+ */
31
+ mask(content: string, mask?: string): string;
32
+ /**
33
+ * Find all blocking words in the content.
34
+ *
35
+ * @param content the content to match.
36
+ * @param startIndex the starting index of the content to match.
37
+ * @return a list of blocking words.
38
+ */
39
+ find(content: string, startIndex?: number): string[];
40
+ }
41
+ //#endregion
42
+ //#region src/lib/dfa.d.ts
43
+ interface DFANode {
44
+ isEnd: boolean;
45
+ [key: string]: any;
46
+ }
47
+ //#endregion
48
+ //#region src/index.d.ts
49
+ declare const sw: SensitiveWords;
50
+ //#endregion
51
+ export { DFANode, SensitiveWords, sw as default, sw as instance };
package/dist/index.mjs ADDED
@@ -0,0 +1,110 @@
1
+ //#region src/lib/dfa.ts
2
+ var DFAMap = class {
3
+ map = { isEnd: false };
4
+ get words() {
5
+ return this.map;
6
+ }
7
+ append(word) {
8
+ let curr = this.map;
9
+ for (let i = 0; i < word.length; i++) {
10
+ const char = word[i];
11
+ if (curr[char] !== void 0) curr = curr[char];
12
+ else curr = curr[char] = { isEnd: false };
13
+ if (i === word.length - 1) curr.isEnd = true;
14
+ }
15
+ }
16
+ appendAll(...words) {
17
+ words.forEach((w) => this.append(w));
18
+ }
19
+ };
20
+
21
+ //#endregion
22
+ //#region src/lib/sensitivewords.ts
23
+ var SensitiveWords = class {
24
+ map = new DFAMap();
25
+ /**
26
+ * Add blocking words.
27
+ *
28
+ * @param words the blocking words.
29
+ */
30
+ addWords(...words) {
31
+ this.map.appendAll(...words);
32
+ }
33
+ /**
34
+ * Check whether there's a blocking word in the content.
35
+ * @param content the content to match.
36
+ * @return `true` if matched any.
37
+ */
38
+ contains(content) {
39
+ for (let i = 0; i < content.length; i++) if (this.check(content, i) > 0) return true;
40
+ return false;
41
+ }
42
+ /**
43
+ * Find the blocking words in the content.
44
+ *
45
+ * @param content the content to match.
46
+ * @param startIndex the starting index of the content to match.
47
+ * @return the length of matched sensitive word starting at the given index, or 0 if not any matched.
48
+ */
49
+ check(content, startIndex = 0) {
50
+ let matchLen = 0, tempLen = 0;
51
+ let curr = this.map.words;
52
+ for (let i = startIndex; i < content.length; i++) {
53
+ let char = content[i];
54
+ curr = curr[char];
55
+ if (curr === void 0) break;
56
+ else {
57
+ tempLen++;
58
+ if (curr.isEnd) matchLen = tempLen;
59
+ }
60
+ }
61
+ return matchLen;
62
+ }
63
+ /**
64
+ * Replace the blocking words in the content with given mask.
65
+ *
66
+ * @param content the content to match.
67
+ * @param mask the mask to replace the blocking words, a single character is recommended (e.g., '*').
68
+ * @return the content with all blocking words replaced with masks.
69
+ */
70
+ mask(content, mask = "*") {
71
+ let result = content;
72
+ let i = 0;
73
+ while (i < content.length) {
74
+ const length = this.check(content, i);
75
+ if (length > 0) {
76
+ const replacement = mask.repeat(length);
77
+ result = result.substring(0, i) + replacement + result.substring(i + length);
78
+ i += length;
79
+ } else i++;
80
+ }
81
+ return result;
82
+ }
83
+ /**
84
+ * Find all blocking words in the content.
85
+ *
86
+ * @param content the content to match.
87
+ * @param startIndex the starting index of the content to match.
88
+ * @return a list of blocking words.
89
+ */
90
+ find(content, startIndex = 0) {
91
+ const result = [];
92
+ let i = startIndex;
93
+ while (i < content.length) {
94
+ const length = this.check(content, i);
95
+ if (length > 0) {
96
+ result.push(content.substring(i, i + length));
97
+ i += length;
98
+ } else i++;
99
+ }
100
+ return result;
101
+ }
102
+ };
103
+
104
+ //#endregion
105
+ //#region src/index.ts
106
+ const sw = new SensitiveWords();
107
+ var src_default = sw;
108
+
109
+ //#endregion
110
+ export { SensitiveWords, src_default as default, sw as instance };
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "ts-sensitivewords",
3
+ "type": "module",
4
+ "version": "1.0.0",
5
+ "description": "A DFA map implementation for sensitive words matching and blocking.",
6
+ "author": "Taskeren",
7
+ "license": "MIT",
8
+ "homepage": "https://github.com/Taskeren/ts-sensitivewords",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/Taskeren/ts-sensitivewords.git"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/Taskeren/ts-sensitivewords/issues"
15
+ },
16
+ "exports": {
17
+ ".": "./dist/index.mjs",
18
+ "./package.json": "./package.json"
19
+ },
20
+ "main": "./dist/index.mjs",
21
+ "module": "./dist/index.mjs",
22
+ "types": "./dist/index.d.mts",
23
+ "files": [
24
+ "dist"
25
+ ],
26
+ "scripts": {
27
+ "build": "tsdown",
28
+ "dev": "tsdown --watch",
29
+ "test": "vitest",
30
+ "typecheck": "tsc --noEmit",
31
+ "prepublishOnly": "npm run build"
32
+ },
33
+ "devDependencies": {
34
+ "@types/node": "^25.0.3",
35
+ "bumpp": "^10.3.2",
36
+ "tsdown": "^0.18.1",
37
+ "typescript": "^5.9.3",
38
+ "vitest": "^4.0.16"
39
+ }
40
+ }