Giter Club home page Giter Club logo

no-profanity's Introduction

no-profanity

A JavaScript package to detect and filter profanity. Yes this code, contain profanity. But what do you expect from a no-profanity package.

Installation

npm i no-profanity

Credit

Thanks to bad-words for the original package. This package is a rewrite of that package, with some extra features and a lot of performance improvements.

Thanks to google-profanity-words for providing the list of profanities.

Usage

Using the no-profanity package is very simple. You can use it to detect profanity, or to filter profanity from a string. There are some basic options as well, such as overriding the placeholder character, or adding/removing words from the filterlist.

Check if a string contains profanity

A basic sample showing a simple checker

import { isProfane } from 'no-profanity';
console.log(isProfane("Don't be an asshole")); // true
console.log(isProfane("This is a nice text")); // false

Replace profanity in a string

A basic sample showing how to replace profanities

import { replaceProfanities } from 'no-profanity';
console.log(replaceProfanities("Don't be an asshole")); // Don't be an *******
console.log(replaceProfanities("This is a nice text")); // This is a nice text

Get profanities from a string

A basic sample returning the profanities

console.log(containsProfanities("what an asshole"));

Will return:

[ { 
    word: 'asshole', 
    index: 8 
} ]

Get all profanities

A basic sample returning all profanities. It will take all options into account, such as excludes and includes. This can be useful in running tests, such as in the file test/list.test.js.

import { getProfanities } from 'no-profanity';
const options = {}; // add your options here
console.log(getProfanities({options: options}));

Options

There are some overrides possible, but as soon as you want to use an override you will no longer be able pass a string to the functions defined above, but instead, they require an arguments object, like this:

replaceProfanities({
    testString: "testable string",
    options: {}
});

Override placeholder

The options object should contain a property called replacement which should be a string of length 1, unless you want a longer replacement value as the original wordlength. The default value is *.

replaceProfanities({
    testString: "testable string",
    options: {
        replacement: '#'
    }
});

Remove words from the filterlist

The options object should contain a property called excludes which should be an array of strings you don't want to filter on.

replaceProfanities({
    testString: "testable string",
    options: {
        excludes: ['testable']
    }
});

You can also remove all words from the filter list so you can start your own, using the emptyList property.

replaceProfanities({
    testString: "testable string",
    options: {
        emptyList: true
    }
});

Add words to the filterlist

The options object should contain a property called includes which should be an array of strings you want to filter on.

replaceProfanities({
    testString: "testable string",
    options: {
        includes: ['testable']
    }
});

Pre-sanitize the string

The options object has the option to contain a regex pattern to sanitize the string before checking for profanities.

replaceProfanities({
    testString: "testable string",
    options: {
        preSanitize: /[^a-zA-Z0-9]/g
    }
});

All matches will be replaced with an empty string. If you want to change the replacement for preSanitize, you can use the preSanitizeReplacement property.

replaceProfanities({
    testString: "tabs are the best",
    options: {
        preSanitizeReplacement: "spaces",
        preSanitize: /\btabs\b/,
    }
});

bad-words package

This package is the replacement for the bad-words package which is outdated and slow. According to a handful of benchmarks, this package is about 150 times as fast.

See also this page for more information and the migration guide.

Most options from the bad-words package are usable in the options object to be used in this package. However, the replaceRegex option is not supported.

Contributing

Any contributions are highly appreciated. If you want to contribute, please fork the repository and create a pull request. If you have any questions, feel free to create an issue.

License

This project is licensed under the MIT License - see the LICENSE file for details. With MIT comes the freedom to use the code for whatever you want, but a credit would be appreciated.

no-profanity's People

Contributors

topener avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

humanornot-so

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.