Skip to content

Commit

Permalink
feat/ Log Profane Enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
thegdsks committed Aug 3, 2024
1 parent 1f8a732 commit 1267229
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ new Filter(config?: {
customWords?: string[];
replaceWith?: string;
severityLevels?: boolean;
ignoreWords?: string[];
logProfanity?: boolean;
});
```

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
14 changes: 12 additions & 2 deletions src/filters/Filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ interface FilterConfig {
customWords?: string[];
replaceWith?: string;
severityLevels?: boolean;
ignoreWords?: string[];
logProfanity?: boolean;
}

class Filter {
Expand All @@ -17,13 +19,17 @@ class Filter {
private wordBoundaries: boolean;
private replaceWith?: string;
private severityLevels: boolean;
private ignoreWords: Set<string>;
private logProfanity: boolean;

constructor(config?: FilterConfig) {
let words: string[] = [];
this.caseSensitive = config?.caseSensitive ?? false;
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) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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) {
Expand Down

0 comments on commit 1267229

Please sign in to comment.