@sideid/id-profanity-filter 1.11.11 → 1.11.12
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.esm.js +34 -1
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +34 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/core/analyzer.ts +4 -0
- package/src/core/matcher.ts +32 -1
package/src/core/matcher.ts
CHANGED
|
@@ -46,6 +46,8 @@ export function findProfanity(text: string, options: FilterOptions = {}): string
|
|
|
46
46
|
maxLevenshteinDistance = 2,
|
|
47
47
|
} = { ...DEFAULT_OPTIONS, ...options };
|
|
48
48
|
|
|
49
|
+
const normalizedWhitelist = whitelist.map((w) => w.toLowerCase());
|
|
50
|
+
|
|
49
51
|
const normalizedText = normalizeText(text);
|
|
50
52
|
|
|
51
53
|
let baseWordsToCheck: string[] = wordList.length > 0 ? wordList : [];
|
|
@@ -91,7 +93,11 @@ export function findProfanity(text: string, options: FilterOptions = {}): string
|
|
|
91
93
|
|
|
92
94
|
const basicMatches = globalAhoCorasick.searchUnique(normalizedText);
|
|
93
95
|
for (const match of basicMatches) {
|
|
96
|
+
if (normalizedWhitelist.includes(match.toLowerCase())) continue;
|
|
97
|
+
|
|
94
98
|
const originalWord = aliasMap.get(match.toLowerCase()) || match.toLowerCase();
|
|
99
|
+
if (normalizedWhitelist.includes(originalWord)) continue;
|
|
100
|
+
|
|
95
101
|
matches.add(originalWord);
|
|
96
102
|
|
|
97
103
|
if (!actualMatches.has(originalWord)) {
|
|
@@ -112,7 +118,12 @@ export function findProfanity(text: string, options: FilterOptions = {}): string
|
|
|
112
118
|
|
|
113
119
|
let match;
|
|
114
120
|
while ((match = leetRegex.exec(text)) !== null) {
|
|
121
|
+
const matchedText = match[0];
|
|
122
|
+
if (normalizedWhitelist.includes(matchedText.toLowerCase())) continue;
|
|
123
|
+
|
|
115
124
|
const originalWord = aliasMap.get(word.toLowerCase()) || word.toLowerCase();
|
|
125
|
+
if (normalizedWhitelist.includes(originalWord)) continue;
|
|
126
|
+
|
|
116
127
|
matches.add(originalWord);
|
|
117
128
|
|
|
118
129
|
if (!actualMatches.has(originalWord)) {
|
|
@@ -135,7 +146,12 @@ export function findProfanity(text: string, options: FilterOptions = {}): string
|
|
|
135
146
|
|
|
136
147
|
let match;
|
|
137
148
|
while ((match = variantRegex.exec(text)) !== null) {
|
|
149
|
+
const matchedText = match[0];
|
|
150
|
+
if (normalizedWhitelist.includes(matchedText.toLowerCase())) continue;
|
|
151
|
+
|
|
138
152
|
const originalWord = aliasMap.get(word.toLowerCase()) || word.toLowerCase();
|
|
153
|
+
if (normalizedWhitelist.includes(originalWord)) continue;
|
|
154
|
+
|
|
139
155
|
matches.add(originalWord);
|
|
140
156
|
|
|
141
157
|
if (!actualMatches.has(originalWord)) {
|
|
@@ -158,7 +174,12 @@ export function findProfanity(text: string, options: FilterOptions = {}): string
|
|
|
158
174
|
|
|
159
175
|
let match;
|
|
160
176
|
while ((match = splitRegex.exec(text)) !== null) {
|
|
177
|
+
const matchedText = match[0];
|
|
178
|
+
if (normalizedWhitelist.includes(matchedText.toLowerCase())) continue;
|
|
179
|
+
|
|
161
180
|
const originalWord = aliasMap.get(word.toLowerCase()) || word.toLowerCase();
|
|
181
|
+
if (normalizedWhitelist.includes(originalWord)) continue;
|
|
182
|
+
|
|
162
183
|
matches.add(originalWord);
|
|
163
184
|
|
|
164
185
|
if (!actualMatches.has(originalWord)) {
|
|
@@ -179,8 +200,13 @@ export function findProfanity(text: string, options: FilterOptions = {}): string
|
|
|
179
200
|
);
|
|
180
201
|
|
|
181
202
|
possibleProfanity.forEach((item) => {
|
|
203
|
+
// Check if the matched word is in whitelist
|
|
204
|
+
if (normalizedWhitelist.includes(item.word.toLowerCase())) return;
|
|
205
|
+
|
|
182
206
|
const originalWord =
|
|
183
207
|
aliasMap.get(item.original.toLowerCase()) || item.original.toLowerCase();
|
|
208
|
+
if (normalizedWhitelist.includes(originalWord)) return;
|
|
209
|
+
|
|
184
210
|
matches.add(originalWord);
|
|
185
211
|
|
|
186
212
|
if (!actualMatches.has(originalWord)) {
|
|
@@ -196,10 +222,15 @@ export function findProfanity(text: string, options: FilterOptions = {}): string
|
|
|
196
222
|
);
|
|
197
223
|
|
|
198
224
|
possibleProfanity.forEach((item) => {
|
|
199
|
-
|
|
225
|
+
// Check if the matched word is in whitelist
|
|
226
|
+
if (normalizedWhitelist.includes(item.word.toLowerCase())) return;
|
|
200
227
|
|
|
201
228
|
const originalWord =
|
|
202
229
|
aliasMap.get(item.original.toLowerCase()) || item.original.toLowerCase();
|
|
230
|
+
if (normalizedWhitelist.includes(originalWord)) return;
|
|
231
|
+
|
|
232
|
+
matches.add(originalWord);
|
|
233
|
+
|
|
203
234
|
if (!actualMatches.has(originalWord)) {
|
|
204
235
|
actualMatches.set(originalWord, []);
|
|
205
236
|
}
|