allprofanity 2.0.0 → 2.1.1
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 +258 -245
- package/dist/index.d.ts +156 -80
- package/dist/index.js +598 -492
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -7,7 +7,27 @@ export { default as bengaliBadWords } from "./languages/bengali-words.js";
|
|
|
7
7
|
export { default as tamilBadWords } from "./languages/tamil-words.js";
|
|
8
8
|
export { default as teluguBadWords } from "./languages/telugu-words.js";
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
10
|
+
* Logger interface for the library.
|
|
11
|
+
*/
|
|
12
|
+
export interface Logger {
|
|
13
|
+
/**
|
|
14
|
+
* Log informational messages.
|
|
15
|
+
* @param message - The message to log.
|
|
16
|
+
*/
|
|
17
|
+
info(message: string): void;
|
|
18
|
+
/**
|
|
19
|
+
* Log warning messages.
|
|
20
|
+
* @param message - The message to log.
|
|
21
|
+
*/
|
|
22
|
+
warn(message: string): void;
|
|
23
|
+
/**
|
|
24
|
+
* Log error messages.
|
|
25
|
+
* @param message - The message to log.
|
|
26
|
+
*/
|
|
27
|
+
error(message: string): void;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Configuration options for AllProfanity.
|
|
11
31
|
*/
|
|
12
32
|
export interface AllProfanityOptions {
|
|
13
33
|
languages?: string[];
|
|
@@ -18,9 +38,10 @@ export interface AllProfanityOptions {
|
|
|
18
38
|
whitelistWords?: string[];
|
|
19
39
|
strictMode?: boolean;
|
|
20
40
|
detectPartialWords?: boolean;
|
|
41
|
+
logger?: Logger;
|
|
21
42
|
}
|
|
22
43
|
/**
|
|
23
|
-
* Severity levels for profanity detection
|
|
44
|
+
* Severity levels for profanity detection.
|
|
24
45
|
*/
|
|
25
46
|
export declare enum ProfanitySeverity {
|
|
26
47
|
MILD = 1,
|
|
@@ -29,7 +50,7 @@ export declare enum ProfanitySeverity {
|
|
|
29
50
|
EXTREME = 4
|
|
30
51
|
}
|
|
31
52
|
/**
|
|
32
|
-
* Detection result
|
|
53
|
+
* Detection result for profanity detection.
|
|
33
54
|
*/
|
|
34
55
|
export interface ProfanityDetectionResult {
|
|
35
56
|
hasProfanity: boolean;
|
|
@@ -43,149 +64,204 @@ export interface ProfanityDetectionResult {
|
|
|
43
64
|
}>;
|
|
44
65
|
}
|
|
45
66
|
/**
|
|
46
|
-
*
|
|
47
|
-
* No external dependencies - built from scratch for maximum performance and control
|
|
67
|
+
* Main class for profanity detection and filtering.
|
|
48
68
|
*/
|
|
49
69
|
export declare class AllProfanity {
|
|
50
|
-
private
|
|
51
|
-
private
|
|
70
|
+
private readonly profanityTrie;
|
|
71
|
+
private readonly whitelistSet;
|
|
72
|
+
private readonly loadedLanguages;
|
|
73
|
+
private readonly logger;
|
|
52
74
|
private defaultPlaceholder;
|
|
53
|
-
private loadedLanguages;
|
|
54
|
-
private whitelistSet;
|
|
55
75
|
private enableLeetSpeak;
|
|
56
76
|
private caseSensitive;
|
|
57
77
|
private strictMode;
|
|
58
78
|
private detectPartialWords;
|
|
59
|
-
private readonly
|
|
60
|
-
private readonly
|
|
61
|
-
private readonly
|
|
62
|
-
private readonly commonPrefixes;
|
|
63
|
-
private availableLanguages;
|
|
79
|
+
private readonly availableLanguages;
|
|
80
|
+
private readonly leetMappings;
|
|
81
|
+
private readonly dynamicWords;
|
|
64
82
|
/**
|
|
65
|
-
* Create
|
|
66
|
-
* @param options -
|
|
83
|
+
* Create an AllProfanity instance.
|
|
84
|
+
* @param options - Profanity filter configuration options.
|
|
67
85
|
*/
|
|
68
86
|
constructor(options?: AllProfanityOptions);
|
|
69
87
|
/**
|
|
70
|
-
* Normalize
|
|
71
|
-
* @param text -
|
|
72
|
-
* @returns Normalized text
|
|
88
|
+
* Normalize leet speak to regular characters.
|
|
89
|
+
* @param text - The input text.
|
|
90
|
+
* @returns Normalized text.
|
|
73
91
|
*/
|
|
74
92
|
private normalizeLeetSpeak;
|
|
75
|
-
private escapeRegex;
|
|
76
93
|
/**
|
|
77
|
-
*
|
|
94
|
+
* Escape regex special characters in a string.
|
|
95
|
+
* @param str - The string to escape.
|
|
96
|
+
* @returns The escaped string.
|
|
78
97
|
*/
|
|
79
|
-
private
|
|
98
|
+
private escapeRegex;
|
|
80
99
|
/**
|
|
81
|
-
* Check if
|
|
100
|
+
* Check if a match is bounded by word boundaries (strict mode).
|
|
101
|
+
* @param text - The text.
|
|
102
|
+
* @param start - Start index.
|
|
103
|
+
* @param end - End index.
|
|
104
|
+
* @returns True if match is at word boundaries, false otherwise.
|
|
82
105
|
*/
|
|
83
106
|
private hasWordBoundaries;
|
|
84
107
|
/**
|
|
85
|
-
*
|
|
108
|
+
* Determine if a match is a whole word.
|
|
109
|
+
* @param text - The text.
|
|
110
|
+
* @param start - Start index.
|
|
111
|
+
* @param end - End index.
|
|
112
|
+
* @returns True if whole word, false otherwise.
|
|
86
113
|
*/
|
|
87
|
-
private
|
|
114
|
+
private isWholeWord;
|
|
88
115
|
/**
|
|
89
|
-
*
|
|
90
|
-
* @param
|
|
91
|
-
* @
|
|
116
|
+
* Check if a match is whitelisted.
|
|
117
|
+
* @param word - Word from dictionary.
|
|
118
|
+
* @param matchedText - Actual matched text.
|
|
119
|
+
* @returns True if whitelisted, false otherwise.
|
|
92
120
|
*/
|
|
93
|
-
|
|
121
|
+
private isWhitelistedMatch;
|
|
94
122
|
/**
|
|
95
|
-
*
|
|
96
|
-
* @param
|
|
97
|
-
* @returns
|
|
123
|
+
* Remove overlapping matches, keeping only the longest at each start position.
|
|
124
|
+
* @param matches - Array of match results.
|
|
125
|
+
* @returns Deduplicated matches.
|
|
98
126
|
*/
|
|
99
|
-
|
|
127
|
+
private deduplicateMatches;
|
|
100
128
|
/**
|
|
101
|
-
*
|
|
102
|
-
* @
|
|
129
|
+
* Detect profanity in a given text.
|
|
130
|
+
* @param text - The text to check.
|
|
131
|
+
* @returns Profanity detection result.
|
|
103
132
|
*/
|
|
104
|
-
|
|
133
|
+
detect(text: string): ProfanityDetectionResult;
|
|
105
134
|
/**
|
|
106
|
-
*
|
|
107
|
-
* @param
|
|
108
|
-
* @param
|
|
135
|
+
* Main matching function, with whole-word logic.
|
|
136
|
+
* @param searchText - The normalized text to search.
|
|
137
|
+
* @param originalText - The original text.
|
|
138
|
+
* @param matches - Array to collect matches.
|
|
109
139
|
*/
|
|
110
|
-
|
|
140
|
+
private findMatches;
|
|
111
141
|
/**
|
|
112
|
-
*
|
|
113
|
-
* @param
|
|
142
|
+
* Generate cleaned text by replacing profane words.
|
|
143
|
+
* @param originalText - The original text.
|
|
144
|
+
* @param matches - Array of matches.
|
|
145
|
+
* @returns Cleaned text.
|
|
146
|
+
*/
|
|
147
|
+
private generateCleanedText;
|
|
148
|
+
/**
|
|
149
|
+
* Check if a string contains profanity.
|
|
150
|
+
* @param text - The text to check.
|
|
151
|
+
* @returns True if profanity is found, false otherwise.
|
|
152
|
+
*/
|
|
153
|
+
check(text: string): boolean;
|
|
154
|
+
/**
|
|
155
|
+
* Clean text with a custom placeholder.
|
|
156
|
+
* @param text - The text to clean.
|
|
157
|
+
* @param placeholder - The placeholder to use.
|
|
158
|
+
* @returns Cleaned text.
|
|
159
|
+
*/
|
|
160
|
+
clean(text: string, placeholder?: string): string;
|
|
161
|
+
/**
|
|
162
|
+
* Clean text by replacing each profane word with a single placeholder (word-level).
|
|
163
|
+
* @param text - The text to clean.
|
|
164
|
+
* @param placeholder - The placeholder to use.
|
|
165
|
+
* @returns Word-level cleaned text.
|
|
166
|
+
*/
|
|
167
|
+
cleanWithPlaceholder(text: string, placeholder?: string): string;
|
|
168
|
+
/**
|
|
169
|
+
* Add word(s) to the profanity filter.
|
|
170
|
+
* @param word - Word or array of words to add.
|
|
171
|
+
*/
|
|
172
|
+
add(word: string | string[]): void;
|
|
173
|
+
/**
|
|
174
|
+
* Remove word(s) from the profanity filter.
|
|
175
|
+
* @param word - Word or array of words to remove.
|
|
176
|
+
*/
|
|
177
|
+
remove(word: string | string[]): void;
|
|
178
|
+
/**
|
|
179
|
+
* Add words to the whitelist.
|
|
180
|
+
* @param words - Words to whitelist.
|
|
114
181
|
*/
|
|
115
182
|
addToWhitelist(words: string[]): void;
|
|
116
183
|
/**
|
|
117
|
-
* Remove words from whitelist
|
|
118
|
-
* @param words -
|
|
184
|
+
* Remove words from the whitelist.
|
|
185
|
+
* @param words - Words to remove from whitelist.
|
|
119
186
|
*/
|
|
120
187
|
removeFromWhitelist(words: string[]): void;
|
|
121
188
|
/**
|
|
122
|
-
*
|
|
123
|
-
* @param
|
|
124
|
-
* @returns
|
|
189
|
+
* Check if a word is whitelisted.
|
|
190
|
+
* @param word - The word to check.
|
|
191
|
+
* @returns True if whitelisted, false otherwise.
|
|
125
192
|
*/
|
|
126
|
-
|
|
193
|
+
private isWhitelisted;
|
|
127
194
|
/**
|
|
128
|
-
*
|
|
129
|
-
* @param
|
|
130
|
-
* @returns
|
|
195
|
+
* Load a built-in language dictionary.
|
|
196
|
+
* @param language - The language key.
|
|
197
|
+
* @returns True if loaded, false otherwise.
|
|
131
198
|
*/
|
|
132
|
-
|
|
199
|
+
loadLanguage(language: string): boolean;
|
|
133
200
|
/**
|
|
134
|
-
*
|
|
135
|
-
* @param
|
|
136
|
-
* @
|
|
137
|
-
* @returns string - The cleaned string
|
|
201
|
+
* Load multiple language dictionaries.
|
|
202
|
+
* @param languages - Array of languages to load.
|
|
203
|
+
* @returns Number of successfully loaded languages.
|
|
138
204
|
*/
|
|
139
|
-
|
|
205
|
+
loadLanguages(languages: string[]): number;
|
|
140
206
|
/**
|
|
141
|
-
*
|
|
142
|
-
* @
|
|
143
|
-
* @param placeholder - The placeholder to use (defaults to '***')
|
|
144
|
-
* @returns string - The cleaned string
|
|
207
|
+
* Load all supported Indian languages.
|
|
208
|
+
* @returns Number of loaded Indian languages.
|
|
145
209
|
*/
|
|
146
|
-
|
|
210
|
+
loadIndianLanguages(): number;
|
|
147
211
|
/**
|
|
148
|
-
*
|
|
149
|
-
* @
|
|
212
|
+
* Load a custom dictionary.
|
|
213
|
+
* @param name - Name of the dictionary.
|
|
214
|
+
* @param words - Words to add.
|
|
150
215
|
*/
|
|
151
|
-
|
|
216
|
+
loadCustomDictionary(name: string, words: string[]): void;
|
|
152
217
|
/**
|
|
153
|
-
* Add word
|
|
154
|
-
* @param word -
|
|
218
|
+
* Add a single word to the trie.
|
|
219
|
+
* @param word - The word to add.
|
|
220
|
+
* @returns True if added, false otherwise.
|
|
155
221
|
*/
|
|
156
|
-
|
|
222
|
+
private addWordToTrie;
|
|
157
223
|
/**
|
|
158
|
-
*
|
|
159
|
-
* @param
|
|
224
|
+
* Calculate severity from matches.
|
|
225
|
+
* @param matches - Array of matches.
|
|
226
|
+
* @returns Severity level.
|
|
160
227
|
*/
|
|
161
|
-
|
|
228
|
+
private calculateSeverity;
|
|
162
229
|
/**
|
|
163
|
-
* Clear
|
|
230
|
+
* Clear all loaded dictionaries and dynamic words.
|
|
164
231
|
*/
|
|
165
232
|
clearList(): void;
|
|
166
233
|
/**
|
|
167
|
-
*
|
|
168
|
-
* @param placeholder -
|
|
234
|
+
* Set the placeholder character for filtered words.
|
|
235
|
+
* @param placeholder - The placeholder character.
|
|
169
236
|
*/
|
|
170
237
|
setPlaceholder(placeholder: string): void;
|
|
171
238
|
/**
|
|
172
|
-
* Get the list of
|
|
173
|
-
* @returns
|
|
239
|
+
* Get the list of loaded languages.
|
|
240
|
+
* @returns Array of loaded language keys.
|
|
174
241
|
*/
|
|
175
242
|
getLoadedLanguages(): string[];
|
|
176
243
|
/**
|
|
177
|
-
* Get the list of available
|
|
178
|
-
* @returns
|
|
244
|
+
* Get the list of available built-in languages.
|
|
245
|
+
* @returns Array of available language keys.
|
|
179
246
|
*/
|
|
180
247
|
getAvailableLanguages(): string[];
|
|
181
248
|
/**
|
|
182
|
-
* Get current configuration
|
|
249
|
+
* Get the current configuration of the profanity filter.
|
|
250
|
+
* @returns Partial configuration object.
|
|
183
251
|
*/
|
|
184
252
|
getConfig(): Partial<AllProfanityOptions>;
|
|
185
253
|
/**
|
|
186
|
-
*
|
|
254
|
+
* Rebuild the profanity trie from loaded dictionaries and dynamic words.
|
|
255
|
+
*/
|
|
256
|
+
private rebuildTrie;
|
|
257
|
+
/**
|
|
258
|
+
* Update configuration options for the profanity filter.
|
|
259
|
+
* @param options - Partial configuration object.
|
|
187
260
|
*/
|
|
188
261
|
updateConfig(options: Partial<AllProfanityOptions>): void;
|
|
189
262
|
}
|
|
263
|
+
/**
|
|
264
|
+
* Singleton instance of AllProfanity with default configuration.
|
|
265
|
+
*/
|
|
190
266
|
declare const allProfanity: AllProfanity;
|
|
191
267
|
export default allProfanity;
|