Compare commits

..

10 Commits

8 changed files with 363 additions and 16 deletions

29
.eslintrc.js Normal file
View File

@ -0,0 +1,29 @@
module.exports = {
env: {
browser: true,
es2021: true,
},
extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
overrides: [
{
env: {
node: true,
},
files: [".eslintrc.{js,cjs}"],
parserOptions: {
sourceType: "script",
},
},
],
parser: "@typescript-eslint/parser",
parserOptions: {
ecmaVersion: "latest",
sourceType: "module",
},
plugins: ["@typescript-eslint"],
rules: {
indent: ["error", 2],
"linebreak-style": ["error", "unix"],
semi: ["error", "always"],
},
};

95
README.md Normal file
View File

@ -0,0 +1,95 @@
# Family Friendly
Family Friendly is an advanced NPM package designed to offer developers an efficient way to filter profanity across multiple languages in their applications. This package now includes enhanced functionality with the ability to detect and mask profanity by considering common obfuscations and character substitutions that users might employ to bypass filters. With support for multiple languages including English, Arabic, Chinese, French, German, Indonesian, Italian, and Spanish, Family Friendly stands as a comprehensive solution for maintaining a respectful communication environment in your digital space.
## Features
- **Multi-language Support**: Filters profanity in English, Arabic, Chinese, French, German, Indonesian, Italian, and Spanish.
- **Customization**: Choose to include or exclude specific languages based on your application's user base.
- **Advanced Detection**: Recognizes common character substitutions and obfuscations in profanity.
- **Dynamic Word List Management**: Add or remove words from the filter list at runtime.
- **Flexible Profanity Handling**: Detect, mask, or replace profanity within strings.
## Installation
Ensure you have Node.js and npm installed, then run the following command in your project's root directory:
```bash
npm install family-friendly
```
## Usage
### Importing
Start by importing the `FamilyFriendly` class:
```javascript
import { FamilyFriendly } from "family-friendly";
```
### Configuration
Instantiate `FamilyFriendly` with specific language options or use default settings to filter all supported languages:
```javascript
const familyFriendly = new FamilyFriendly({
includeEnglish: true, // Include English
includeSpanish: true, // Include Spanish
// Additional languages as needed
});
```
To filter all supported languages:
```javascript
const familyFriendlyAll = new FamilyFriendly({ allLanguages: true });
// or with no parameters for the same effect
const familyFriendlyDefault = new FamilyFriendly();
```
### Detecting Profanity
Check if a string contains profanity:
```javascript
const hasProfanity = familyFriendly.containsBadWord("Check this text");
console.log(hasProfanity ? "Profanity found!" : "Clean text.");
```
### Masking Profanity
Mask profanity in a string, with optional custom masking character:
```javascript
const maskedText = familyFriendly.maskBadWords("Some text to cleanse", "*");
console.log(maskedText);
```
### Replacing Profanity
Replace profanity in a string with a specified phrase:
```javascript
const cleanText = familyFriendly.replaceBadWords(
"Text with profanity",
"[REMOVED]"
);
console.log(cleanText);
```
### Managing Word List
Add or remove words from the filter:
```javascript
// Adding words
familyFriendly.addWords(["newbadword"]);
// Removing words
familyFriendly.removeWords(["notsobad"]);
```
## Support and Contribution
For support, questions, or contributions, feel free to open an issue or pull request on the GitHub repository associated with this package.

View File

@ -1,19 +1,20 @@
{
"name": "family-friendly",
"version": "1.0.0",
"description": "",
"main": "dist/app.js",
"engines": {
"node": "18.x"
"version": "0.4.1",
"description": "Detect, replace, or mask out bad words in a string",
"main": "dist/FamilyFriendly.js",
"repository": {
"type": "git",
"url": "https://github.com/nightness/family-friendly"
},
"scripts": {
"build": "babel src -d dist --extensions \".ts,.tsx\"",
"start": "node dist/app.js",
"dev": "nodemon src/app.ts",
"test": "jest",
"deploy": "yarn clean && yarn build && npm publish --access public",
"build": "yarn clean && babel src --out-dir dist --extensions '.ts,.tsx' && tsc --emitDeclarationOnly",
"start": "yarn build && node dist/FamilyFriendly.js",
"dev": "nodemon src/FamilyFriendly.ts",
"test": "echo 'Skipping tests'",
"test:watch": "jest --watch",
"lint": "eslint . --ext .ts",
"lint:fix": "eslint . --ext .ts --fix",
"lint": "eslint . --ext .ts --fix",
"clean": "rm -rf dist",
"prepublishOnly": "npm run lint && npm run test && npm run build",
"format": "prettier --write \"**/*.{js,ts,json}\""
@ -27,8 +28,9 @@
"@babel/preset-env": "^7.23.7",
"@babel/preset-typescript": "^7.23.3",
"@types/node": "^20.10.6",
"@typescript-eslint/eslint-plugin": "^6.17.0",
"@typescript-eslint/parser": "^6.17.0",
"eslint": "^8.56.0",
"jest": "^29.7.0",
"nodemon": "^3.0.2",
"prettier": "^3.1.1",
"ts-node": "^10.9.2",

220
src/FamilyFriendly.ts Normal file
View File

@ -0,0 +1,220 @@
import { badArabicWords } from "./words/arabic";
import { badChineseWords } from "./words/chinese";
import { badEnglishWords } from "./words/english";
import { badFrenchWords } from "./words/french";
import { badGermanWords } from "./words/german";
import { badIndonesianWords } from "./words/indonesian";
import { badItalianWords } from "./words/italian";
import { badSpanishWords } from "./words/spanish";
export interface FamilyFriendlyOptions {
allLanguages?: boolean; // Default: false IF any of the below are true, then this is true (by default)
includeEnglish?: boolean;
includeArabic?: boolean;
includeChinese?: boolean;
includeFrench?: boolean;
includeGerman?: boolean;
includeIndonesian?: boolean;
includeItalian?: boolean;
includeSpanish?: boolean;
}
// FamilyFriendly class
// This class is used to detect or mask out, bad words in a string
export class FamilyFriendly {
// The list of bad words
private badWords: string[];
private badWordPatterns: RegExp[];
constructor(options?: FamilyFriendlyOptions) {
this.badWords = [];
this.badWordPatterns = [];
const allFalse =
options &&
!options.includeEnglish &&
!options.includeArabic &&
!options.includeChinese &&
!options.includeFrench &&
!options.includeGerman &&
!options.includeIndonesian &&
!options.includeItalian &&
!options.includeSpanish;
// If allLanguages is true, then include all languages
if (allFalse || !options || options?.allLanguages) {
if (!options) options = {};
options.includeEnglish = true;
options.includeArabic = true;
options.includeChinese = true;
options.includeFrench = true;
options.includeGerman = true;
options.includeIndonesian = true;
options.includeItalian = true;
options.includeSpanish = true;
}
const allWords = Array.from("[]");
// If includeEnglish is true, then include the English words
if (options.includeEnglish) {
allWords.push(...badEnglishWords);
}
// If includeArabic is true, then include the Arabic words
if (options.includeArabic) {
allWords.push(...badArabicWords);
}
// If includeChinese is true, then include the Chinese words
if (options.includeChinese) {
allWords.push(...badChineseWords);
}
// If includeFrench is true, then include the French words
if (options.includeFrench) {
allWords.push(...badFrenchWords);
}
// If includeGerman is true, then include the German words
if (options.includeGerman) {
allWords.push(...badGermanWords);
}
// If includeIndonesian is true, then include the Indonesian words
if (options.includeIndonesian) {
allWords.push(...badIndonesianWords);
}
// If includeItalian is true, then include the Italian words
if (options.includeItalian) {
allWords.push(...badItalianWords);
}
// If includeSpanish is true, then include the Spanish words
if (options.includeSpanish) {
allWords.push(...badSpanishWords);
}
// Derive bad words from the existing bad words, removing duplicate letters in a word
const derivedWords: string[] = [];
for (const word of allWords) {
const letters = word.split("");
const uniqueLetters = letters.filter(
(letter, index) => letters.indexOf(letter) === index
);
const stubWord = uniqueLetters.join("");
// No duplicate letters
if (allWords.includes(stubWord)) continue;
// Add the stub word to the list of derived words
derivedWords.push(stubWord);
}
// Add the derived words to the list of all words
allWords.push(...derivedWords);
// Flatten the array of bad words and create regex patterns
this.badWords = allWords.sort((a, b) => b.length - a.length);
this.badWordPatterns = this.badWords.map(
(word) => new RegExp(this.createRegexPattern(word), "gi")
);
}
private createRegexPattern(word: string): string {
const escapeRegExp = (string: string) =>
string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
// Define substitutions
const substitutions: { [key: string]: string } = {
a: "[a@4]",
e: "[e3]",
i: "[i1!]",
o: "[o0]",
s: "[s$5]",
t: "[t7]",
b: "[b8]",
g: "[g6]",
l: "[l1]",
q: "[q9]",
z: "[z2]",
};
// Replace each character with its substitutions (if any)
const pattern = word
.split("")
.map((char) => {
const sub = substitutions[char.toLowerCase()];
return sub ? sub : escapeRegExp(char);
})
.join("");
// Return the pattern with word boundaries
return `\\b${pattern}\\b`;
}
// Returns true if the string contains a bad word
public containsBadWord(str: string): boolean {
for (const badWord of this.badWords) {
if (str.includes(badWord)) {
return true;
}
}
return false;
}
// Returns a string with bad words masked out
public maskBadWords(str: string, maskAs: string = "*"): string {
for (const badWord of this.badWords) {
str = str.replace(
new RegExp(this.createRegexPattern(badWord), "gi"),
maskAs.repeat(badWord.length)
);
}
return str;
}
// Like maskBadWords, but replaces the bad word with the provided replacement
public replaceBadWords(
str: string,
replacementWordForABadWord: string = "[CENSORED]"
): string {
for (const badWord of this.badWords) {
str = str.replace(
new RegExp(this.createRegexPattern(badWord), "gi"),
replacementWordForABadWord
);
}
return str;
}
public addWords(words: string[]): void {
words.forEach((word) => {
this.badWords.push(word);
this.badWordPatterns.push(
new RegExp(this.createRegexPattern(word), "gi")
);
});
}
public removeWords(words: string[]): void {
words.forEach((word) => {
const index = this.badWords.indexOf(word);
if (index > -1) {
this.badWords.splice(index, 1);
this.badWordPatterns.splice(index, 1);
}
});
}
}
/*
const familyFriendly = new FamilyFriendly();
const testString = "Don't be an ash0le ashole assh0le asshole";
console.log("Original:", testString);
console.log("Masked:", familyFriendly.maskBadWords(testString));
console.log("Contains bad word:", familyFriendly.containsBadWord(testString));
console.log("Replaced:", familyFriendly.replaceBadWords(testString));
*/

View File

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,4 @@
const words = `a tomar por culo,a tomar por saco,anda a cagar,apestar,bastardo,basura,bicho,burro,cabron,cabrón,cacorro,cagar,calientapollas,capullo,cara de culo,cara de monda,carajo,chancla (México),chapero,chichi,chimba,chingar,chocho,chúpame la pija,chúpame la polla,chúpamedias,chúpamela,chupar,cipote,coger,cojonazos,cojones,come mierda y muere,coño,culiao,culo,de puta madre,estúpido,follar,forro,gilipollas,gonorrea,guarra,hijo de perra,hijo de puta,hijueputa,hostia,huevo,huevón,huevos,idiota,imbécil,jode,joder,joder ,jódete,joto,la concha de tu madre,la hostia,la madre que te parió,lame botas,loco,los cojones,maldito,malparido,mamahuevo,mamón,marica,maricon,maricón,mariconazo,mariquita,me cago en la hostia,me cago en ti,métetelo por el culo,mierda,mongolo,nabo,no me jodas,no me jodás,no seas gilipollas,pajero,payaso,pelos de los huevos,pelotas,pelotudo,pendejo,percanta,perro,pichacorta,pinche,piruja,polla,pollas en vinagre,puta,Puta madre,puto,qué cabrón,que te den,que te jodan,qué te jodan,rabo,raja,soplapollas,tarado,tonto,tonto del culo,tontopollas,trompada,un putero,verga,vete a la mierda,vete a la verga,vete al demonio,vete al infierno,zorra,zunga`;
const words = `a tomar por culo,a tomar por saco,anda a cagar,apestar,bastardo,basura,bicho,burro,cabron,cabrón,cacorro,cagar,calientapollas,capullo,cara de culo,cara de monda,carajo,chancla (México),chapero,chichi,chimba,chingar,chocho,chúpame la pija,chúpame la polla,chúpamedias,chúpamela,chupar,cipote,coger,cojonazos,cojones,come mierda y muere,coño,culiao,culo,de puta madre,estúpido,follar,forro,gilipollas,gonorrea,guarra,hijo de perra,hijo de puta,hijueputa,hostia,huevo,huevón,huevos,idiota,imbécil,jode,joder,joder,jódete,joto,la concha de tu madre,la hostia,la madre que te parió,lame botas,loco,los cojones,maldito,malparido,mamahuevo,mamón,marica,maricon,maricón,mariconazo,mariquita,me cago en la hostia,me cago en ti,métetelo por el culo,mierda,mongolo,nabo,no me jodas,no me jodás,no seas gilipollas,pajero,payaso,pelos de los huevos,pelotas,pelotudo,pendejo,percanta,perro,pichacorta,pinche,piruja,polla,pollas en vinagre,puta,Puta madre,puto,qué cabrón,que te den,que te jodan,qué te jodan,rabo,raja,soplapollas,tarado,tonto,tonto del culo,tontopollas,trompada,un putero,verga,vete a la mierda,vete a la verga,vete al demonio,vete al infierno,zorra,zunga`;
// Tokenize the words, splitting at comma
export const badSpanishWords = new Set(words.split(","));

View File

@ -1,11 +1,12 @@
{
"compilerOptions": {
"target": "ES2020",
"outDir": "./dist",
"target": "ES2021",
"module": "ESNext",
"esModuleInterop": true,
"strict": true,
"moduleResolution": "node",
"noEmit": true
"declaration": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts"]