From 12672290fe2b78131a93c4732e984911246ce69b Mon Sep 17 00:00:00 2001 From: GDS K S <39922405+gagandua078@users.noreply.github.com> Date: Fri, 2 Aug 2024 19:43:42 -0500 Subject: [PATCH] feat/ Log Profane Enabled --- README.md | 6 ++++++ package.json | 2 +- src/filters/Filter.ts | 14 ++++++++++++-- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cc00cbc..4c39a52 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,8 @@ new Filter(config?: { customWords?: string[]; replaceWith?: string; severityLevels?: boolean; + ignoreWords?: string[]; + logProfanity?: boolean; }); ``` @@ -95,6 +97,8 @@ new Filter(config?: { - `customWords`: An array of custom words to include in the profanity check. - `replaceWith`: A string to replace profane words with. - `severityLevels`: A boolean indicating whether to include severity levels for profane words. + - `ignoreWords`: An array of words to ignore in the profanity check. + - `logProfanity`: A boolean indicating whether to log detected profane words. #### Methods @@ -138,6 +142,8 @@ A custom React hook for using the profanity checker. - `customWords`: An array of custom words to include in the profanity check. - `replaceWith`: A string to replace profane words with. - `severityLevels`: A boolean indicating whether to include severity levels for profane words. + - `ignoreWords`: An array of words to ignore in the profanity check. + - `logProfanity`: A boolean indicating whether to log detected profane words. - `customActions`: A function to execute custom actions when profanity is detected. #### Return Value diff --git a/package.json b/package.json index 0619d78..4690f74 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "glin-profanity", - "version": "1.1.1", + "version": "1.1.2", "description": "Glin-Profanity is a lightweight and efficient npm package designed to detect and filter profane language in text inputs across multiple languages. Whether you’re building a chat application, a comment section, or any platform where user-generated content is involved, Glin-Profanity helps you maintain a clean and respectful environment.", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/filters/Filter.ts b/src/filters/Filter.ts index 53d15b5..bed7dfd 100644 --- a/src/filters/Filter.ts +++ b/src/filters/Filter.ts @@ -9,6 +9,8 @@ interface FilterConfig { customWords?: string[]; replaceWith?: string; severityLevels?: boolean; + ignoreWords?: string[]; + logProfanity?: boolean; } class Filter { @@ -17,6 +19,8 @@ class Filter { private wordBoundaries: boolean; private replaceWith?: string; private severityLevels: boolean; + private ignoreWords: Set; + private logProfanity: boolean; constructor(config?: FilterConfig) { let words: string[] = []; @@ -24,6 +28,8 @@ class Filter { this.wordBoundaries = config?.wordBoundaries ?? true; this.replaceWith = config?.replaceWith; this.severityLevels = config?.severityLevels ?? false; + this.ignoreWords = new Set(config?.ignoreWords?.map(word => word.toLowerCase()) || []); + this.logProfanity = config?.logProfanity ?? false; if (config?.allLanguages) { for (const lang in dictionary) { @@ -56,7 +62,7 @@ class Filter { isProfane(value: string): boolean { for (const word of this.words.keys()) { - if (this.getRegex(word).test(value)) return true; + if (!this.ignoreWords.has(word.toLowerCase()) && this.getRegex(word).test(value)) return true; } return false; } @@ -67,12 +73,16 @@ class Filter { const severityMap: { [word: string]: number } = {}; for (const word of words) { - if (this.words.has(word.toLowerCase())) { + if (this.words.has(word.toLowerCase()) && !this.ignoreWords.has(word.toLowerCase())) { profaneWords.push(word); severityMap[word] = this.words.get(word.toLowerCase())!; } } + if (this.logProfanity && profaneWords.length > 0) { + console.log(`Profane words detected: ${profaneWords.join(', ')}`); + } + let processedText = text; if (this.replaceWith) { for (const word of profaneWords) {