Skip to content

Commit

Permalink
Feat/docs (#9)
Browse files Browse the repository at this point in the history
* fix/Minor Docs

* fix/Minor Docs #2

* fix/Minor Docs #3
  • Loading branch information
thegdsks authored Aug 3, 2024
1 parent b8204a6 commit 1962d26
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 16 deletions.
6 changes: 1 addition & 5 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
"plugin:@typescript-eslint/recommended",
],
"plugins": ["react", "@typescript-eslint"],
"env": {
Expand All @@ -23,9 +22,6 @@
"react": {
"version": "detect"
}
},
"rules": {
"prettier/prettier": "error"
}
}

31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# Glin Profanity
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.

Expand All @@ -8,7 +9,11 @@ To install Glin-Profanity, use npm:
```bash
npm install glin-profanity
```
OR

```bash
yarn add glin-profanity
```
## Usage

### Basic Usage
Expand Down Expand Up @@ -71,12 +76,25 @@ export default App;
#### Constructor

```typescript
new Filter(config?: { languages?: Language[]; allLanguages?: boolean });
new Filter(config?: {
languages?: Language[];
allLanguages?: boolean;
caseSensitive?: boolean;
wordBoundaries?: boolean;
customWords?: string[];
replaceWith?: string;
severityLevels?: boolean;
});
```

- `config`: An optional configuration object.
- `languages`: An array of languages to check for profanities.
- `allLanguages`: A boolean indicating whether to check for all languages.
- `caseSensitive`: A boolean indicating whether the profanity check should be case-sensitive.
- `wordBoundaries`: A boolean indicating whether to consider word boundaries when checking for profanities.
- `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.

#### Methods

Expand All @@ -103,6 +121,8 @@ checkProfanity(text: string): CheckProfanityResult;
- Returns: `CheckProfanityResult`
- `containsProfanity`: `boolean` - `true` if the text contains profanities, `false` otherwise.
- `profaneWords`: `string[]` - An array of profane words found in the text.
- `processedText`: `string` - The text with profane words replaced (if `replaceWith` is specified).
- `severityMap`: `{ [word: string]: number }` - A map of profane words to their severity levels (if `severityLevels` is specified).

### `useProfanityChecker` Hook

Expand All @@ -113,14 +133,21 @@ A custom React hook for using the profanity checker.
- `config`: An optional configuration object.
- `languages`: An array of languages to check for profanities.
- `allLanguages`: A boolean indicating whether to check for all languages.
- `caseSensitive`: A boolean indicating whether the profanity check should be case-sensitive.
- `wordBoundaries`: A boolean indicating whether to consider word boundaries when checking for profanities.
- `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.
- `customActions`: A function to execute custom actions when profanity is detected.

#### Return Value

- `result`: The result of the profanity check.
- `checkText`: A function to check a given text for profanities.
- `checkTextAsync`: A function to check a given text for profanities asynchronously.

```typescript
const { result, checkText } = useProfanityChecker(config);
const { result, checkText, checkTextAsync } = useProfanityChecker(config);
```

## License
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.0",
"version": "1.1.1",
"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
36 changes: 28 additions & 8 deletions src/filters/Filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@ interface FilterConfig {
caseSensitive?: boolean;
wordBoundaries?: boolean;
customWords?: string[];
replaceWith?: string;
severityLevels?: boolean;
}

class Filter {
private words: Set<string>;
private words: Map<string, number>;
private caseSensitive: boolean;
private wordBoundaries: boolean;
private replaceWith?: string;
private severityLevels: 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;

if (config?.allLanguages) {
for (const lang in dictionary) {
Expand All @@ -39,32 +45,46 @@ class Filter {
words = [...words, ...config.customWords];
}

this.words = new Set<string>(words);
this.words = new Map(words.map(word => [word, 1])); // Default severity level is 1
}

isProfane(value: string): boolean {
private getRegex(word: string): RegExp {
const flags = this.caseSensitive ? 'g' : 'gi';
for (const word of this.words) {
const boundary = this.wordBoundaries ? '\\b' : '';
const wordExp = new RegExp(`${boundary}${word.replace(/(\W)/g, '\\$1')}${boundary}`, flags);
if (wordExp.test(value)) return true;
const boundary = this.wordBoundaries ? '\\b' : '';
return new RegExp(`${boundary}${word.replace(/(\W)/g, '\\$1')}${boundary}`, flags);
}

isProfane(value: string): boolean {
for (const word of this.words.keys()) {
if (this.getRegex(word).test(value)) return true;
}
return false;
}

checkProfanity(text: string): CheckProfanityResult {
const words = text.split(/\s+/);
const profaneWords: string[] = [];
const severityMap: { [word: string]: number } = {};

for (const word of words) {
if (this.isProfane(word)) {
if (this.words.has(word.toLowerCase())) {
profaneWords.push(word);
severityMap[word] = this.words.get(word.toLowerCase())!;
}
}

let processedText = text;
if (this.replaceWith) {
for (const word of profaneWords) {
processedText = processedText.replace(this.getRegex(word), this.replaceWith);
}
}

return {
containsProfanity: profaneWords.length > 0,
profaneWords,
processedText: this.replaceWith ? processedText : undefined,
severityMap: this.severityLevels ? severityMap : undefined,
};
}
}
Expand Down

0 comments on commit 1962d26

Please sign in to comment.