allprofanity 1.1.0 → 2.1.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/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export { default as englishBadWords } from "./languages/english-words.js";
1
2
  export { default as hindiBadWords } from "./languages/hindi-words.js";
2
3
  export { default as frenchBadWords } from "./languages/french-words.js";
3
4
  export { default as germanBadWords } from "./languages/german-words.js";
@@ -5,6 +6,14 @@ export { default as spanishBadWords } from "./languages/spanish-words.js";
5
6
  export { default as bengaliBadWords } from "./languages/bengali-words.js";
6
7
  export { default as tamilBadWords } from "./languages/tamil-words.js";
7
8
  export { default as teluguBadWords } from "./languages/telugu-words.js";
9
+ /**
10
+ * Logging interface for the library
11
+ */
12
+ export interface Logger {
13
+ info(message: string): void;
14
+ warn(message: string): void;
15
+ error(message: string): void;
16
+ }
8
17
  /**
9
18
  * Configuration options for AllProfanity
10
19
  */
@@ -12,98 +21,174 @@ export interface AllProfanityOptions {
12
21
  languages?: string[];
13
22
  customDictionaries?: Record<string, string[]>;
14
23
  defaultPlaceholder?: string;
24
+ enableLeetSpeak?: boolean;
25
+ caseSensitive?: boolean;
26
+ whitelistWords?: string[];
27
+ strictMode?: boolean;
28
+ detectPartialWords?: boolean;
29
+ logger?: Logger;
30
+ }
31
+ /**
32
+ * Severity levels for profanity detection
33
+ */
34
+ export declare enum ProfanitySeverity {
35
+ MILD = 1,
36
+ MODERATE = 2,
37
+ SEVERE = 3,
38
+ EXTREME = 4
15
39
  }
16
40
  /**
17
- * AllProfanity - Extended profanity filter with multi-language support
18
- * Based on leo-profanity with additional language capabilities
41
+ * Detection result interface
42
+ */
43
+ export interface ProfanityDetectionResult {
44
+ hasProfanity: boolean;
45
+ detectedWords: string[];
46
+ cleanedText: string;
47
+ severity: ProfanitySeverity;
48
+ positions: Array<{
49
+ word: string;
50
+ start: number;
51
+ end: number;
52
+ }>;
53
+ }
54
+ /**
55
+ * Advanced AllProfanity - Fixed profanity filter with multi-language support
56
+ * Addresses all critical issues from the original implementation
19
57
  */
20
58
  export declare class AllProfanity {
21
- private filter;
59
+ private readonly profanityTrie;
60
+ private readonly whitelistSet;
61
+ private readonly loadedLanguages;
62
+ private readonly logger;
22
63
  private defaultPlaceholder;
23
- private loadedLanguages;
24
- private availableLanguages;
64
+ private enableLeetSpeak;
65
+ private caseSensitive;
66
+ private strictMode;
67
+ private detectPartialWords;
68
+ private readonly availableLanguages;
69
+ private readonly leetMappings;
70
+ private readonly dynamicWords;
71
+ constructor(options?: AllProfanityOptions);
25
72
  /**
26
- * Create a new AllProfanity instance
27
- * @param options - Configuration options
73
+ * Normalize text by converting leet speak to regular characters.
28
74
  */
29
- constructor(options?: AllProfanityOptions);
75
+ private normalizeLeetSpeak;
30
76
  /**
31
- * Load a built-in language dictionary
32
- * @param language - The language to load
33
- * @returns boolean - True if loaded successfully, false otherwise
77
+ * Properly escape regex special characters
34
78
  */
35
- loadLanguage(language: string): boolean;
79
+ private escapeRegex;
36
80
  /**
37
- * Load multiple languages at once
38
- * @param languages - Array of language names to load
39
- * @returns number - Number of successfully loaded languages
81
+ * Check if a position has word boundaries (for strict mode)
40
82
  */
41
- loadLanguages(languages: string[]): number;
83
+ private hasWordBoundaries;
42
84
  /**
43
- * Load all Indian languages at once
44
- * @returns number - Number of Indian languages loaded
85
+ * Helper method to verify whole-word matching.
45
86
  */
46
- loadIndianLanguages(): number;
87
+ private isWholeWord;
47
88
  /**
48
- * Load a custom dictionary with a given name
49
- * @param name - Name to identify this dictionary
50
- * @param words - Array of profanity words
89
+ * Check if a match is whitelisted (by actual matched substring and dictionary word)
51
90
  */
52
- loadCustomDictionary(name: string, words: string[]): void;
91
+ private isWhitelistedMatch;
53
92
  /**
54
- * Get the list of currently loaded languages
55
- * @returns string[] - Array of loaded language names
93
+ * Remove overlapping matches, keep only the longest at each start position
56
94
  */
57
- getLoadedLanguages(): string[];
95
+ private deduplicateMatches;
58
96
  /**
59
- * Get the list of available language dictionaries
60
- * @returns string[] - Array of available language names
97
+ * Advanced profanity detection using efficient trie-based algorithm
61
98
  */
62
- getAvailableLanguages(): string[];
99
+ detect(text: string): ProfanityDetectionResult;
63
100
  /**
64
- * Check if a string contains profanity
65
- * @param string - The string to check
66
- * @returns boolean - True if profanity found, false otherwise
101
+ * Main matching function, with whole-word logic.
67
102
  */
68
- check(string: string): boolean;
103
+ private findMatches;
69
104
  /**
70
- * Clean a string by replacing profanities with placeholders
71
- * @param string - The string to clean
72
- * @param placeholder - Optional custom placeholder (defaults to '*')
73
- * @returns string - The cleaned string
105
+ * Generate cleaned text by replacing profane words (non-overlapping only)
74
106
  */
75
- clean(string: string, placeholder?: string): string;
107
+ private generateCleanedText;
76
108
  /**
77
- * Clean a string by replacing each profane word with a single placeholder
78
- * @param string - The string to clean
79
- * @param placeholder - The placeholder to use (defaults to '***')
80
- * @returns string - The cleaned string
109
+ * Simple boolean check for profanity
81
110
  */
82
- cleanWithWord(string: string, placeholder?: string): string;
111
+ check(text: string): boolean;
83
112
  /**
84
- * Get the current list of profanity words
85
- * @returns string[] - Array of all profanity words
113
+ * Clean text with custom placeholder
86
114
  */
87
- list(): string[];
115
+ clean(text: string, placeholder?: string): string;
116
+ /**
117
+ * Clean text by replacing each profane word with a single placeholder (word-level)
118
+ */
119
+ cleanWithPlaceholder(text: string, placeholder?: string): string;
88
120
  /**
89
121
  * Add word(s) to the profanity list
90
- * @param word - String or array of strings to add
91
122
  */
92
123
  add(word: string | string[]): void;
93
124
  /**
94
125
  * Remove word(s) from the profanity list
95
- * @param word - String or array of strings to remove
96
126
  */
97
127
  remove(word: string | string[]): void;
98
128
  /**
99
- * Clear the filter list and reset to default
129
+ * Add words to whitelist
130
+ */
131
+ addToWhitelist(words: string[]): void;
132
+ /**
133
+ * Remove words from whitelist
134
+ */
135
+ removeFromWhitelist(words: string[]): void;
136
+ /**
137
+ * Helper for whitelist checking with correct normalization
138
+ */
139
+ private isWhitelisted;
140
+ /**
141
+ * Load a built-in language dictionary
142
+ */
143
+ loadLanguage(language: string): boolean;
144
+ /**
145
+ * Load multiple languages at once
146
+ */
147
+ loadLanguages(languages: string[]): number;
148
+ /**
149
+ * Load all Indian languages
150
+ */
151
+ loadIndianLanguages(): number;
152
+ /**
153
+ * Load a custom dictionary
154
+ */
155
+ loadCustomDictionary(name: string, words: string[]): void;
156
+ /**
157
+ * Add a single word to the trie structure
158
+ */
159
+ private addWordToTrie;
160
+ /**
161
+ * Remove overlapping matches, keep only the longest at each start position
162
+ */
163
+ private calculateSeverity;
164
+ /**
165
+ * Clear all loaded dictionaries
100
166
  */
101
167
  clearList(): void;
102
168
  /**
103
- * Change the character used as placeholder
104
- * @param placeholder - Single character to use as placeholder
169
+ * Set placeholder character
105
170
  */
106
171
  setPlaceholder(placeholder: string): void;
172
+ /**
173
+ * Get loaded languages
174
+ */
175
+ getLoadedLanguages(): string[];
176
+ /**
177
+ * Get available languages
178
+ */
179
+ getAvailableLanguages(): string[];
180
+ /**
181
+ * Get current configuration
182
+ */
183
+ getConfig(): Partial<AllProfanityOptions>;
184
+ /**
185
+ * Rebuilds the profanity trie from loaded language dictionaries and dynamic words.
186
+ */
187
+ private rebuildTrie;
188
+ /**
189
+ * Update configuration. Rebuild trie if needed.
190
+ */
191
+ updateConfig(options: Partial<AllProfanityOptions>): void;
107
192
  }
108
193
  declare const allProfanity: AllProfanity;
109
194
  export default allProfanity;