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/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
- * Configuration options for AllProfanity
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 interface
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
- * Advanced AllProfanity - Custom profanity filter with multi-language support and leet speak detection
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 profanitySet;
51
- private normalizedProfanityMap;
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 leetMap;
60
- private readonly wordBoundaryChars;
61
- private readonly commonSuffixes;
62
- private readonly commonPrefixes;
63
- private availableLanguages;
79
+ private readonly availableLanguages;
80
+ private readonly leetMappings;
81
+ private readonly dynamicWords;
64
82
  /**
65
- * Create a new AllProfanity instance
66
- * @param options - Configuration options
83
+ * Create an AllProfanity instance.
84
+ * @param options - Profanity filter configuration options.
67
85
  */
68
86
  constructor(options?: AllProfanityOptions);
69
87
  /**
70
- * Normalize text by converting leet speak to regular characters
71
- * @param text - Text to normalize
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
- * Generate word variations with common prefixes and suffixes
94
+ * Escape regex special characters in a string.
95
+ * @param str - The string to escape.
96
+ * @returns The escaped string.
78
97
  */
79
- private generateWordVariations;
98
+ private escapeRegex;
80
99
  /**
81
- * Check if text contains word boundaries around a match
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
- * Calculate severity based on detected words
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 calculateSeverity;
114
+ private isWholeWord;
88
115
  /**
89
- * Load a built-in language dictionary
90
- * @param language - The language to load
91
- * @returns boolean - True if loaded successfully, false otherwise
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
- loadLanguage(language: string): boolean;
121
+ private isWhitelistedMatch;
94
122
  /**
95
- * Load multiple languages at once
96
- * @param languages - Array of language names to load
97
- * @returns number - Number of successfully loaded languages
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
- loadLanguages(languages: string[]): number;
127
+ private deduplicateMatches;
100
128
  /**
101
- * Load all Indian languages at once
102
- * @returns number - Number of Indian languages loaded
129
+ * Detect profanity in a given text.
130
+ * @param text - The text to check.
131
+ * @returns Profanity detection result.
103
132
  */
104
- loadIndianLanguages(): number;
133
+ detect(text: string): ProfanityDetectionResult;
105
134
  /**
106
- * Load a custom dictionary with a given name
107
- * @param name - Name to identify this dictionary
108
- * @param words - Array of profanity words
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
- loadCustomDictionary(name: string, words: string[]): void;
140
+ private findMatches;
111
141
  /**
112
- * Add words to whitelist (words that should never be flagged as profanity)
113
- * @param words - Array of words to whitelist
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 - Array of words to remove from whitelist
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
- * Advanced profanity detection with detailed results
123
- * @param text - The text to analyze
124
- * @returns ProfanityDetectionResult - Detailed detection results
189
+ * Check if a word is whitelisted.
190
+ * @param word - The word to check.
191
+ * @returns True if whitelisted, false otherwise.
125
192
  */
126
- detect(text: string): ProfanityDetectionResult;
193
+ private isWhitelisted;
127
194
  /**
128
- * Check if a string contains profanity (simple boolean check)
129
- * @param string - The string to check
130
- * @returns boolean - True if profanity found, false otherwise
195
+ * Load a built-in language dictionary.
196
+ * @param language - The language key.
197
+ * @returns True if loaded, false otherwise.
131
198
  */
132
- check(string: string): boolean;
199
+ loadLanguage(language: string): boolean;
133
200
  /**
134
- * Clean a string by replacing profanities with placeholders
135
- * @param string - The string to clean
136
- * @param placeholder - Optional custom placeholder
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
- clean(string: string, placeholder?: string): string;
205
+ loadLanguages(languages: string[]): number;
140
206
  /**
141
- * Clean a string by replacing each profane word with a single placeholder
142
- * @param string - The string to clean
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
- cleanWithWord(string: string, placeholder?: string): string;
210
+ loadIndianLanguages(): number;
147
211
  /**
148
- * Get the current list of profanity words
149
- * @returns string[] - Array of all profanity words
212
+ * Load a custom dictionary.
213
+ * @param name - Name of the dictionary.
214
+ * @param words - Words to add.
150
215
  */
151
- list(): string[];
216
+ loadCustomDictionary(name: string, words: string[]): void;
152
217
  /**
153
- * Add word(s) to the profanity list
154
- * @param word - String or array of strings to add
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
- add(word: string | string[]): void;
222
+ private addWordToTrie;
157
223
  /**
158
- * Remove word(s) from the profanity list
159
- * @param word - String or array of strings to remove
224
+ * Calculate severity from matches.
225
+ * @param matches - Array of matches.
226
+ * @returns Severity level.
160
227
  */
161
- remove(word: string | string[]): void;
228
+ private calculateSeverity;
162
229
  /**
163
- * Clear the filter list and reset to default
230
+ * Clear all loaded dictionaries and dynamic words.
164
231
  */
165
232
  clearList(): void;
166
233
  /**
167
- * Change the character used as placeholder
168
- * @param placeholder - Single character to use as 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 currently loaded languages
173
- * @returns string[] - Array of loaded language names
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 language dictionaries
178
- * @returns string[] - Array of available language names
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
- * Update configuration
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;