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

curse-filter

v5.2.4

Published

JavaScript/Typescript multi-language curse word filter

Downloads

340

Readme

curse-filter

curse-filter is a fully-equipped Node.js library that simplifies profanity filtering in 15+ different languages.

npm version install size GitHub GitHub last commit

Installation

npm install curse-filter

Loading the module

ESM / Typescript

This module is first thought for Typescript/ESM, so this is the recommended way to load it.

import { filter } from 'curse-filter';

Usage

supportedLangs

import { supportedLangs } from 'curse-filter';

console.log(supportedLangs); // This will log an array of supported languages

filter()

The filter() function will return a string with all curse words replaced with asterisks ("***").

import { filter } from 'curse-filter';

filter('fuck you'); // '*** you'
filter('fuck you', 'en'); // '*** you'
filter('fuck you, coglione', ['en', 'it']); // '*** you, ***'
filter('fuck you, coglione', ['en', 'it'], 'customPlaceholder'); // 'customPlaceholder you, customPlaceholder'

Alternately, if you want to reduce filtering time, you can pass as second parameter a string of an array of string (all supported languages).

import { filter } from 'curse-filter';

const str = '<...>';

filter(str); // automatically selects all languages
filter(str, 'en');
filter(str, ['en', 'es', 'fr', 'it']);

detect()

The detect() function will return a boolean representing whether or not curse words are in a string.

import { detect } from 'curse-filter';

// you can select the languages to detect in the second argument, like in the `filter()` function

console.log(detect('Fuck you')); // true
console.log(detect('Fuck you', 'en')); // true
console.log(detect('Fuck you', ['en', 'fr'])); // true
console.log(detect('Fuckyou', 'en')); // false, view next paragraph.
console.log(detect('I love you')); // false

For more rigid use cases, you can use rigidMode option to also detect curse words that are part of bigger words (For example, for hiding them!).

import { detect } from 'curse-filter';

console.log(detect('Fuckyou', 'en')); // false, the word "Fuck" is inside of a bigger word: "Fuckyou"
console.log(detect('Fuckyou', 'en', { rigidMode: true })); // true, the word "Fuck" is detected even if part of a bigger word

Promise versions

You can access promise versions of filter and detect functions from curse-filter/promises.

import { filter, detect } from 'curse-filter/promises';

const main = async () => {
    const filtered = await filter('Fuck you');
    const detected = await detect('Fuck you');

    console.log(filtered, detected); // '*** you', true
};

main();

Adding custom keywords to search for

curse-filter offers a nice and familiar way to add strings or arrays of strings to the words to search for with the CustomKeywords Set-like object.

import { filter, CustomKeywords } from 'curse-filter';

CustomKeywords.add('Hello'); // Hello

// Custom method of CustomKeywords object
CustomKeywords.addKeywords('Hey', 'Hi', ['Bonjour', 'Ciao']); // Hello, Hey, Hi, Bonjour, Ciao

// The filter and detect functions automatically look for custom keywords added to the object
const result = filter('Hey John!');
console.log(result); // '*** John!'

Express.js middlewares

curse-filter comes with built-in express.js middlewares.

detectMiddleware middleware

The detectMiddleware middleware analizes the whole req.body object for curses.

import express, { Request, Response } from 'express';
import { detectMiddleware } from 'curse-filter';
import { registerUserToDB } from './db';

const app = express();

app.use(express.json());

app.post('/register', detectMiddleware, async (req: Request, res: Response) => {
    /* If the request body contains curse words, the middleware will automatically 
    send a 422 response with a message containing the detected curse words.
    If no curse words are detected, the middleware will call the next() function. */

    await registerUserToDB(req.body);

    res.status(200).json({ message: 'User registered!' });
});

You can configure the middleware with the following options:

// Class for configuring the middleware
import { MiddlewaresConfig } from 'curse-filter';

// Default values:

MiddlewareConfig.onError = null; // Called when a curse word is detected, before sending the response

MiddlewareConfig.detectOptions = {}; // Options for the detect function

MiddlewareConfig.errorMessage = 'Not allowed content detected.'; // Message sent in the response

MiddlewareConfig.statusCode = 422; // Status code sent in the response

Typescript

SupportedLang type

import { SupportedLang } from 'curse-filter';

const lang: SupportedLang = 'en';

Custom Set interfaces

import { KeywordsSet, KeywordsSetConstructor } from 'curse-filter';

/*
 * KeywordsSet is a Set<string> with a custom addKeywords method
 * KeywordsSetConstructor is a SetConstructor with a custom addKeywords method
 */

License

MIT