1.3.0 • Published 2 years ago

badwords-filter v1.3.0

Weekly downloads
69
License
MIT
Repository
github
Last release
2 years ago

npm npm install size

NPM

badwords-filter

Installation

npm i -s badwords-filter

An easy-to-use word filter with advanced detection techniques. A lightweight package with zero dependencies.

Features

  • no case-sensitivity
  • detects L33t text
  • detects accented characters
  • detects extra/missing repeated characters
  • works with regex strings or normal strings

Usage

const Filter = require("badwords-filter");
const config = {
	list: ["test", "hello"],
	cleanWith: "$",
	useRegex: false,
};
const filter = new Filter(config);

Configuration options for filter

PropertyTypeDefaultDescription
listArrayen.json filtersetArray of filters in string format
cleanWithString"*"Character or array of strings to replace bad words with in clean function
useRegexbooleanfalseOption to convert strings in list to regex expressions

Functions

FunctionParametersReturnsDescription
normalizeString message to normalizeString normalized messageconverts to lowercase, normalizes accented characters, converts l33t text to normal text, removes excess non-alphabetical characters (automatically used in all package functions)
isUncleanString message to check for cleanlinessBoolean true if contains any filtered wordparses message for any filtered words
cleanString message to cleanString cleaned messagereplaces all filtered words with cleanWith character or a random string
getUncleanWordIndexesString message to parseArray <number> indexes of words that contain filtered wordsgets indexes of all filtered words
isWordUncleanString word to checkBoolean true if word is detected as a filtered wordchecks if a word is filtered
debugString message to testundefinedprints to console the outputs of all functions on the given string

Example Detection

const Filter = require("badwords-filter");
const config = { list: ["hello"] };
const filter = new Filter(config);

//All the following would return true
filter.isUnclean("hello");
filter.isUnclean("HeLlO");
filter.isUnclean("h3ll0");
filter.isUnclean("heeeellloooo");
filter.isUnclean("heeeeellllooooooo there!!!");
filter.isUnclean("héllo");
filter.isUnclean("h.#ell-o");

Examples

Using a custom filter list

Normal strings filter

const Filter = require("badwords-filter");
const filter = new Filter({ list: ["badword"] });
filter.isUnclean("This sentence contains 'badword'"); // true
filter.isUnclean("This sentence does not contain any nasty words"); // false
filter.clean("This sentence contains 'badword'"); // "This sentence contains *********"
filter.getUncleanWordIndexes("This sentence contains 'badword'"); //[3]
filter.getUncleanWordIndexes("baaadword, goodword, okayword, badword"); // [0,3]
filter.isUnclean("baaaaaadw0rd"); //true

Regex strings filter

const Filter = require("badwords-filter");
const filter = new Filter({
	list: ["b.+d"], // any word that stars with b and ends with d
	useRegex: true,
});
filter.isUnclean("marching band"); // true
filter.clean("marching band"); // "marching ****"

Future Features

  • support with phrases
  • detects words with whitespace seperation
  • more efficiency optimization
  • sensitivity option for detection