![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
@2toad/profanity
Advanced tools
A JavaScript profanity filter (with TypeScript support)
Install the package
npm i @2toad/profanity
If you're using Node 11.x or older, you'll need to install Profanity 1.x
import { profanity, CensorType } from '@2toad/profanity';
// or
const { profanity, CensorType } = require('@2toad/profanity');
profanity.exists('I like big butts and I cannot lie');
// true
profanity.exists('I like big glutes and I cannot lie');
// false
profanity.censor('I like big butts (aka arses) and I cannot lie');
// I like big @#$%&! (aka @#$%&!) and I cannot lie
profanity.censor('I like big butts (aka arses) and I cannot lie', CensorType.FirstChar);
// I like big *utts (aka *rses) and I cannot lie
Create an instance of the Profanity class to change the default options:
import { Profanity } from '@2toad/profanity';
const profanity = new Profanity({
wholeWord: false,
grawlix: '*****',
grawlixChar: '$',
});
By default, this is set to true
so profanity only matches on whole words:
profanity.exists('Arsenic is poisonous but not profane');
// false
Setting this to false
, results in partial word matches:
profanity.exists('Arsenic is poisonous but not profane');
// true (matched on arse)
Profanity detection works on parts of compound words, rather than treating hyphenated or underscore-separated words as indivisible.
When wholeWord
is true
, each portion of a compound word is analyzed for a match:
profanity.exists("Don't be an arsenic-monster");
// false
profanity.exists("Don't be an arse-monster");
// true (matched on arse)
Setting wholeWord
to false
, results in partial word matches on each portion of a compound word:
profanity.exists("Don't be an arsenic-monster");
// true (matched on arse)
By default this is set to @#$%&!
:
profanity.censor('I like big butts and I cannot lie');
// I like big @#$%&! and I cannot lie
Setting this to ****
, results in:
profanity.censor('I like big butts and I cannot lie');
// I like big **** and I cannot lie
When specifying a CensorType
other than CensorType.Word
, this is the character used by the censor
function.
By default this is set to *
:
profanity.censor('I like big butts and I cannot lie', CensorType.AllVowels);
// I like big b*tts and I cannot lie
Setting this to $
, results in:
profanity.censor('I like big butts and I cannot lie', CensorType.AllVowels);
// I like big b$tts and I cannot lie
Add words:
profanity.addWords(['aardvark', 'zebra']);
Remove words:
profanity.removeWords(['butt', 'arse']);
The whitelist allows you to specify words that are always ignored by the profanity filter.
This can be useful if you want to enable partial word matching (
wholeWord = false
), so combined words are caught (e.g., arselicker), while specific words you add to the whitelist are ignored (e.g., arsenic).
Add words to the whitelist:
profanity.whitelist.addWords(['arsenic', 'buttress']);
Remove words from the whitelist:
profanity.whitelist.removeWords(['arsenic', 'buttress']);
To see how Profanity performs, check out our benchmark results.
So you want to contribute to the Profanity project? Fantastic! Please read the Contribute doc to get started.
FAQs
A multi-language profanity filter with full TypeScript support
The npm package @2toad/profanity receives a total of 4,913 weekly downloads. As such, @2toad/profanity popularity was classified as popular.
We found that @2toad/profanity demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.