Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

obscenity

jo3-l154kMIT0.4.3TypeScript support: included

Robust, extensible profanity filter.

profanity, profane, obscenities, obscenity, obscene, filter, curse, swear, swearing, vulgar, vulgarity, bad-words, badwords, cuss, cussing

readme

Obscenity

Robust, extensible profanity filter for NodeJS.

Build status Codecov status npm version Language License

Why Obscenity?

  • Accurate: Though Obscenity is far from perfect (as with all profanity filters), it makes reducing false positives as simple as possible: adding whitelisted phrases is as easy as adding a new string to an array, and using word boundaries is equally simple.
  • Robust: Obscenity's transformer-based design allows it to match on variants of phrases other libraries are typically unable to, e.g. fuuuuuuuckkk, ʃṳ𝒸𝗄, wordsbeforefuckandafter and so on. There's no need to manually write out all the variants either: just adding the pattern fuck will match all of the cases above by default.
  • Extensible: With Obscenity, you aren't locked into anything - removing phrases that you don't agree with from the default set of words is trivial, as is disabling any transformations you don't like (perhaps you feel that leet-speak decoding is too error-prone for you).

Installation

$ npm install obscenity
$ yarn add obscenity
$ pnpm add obscenity

Example usage

First, import Obscenity:

const {
    RegExpMatcher,
    TextCensor,
    englishDataset,
    englishRecommendedTransformers,
} = require('obscenity');

Or, in TypeScript/ESM:

import {
    RegExpMatcher,
    TextCensor,
    englishDataset,
    englishRecommendedTransformers,
} from 'obscenity';

Now, we can create a new matcher using the English preset.

const matcher = new RegExpMatcher({
    ...englishDataset.build(),
    ...englishRecommendedTransformers,
});

Now, we can use our matcher to search for profanities in the text. Here's two examples of what you can do:

Check if there are any matches in some text:

if (matcher.hasMatch('fuck you')) {
    console.log('The input text contains profanities.');
}
// The input text contains profanities.

Output the positions of all matches along with the original word used:

// Pass "true" as the "sorted" parameter so the matches are sorted by their position.
const matches = matcher.getAllMatches('ʃ𝐟ʃὗƈk ỹоứ 𝔟ⁱẗ𝙘ɦ', true);
for (const match of matches) {
    const { phraseMetadata, startIndex, endIndex } =
        englishDataset.getPayloadWithPhraseMetadata(match);
    console.log(
        `Match for word ${phraseMetadata.originalWord} found between ${startIndex} and ${endIndex}.`,
    );
}
// Match for word fuck found between 0 and 6.
// Match for word bitch found between 12 and 18.

Censoring matched text:

To censor text, we'll need to import another class: the TextCensor. Some other imports and creation of the matcher have been elided for simplicity.

const { TextCensor, ... } = require('obscenity');
// ...
const censor = new TextCensor();
const input = 'fuck you little bitch';
const matches = matcher.getAllMatches(input);
console.log(censor.applyTo(input, matches));
// %@$% you little **%@%

This is just a small slice of what Obscenity can do: for more, check out the documentation.

Accuracy

Note: As with all swear filters, Obscenity is not perfect (nor will it ever be). Use its output as a heuristic, and not as the sole judge of whether some content is appropriate or not.

With the English preset, Obscenity (correctly) finds matches in all of the following texts:

  • you are a little fucker
  • fk you
  • ffuk you
  • i like a$$es
  • ʃ𝐟ʃὗƈk ỹоứ

...and it does not match on the following:

  • the pen is mightier than the sword
  • i love bananas so yeah
  • this song seems really banal
  • grapes are really yummy

Documentation

For a step-by-step guide on how to use Obscenity, check out the guide.

Otherwise, refer to the auto-generated API documentation.

Contributing

Issues can be reported using the issue tracker. If you'd like to submit a pull request, please read the contribution guide first.

Author

Obscenity © Joe L. under the MIT license. Authored and maintained by Joe L.

GitHub @jo3-l

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

0.4.3 (2025-01-26)

Bug Fixes

  • preset/english: match 'shit' at end of word (0299b49), closes #47

0.4.2 (2025-01-18)

Features

  • add more characters to leet transformer (#78) (fa673e6)

Bug Fixes

  • censor: don't generate the same character twice in a row (#85) (58f2715), closes #82
  • preset/english: add word boundary to 'shit' pattern (9554e7c), closes #93
  • preset/english: whitelist "fick" (#88) (40f66fb)

0.4.1 (2024-12-03)

Bug Fixes

  • preset/english: add "fickle" to whitelist (#87) (da754da)
  • preset/english: remove erroneous patterns for dick (e43d502), closes #86

0.4.0 (2024-08-02)

⚠ BREAKING CHANGES

  • regexp-matcher: Passing an empty whitelisted term to the RegExpMatcher will result in a runtime error.

This was unsupported previously and likely did not work correctly. Make it a real error.

Bug Fixes

  • regexp-matcher: advance index correctly in whitelist matcher (ebf95ad), closes #49
  • regexp-matcher: correctly remap to original indices in all cases (3a49579), closes #71
  • regexp-matcher: reject empty whitelist terms (9a46113)

0.3.1 (2024-07-17)

0.3.0 (2024-07-17)

⚠ BREAKING CHANGES

  • The library no longer exports a version constant.

  • drop version constant (2810674)

0.2.2 (2024-07-17)

Features

  • english-preset: add more blacklisted terms (#50) (4653de5)

Bug Fixes

  • english-preset: whitelist 'kung-fu' (d60b4f4), closes #67

0.2.1 (2024-03-03)

Features

  • english-preset: add more blacklisted terms (#50) (c189595)

0.2.0 (2024-01-05)

⚠ BREAKING CHANGES

  • english-preset: Using the default English preset, Obscenity will no longer strip non-alphabetic characters from the input text before matching.

This addresses a class of egregious false negatives in previous versions (see #23), but introduces a regression where cases such as 'f u c k' (with the space) will no longer be detected by default. We expect to provide a more comprehensive fix in the next minor release.

If desired, it remains possible to revert to the previous behavior by providing a custom set of transformers to the matcher.

  • matchers: The NfaMatcher class has been removed. Use the RegExpMatcher instead.

Features

  • english-preset: blacklist 'shit' by default (b0d90aa), closes #47

Bug Fixes

  • english-preset: don't include skip-non-alphabetic transformer (620c721), closes #23 #46
  • english-preset: remove extraneous patterns for n-word (e135be5), closes #48
  • pkg: ensure types resolve in ESM (718da8a), closes #44
  • matchers: remove NfaMatcher (b69c21d)

0.1.4 (2023-06-06)

Bug Fixes

  • matchers: gracefully handle empty patterns (#31) (79cfa63)

0.1.1, 0.1.2, 0.1.3

Versions skipped due to temporary issue with release workflow.

0.1.0 (2021-08-27)

Initial release.