diff --git a/package.json b/package.json index 20b3777..e4c612c 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,15 @@ { "name": "family-friendly", "version": "1.0.0", - "description": "", - "main": "dist/app.js", + "description": "Detect or mask out, bad words in a string", + "main": "dist/FamilyFriendly.js", "engines": { "node": "18.x" }, "scripts": { "build": "babel src -d dist --extensions \".ts,.tsx\"", - "start": "node dist/app.js", - "dev": "nodemon src/app.ts", + "start": "node dist/FamilyFriendly.js", + "dev": "nodemon src/FamilyFriendly.ts", "test": "jest", "test:watch": "jest --watch", "lint": "eslint . --ext .ts", diff --git a/src/FamilyFriendly.ts b/src/FamilyFriendly.ts new file mode 100644 index 0000000..74adb13 --- /dev/null +++ b/src/FamilyFriendly.ts @@ -0,0 +1,115 @@ +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 = new Set(); + + constructor(options?: FamilyFriendlyOptions) { + 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); + } + + this.badWords = new Set(allWords); + } + + // 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.replaceAll(badWord, maskAs.repeat(badWord.length)); + } + return str; + } +} diff --git a/src/app.ts b/src/app.ts deleted file mode 100644 index e69de29..0000000