From cddf02154f94fab3b3c168bcf1bdb3f342d391c3 Mon Sep 17 00:00:00 2001 From: Alessandro Buonocore <60114301+bncal@users.noreply.github.com> Date: Tue, 14 Mar 2023 18:06:42 +0100 Subject: [PATCH] refactored --- src/helpers/banned-words.json | 3 +- src/helpers/functions.ts | 109 ++++++++++++++-------------------- src/index.ts | 30 +++------- 3 files changed, 57 insertions(+), 85 deletions(-) diff --git a/src/helpers/banned-words.json b/src/helpers/banned-words.json index 892967a..c2e6137 100644 --- a/src/helpers/banned-words.json +++ b/src/helpers/banned-words.json @@ -1,5 +1,6 @@ { "words": [ - "" + "badword", + "badwording" ] } \ No newline at end of file diff --git a/src/helpers/functions.ts b/src/helpers/functions.ts index 33b5440..9a94dc2 100644 --- a/src/helpers/functions.ts +++ b/src/helpers/functions.ts @@ -1,88 +1,71 @@ import bannedWords from "./banned-words.json"; /** - * @description This function is useful when you need to ** - * @param word word to be checked if it is banned - * @param censor (optional) string to replace the banned word with. Default is "*****" - * @returns censor if word is banned, otherwise returns word. + * @description hasBannedWords checks if a word or a long text contains banned words. + * @param text text to be checked if it contains banned words + * @returns boolean * - * @example console.log(hideBannedWord("badword")); - * Output: ***** - * console.log(hideBannedWord("badword", "@#!")); - * Output: @#! - * Output: This word is banned. + * @example hasBannedWords("Badword"); + * Output: true */ -const hideBannedWord = (word: string, censor: string = "*****"): string => { +const hasBannedWords = (text: string): boolean =>{ + if(text.length == 1) { + + if(bannedWords.words.includes(text)){ + return true; + } + } + + if(text.length > 1) { + const words: string[] = text.split(" "); - if(isWordBanned(word)) { - return censor; + for(let i = 0; i < words.length; i++) { + if(bannedWords.words.includes(words[i])) { + return true; + } + } } - return word; + return false; } /** - * @description This function is useful when you need to sanitize a very long text. - * @param text text to be sanitized + * @description redactBannedWords redacts banned words in a text and/or long text. + * @param text text to be redacted * @param censor (optional) string to replace the banned word with. Default is "*****" * @returns text with banned words replaced with censor. * - * @example const vulgarSentence: string = sanitizeText("Leave this badword outta my badwording face!"); + * @example redactBannedWords("Leave this badword outta my badwording face!"); * Output: Leave this ***** outta my ***** face! - * const customVulgarSentence: string = sanitizeText("Leave this badword outta my badwording face!", "bobba"); - * Output: Leave this bobba outta my bobba face! - * - */ -const sanitizeText = (text: string, censor: string = "*****"): string => { - - const words: string[] = text.split(" "); - - words.forEach((word, i) => { - if (isWordBanned(word)) { - words[i] = censor; - } - }); - - return words.join(" "); - -} - -/** - * @description This function is useful when you need to check if a long text contains a banned word but you don't want to edit it. - * @param text text to be checked - * @returns array of indexes of banned words in the text. * - * @example (doesTextContainBannedWord("Leave this badword outta my face!") ? console.log("It does") : console.log("It does not")); - * Output: It does. - * (doesTextContainBannedWord("Leave this thing outta my face!") ? console.log("It does") : console.log("It does not")); - * Output: It does not. + * @example redactBannedWords("Leave this badword outta my badwording face!", "[REDACTED]"); + * Output: Leave this [REDACTED] outta my [REDACTED] face! * */ -const doesTextContainBannedWord = (text: string): number[] => { +const redactBannedWords = (text: string, censor: string = "******"): string => { + if(text.length == 1) { - const words: string[] = text.split(" "); - const iBannedWords: number[] = []; - - words.forEach((word, i) => { - if (isWordBanned(word)) { - iBannedWords.push(i); + if(bannedWords.words.includes(text)){ + return censor; } - }); + } - return iBannedWords; -} + if(text.length > 1) { + const words: string[] = text.split(" "); -/** - * @description This function is useful when you need to check if a word is banned. - * @param word word to be checked if it is banned - * @returns true if word is banned, otherwise returns false. - * - * @example (isWordBanned("badword") ? console.log("This word is banned") : console.log("This word is not banned")); - * Output: This word is banned. - */ -const isWordBanned = (word: string): boolean => { + words.forEach((word, i) => { + if (hasBannedWords(word)) { + words[i] = censor; + } + }); + + return words.join(" "); + } - return bannedWords.words.includes(word); + return text; } -export {isWordBanned, hideBannedWord, doesTextContainBannedWord, sanitizeText}; +export { + hasBannedWords, + redactBannedWords +}; \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 5e25abd..67e392b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,25 +1,13 @@ -import {isWordBanned, hideBannedWord, doesTextContainBannedWord, sanitizeText} from "./helpers/functions"; +import {hasBannedWords, redactBannedWords} from "./helpers/functions"; +console.log(redactBannedWords("Leave this badword outta my badwording face!")); +// Output: Leave this ***** outta my ***** face! -(isWordBanned("shit") ? console.log("This word is banned") : console.log("This word is not banned")); -// Output: This word is banned. -(isWordBanned("thing") ? console.log("This word is banned") : console.log("This word is not banned")); -// Output: This word is not banned. +console.log(redactBannedWords("Leave this badword outta my badwording face!", "[REDACTED]")); +// Output: Leave this [REDACTED] outta my [REDACTED] face! -console.log(hideBannedWord("shit")); -// Output: ***** -console.log(hideBannedWord("shit", "@#!")); -// Output: @#! +console.log(hasBannedWords("Badword")); +// Output: true -(doesTextContainBannedWord("Leave this shit outta my face!") ? console.log("It does") : console.log("It does not")); -// Output: It does. -(doesTextContainBannedWord("Leave this thing outta my face!") ? console.log("It does") : console.log("It does not")); -// Output: It does not. - -const vulgarSentence: string = sanitizeText("Leave this shit outta my fucking face!"); -console.log(vulgarSentence); -// Output: Leave this ***** outta my ***** face! - -const customVulgarSentence: string = sanitizeText("Leave this shit outta my fucking face!", "bobba"); -console.log(customVulgarSentence); -// Output: Leave this bobba outta my bobba face! +console.log(hasBannedWords("Hello world!")); +// Output: false \ No newline at end of file