README
Profanity Light
A lightweight and highly customizable profanity cheker. No dictionary is included to reduce the bundle size, and you can add you dictionary fo words with its set of rules!
Features
- โ๏ธ Lightweight. No dictionary included to keep the bundle size limited ~1kb!
- โ๏ธ Customizable: each language is different from another so the rule to find bad words. Customize your dictionary to be efficient. Not everyone speaks English ๐
- ๐ Batteries included. Sanitize a word changing each letter or the whole word. Pass your custom replacer. Define any alternative symbol to a letter ( e = e,3,&,รฉ,รจ...and so on)
- ๐ Overwrite the single option on each call without the needs to re-instantiate the filter!
- I mentioned it's written in Typescript?!
Install
yarn add profanity-light
or
npm i profanity-light
Usage
Profanity light comes with an empty dictionary. After you instantiate a filter you can add words to the default dictionary or add one or more dictionaries. Each dictionary comes with its own set of rules. For the moment let's see how to use the default dictionary
import filterFactory from 'profanity-light';
// Instantiate a filter
const filter = filterFactory();
// This filter has an empty dictionary included. Let's add some word
filter.addWords(['flower']);
// Use check to know if a text contains any forbidden word
filter.check('A flower is just a flower');
// the above returns true
Adding words: a deep dive
The words you add are checked with a case-insensitive alghoritm, so don't bother to add the upper-case variant. Each word can have many declination like plurals or masculin/feminine. To cover all the cases you can consider that you can add little regural expressions parts in your word
filter.addWords(['flowers?']);
// In this case both the singular and plural version of the word are found
filter.check('a flower'); // true
filter.check('two flowers'); // true
Another example in Italian
filter.addWords(['ca(ne|ni|gn.+)']);
filter.check('il cane mi morse'); // true
filter.check('i cani abbaiarono'); // true
filter.check('le cagne ebbero dei cuccioli'); // true
filter.check('il cagnolone era immenso'); // true
You can add a word that contains a space:
filter.addWords(['sun flower']);
filter.check('the sun flower is yellow'); // true
or arbitrary spaces
filter.addWords(['sun\\s+flower']);
filter.check('the sun flower is yellow'); // true
This very expressive way of defining words will cover most of the cases. Then remember that people are smart... they will find a way ๐
Symbol replacement
Usually a word can be spelled with different characters to overcame a limitation. For example flower
can be spelled fl0wer
. Each dictionary, included the standard one, has a set of symbol replacements
o = 0
a = 4 @
s = $
e = e รฉ รจ 3 โฌ &
... and so on
Don't worry to define all the possible characters when defining a word. For example the word flowers?
will be matched also against fl0wer
and fl0wer$
!
Each dictionary can have a different set of symbol replacement, but we'll see this later when we'll add custom dictionaries.
NOTE: If a symbol has a meaning in a regexp, it must be escaped, i.e. '\