Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clean function to remove any bad words with characters or spaces from a string #109

Open
wants to merge 12 commits into
base: next
Choose a base branch
from
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ results

npm-debug.log
node_modules

.idea
28 changes: 23 additions & 5 deletions lib/badwords.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,29 @@ class Filter {
* @param {string} string - Sentence to filter.
*/
clean(string) {
if (!this.splitRegex.exec(string)) return string;
return string.split(this.splitRegex).map((word) => {
return this.isProfane(word) ? this.replaceWord(word) : word;
}).join(this.splitRegex.exec(string)[0]);
}
const profaneWords = this.list.filter((word) => {
const wordExp = new RegExp(`\\b${word.replace(/(\W)/g, '\\$1')}\\b`, 'gi');
return !this.exclude.includes(word.toLowerCase()) && wordExp.test(string);
});

if (profaneWords.length > 0) {
let cleanedString = string;

profaneWords.forEach(profaneWord => {
cleanedString = cleanedString.toLowerCase().replace(profaneWord.toLowerCase(), this.placeHolder.repeat(4));
});

return cleanedString;
}

return string;

// if (!this.splitRegex.exec(string)) return string;
// return string.split(this.splitRegex).map((word) => {
// return this.isProfane(word) ? this.replaceWord(word) : word;
// }).join(this.splitRegex.exec(string)[0]);

}

/**
* Add word(s) to blacklist filter / remove words from whitelist filter
Expand Down