A filter of swear words.
npm i profanity-js
This module exports a constructor function which takes an inputStr
string and config
object, to creates a Profanity
instance.
Profanity(inputStr, config)
Creates a new Profanity
instance.
-
inputStr
- Optional - A plain JavaScript string containing the value to be filtered. -
config
- Optional - A plain JavaScript object containing configuration options.
- Input string to evaluate if contains profanity.
Profanity configurations.
saveOriginal
- Define if the original input string will be saved.enabled
- Define if the filter will be enabled.placeHolder
- Character used to replace profane words.replaceRegex
- Regular expression used to replace profane words with placeHolder.separatorRegex
- Regular expression used to split a string into words.excludeWords
- List of words to be ignored when filter profane words.wordsList
- List of words to be override the default dictionary of profane words.language
- Language used to filter profane texts.
A Profanity
instance.
Every resource is accessed via your Profanity
instance:
.censor()
- Evaluate if string is profanity and return an edited version..isProfane()
- Evaluate if a string is a profane language..censureWord()
- Censure a word with placeHolder characters..loadOriginal()
- Return original text if config.saveOrigial as true..addWords()
- Add word(s) to wordlist filter..removeWords()
- Remove word(s) from wordlist filter.
const Profanity = require('profanity-js');
By default, profanity
replaces each swear words with the string length with asterisk *
.
const profanity = new Profanity();
console.log(profanity.isProfane("merda"));
// log: true
let censoredText = profanity.censor("isso é uma merda");
console.log(censoredText);
// log: isso é uma *****
In .censor()
asterisks in will be used to replace the swear words.
let censoredText = profanity.censor("You piece of sH1t");
console.log(censoredText);
// log: You piece of ****
Function .isProfane()
return True
if any words in the given string has a word existing in the wordlist.
let dirtyText = "That l3sbi4n did a very good H4ndjob.";
console.log(profanity.isProfane(dirtyText));
// log: true
let customBadwords = ['happy', 'sometext'];
profanity.addWords(...customBadwords);
console.log(profanity.isProfane("Happy day bro!"));
// log: true
let removeBadwords = ['asshole'];
profanity.removeWords(...removeBadwords);
console.log(profanity.isProfane("Don't be an asshole"));
// log: false
Function .isProfane()
return True
if any words in the given string has a word existing in the wordlist.
let dirtyText = "That l3sbi4n did a very good H4ndjob.";
console.log(profanity.isProfane(dirtyText));
// log: true
The lib supports two languages they are Brazilian Portuguese and English, the defaults are Portuguese and to change this, see below.
let config = {
language: "en-us"
};
let dirtyText = "fuck this shit";
const profanity = new Profanity(dirtyText, config)
console.log(profanity.censor());
// log: **** this ****
The
config
Object will be overwritten the default values.
To use a custom placeHolder, just overwritten the default value with a config Object.
let config = {
placeHolder: "-"
};
let dirtyText = "fuck this shit";
const profanity = new Profanity(dirtyText, config)
console.log(profanity.censor());
// log: ---- this ----
To overrides the default profane dictionary, just set the new Array
of words in config Object.
let config = {
wordsList: ['shit', 'fuck', 'this']
};
let dirtyText = "fuck this shit";
const profanity = new Profanity(dirtyText, config)
console.log(profanity.censor());
// log: **** **** *****
npm run test
or
yarn test
This project is licensed under the MIT License - see the LICENSE.md file for details.
If you want to contribute to a project and make it better, feel free to fork and contribute.
- youngkaneda - For help and support with the Regex expressions and make this code better.
Made with ❤ by Rogério Araújo