npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

better_profanity_ts

v1.2.1

Published

censor dirty words

Downloads

14

Readme

better_profanity

Blazingly fast cleaning swear words (and their leetspeak) in strings

Currently, the library has set up default filtering for profanity in both English and Vietnamese languages

version license

Inspired from package profanity of Ben Friedland, this library is significantly faster than the original one, by using string comparison instead of regex.

Inspired by a package better_profanity of Son Thanh Nguyen, This library is a version designed for Node.js.

It supports modified spellings (such as p0rn, h4NDjob, handj0b and b*tCh).

Installation

npm i -D better_profanity_ts

Unicode characters

Only Unicode characters from categories Ll, Lu, Mc and Mn are added. More on Unicode categories can be found here.

Not all languages are supported yet, such as Chinese.

Usage

import { Profanity } from 'better_profanity_ts/lib/better_profanity'

async function main() {
  const profanity = new Profanity()
  const text = "You p1ec3 of sHit. Ban nhu con c4c"
  console.log(profanity.censor(text))
 # You p1ec3 of ****. Ban nhu con ****
}

All modified spellings of words in profanity_wordlist.txt will be generated. For example, the word handjob would be loaded into:

'handjob', 'handj*b', 'handj0b', 'handj@b', 'h@ndjob', 'h@ndj*b', 'h@ndj0b', 'h@ndj@b',
'h*ndjob', 'h*ndj*b', 'h*ndj0b', 'h*ndj@b', 'h4ndjob', 'h4ndj*b', 'h4ndj0b', 'h4ndj@b'

The full mapping of the library can be found in better_profanity.ts.

1. Censor swear words from a text

By default, profanity replaces each swear words with 4 asterisks ****.

import { Profanity } from 'better_profanity_ts/lib/better_profanity'

async function main() {
    const profanity = new Profanity()
    const text = "You p1ec3 of sHit. Ban nhu con c4c"
    console.log(profanity.censor(text))
    # You p1ec3 of ****. Ban nhu con ****
}

2. Censor doesn't care about word dividers

The function .censor() also hides words separated not just by an empty space but also other dividers, such as _, , and .. Except for @, $, *, ", '.

import { Profanity } from 'better_profanity_ts/lib/better_profanity'

async function main() {
    const profanity = new Profanity()
    const text = "...sh1t...hello_cat_fuck,,,,123"
    console.log(profanity.censor(text))
    # ...****...hello_cat_****,,,,123
}

3. Censor swear words with custom character

4 instances of the character in the second parameter in .censor() will be used to replace the swear words.

import { Profanity } from 'better_profanity_ts/lib/better_profanity'

async function main() {
    const profanity = new Profanity()
    const text = "...sh1t...hello_cat_fuck,,,,123"
    console.log(profanity.censor(text, '-'))
    # ...----...hello_cat_----,,,,123
}

4. Check if the string contains any swear words

Function .containsProfanity() return True if any words in the given string has a word existing in the wordlist.

import { Profanity } from 'better_profanity_ts/lib/better_profanity'

async function main() {
    const profanity = new Profanity()
    const text = "...sh1t...hello_cat_fuck,,,,123"
    console.log(profanity.containsProfanity(text))
    # true
}

5. Censor swear words with a custom wordlist

5.1. Wordlist as a List

Function loadCensorWords takes a List of strings as censored words. The provided list will replace the default wordlist.

import { Profanity } from 'better_profanity_ts/lib/better_profanity'

async function main() {
    const profanity = new Profanity()
    const customBadwords: string[] = ['happy', 'jolly', 'merry'];
    profanity.loadCensorWords(customBadwords);
    console.log(profanity.censor("Have a merry day!"));
    # **** a **** day
}

5.2. Wordlist as a file

Function `loadCensorWordsFromFile takes a filename, which is a text file and each word is separated by lines.

import { Profanity } from 'better_profanity_ts/lib/better_profanity'

async function main() {
    const profanity = new Profanity()
    profanity.loadCensorWordsFromFile('/path/to/my/project/my_wordlist.txt');
    console.log(profanity.censor("Have a merry day!"));
    # **** a **** day
}

6. Whitelist

Functions loadCensorWords and loadCensorWordsFromFile take a keyword argument whitelistWords to ignore words in a wordlist.

It is best used when there are only a few words that you would like to ignore in the wordlist.

# Use the default wordlist
const options = { whitelistWords: ['happy', 'merry'] };
profanity.loadCensorWords([], options)

# or with your custom words as a List
const customBadWords: string[] = ['happy', 'jolly', 'merry']
profanity.loadCensorWords(custom_badwords, options)

# or with your custom words as a text file
profanity.loadCensorWordsFromFile('/path/to/my/project/my_wordlist.txt', options)

7. Add more censored words

import { Profanity } from 'better_profanity_ts/lib/better_profanity'

async function main() {
    const profanity = new Profanity()
    const customBadWords: string[] = ['happy', 'jolly', 'merry']
    profanity.addCensorWords(customBadWords);
    console.log(profanity.censor("Happy you, fuck!"));
    # **** you, ****!
}

Contributing

Please read for details on our code, and the process for submitting pull requests to us.

License

This project is licensed under the MIT License - see the LICENSE file for details