-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
55e4a69
commit cddf021
Showing
3 changed files
with
57 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"words": [ | ||
"" | ||
"badword", | ||
"badwording" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |