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

profanity-middleware

v0.3.0

Published

A foul-language filter for NodeJS that works seamlessly as a middleware

Downloads

24

Readme

profanity-middleware

A foul-language filter for NodeJS that works seamlessly as a middleware.

Build Status NPM Module

When used as middleware, all data received for the requests are purified automatically. It inserts profanity filters into all routes (or specific ones) to implicitly take care of purification, so you never have to deal with it.

This plugin is built on the assumption that profanity filters need to be implemented implicitly, so you do not have to manually call functions for sanitizing each string (but you can do that too, with this plugin).

Installation

npm install profanity-middleware

Basic Usage

As middleware with Express for all routes (app middleware):

var app = express();
var profanity = require('profanity-middleware');
...
//try to keep this the last app.use (optional)
app.use(profanity.init); //will filter all user input data in all routes

As middleware with Express for specific routes (route middleware):

var profanity = require('profanity-middleware');
...
app.post('/createPost/', profanity.init, function(req,res) {
	...
});

As function:

var profanity = require('profanity-middleware'),
    filter = profanity.filter;

var html = 'Hello Foul World',
    filtered = filter(html, {blacklist: ['foul']}); //defining foul as a slang word
console.log('filtered');
//Hello f**l world

Configuration

This plugin supports 3 basic options that can be configured:

{
	mask: '$', //default '*'
	fullyMasked: true, // default false
	blacklist: ['foul', 'slang'] //default []
}

mask (optional) is used to filter foul words with a character of your choice; default is asterisk (*)

fullyMasked (optional) is used to replace all letters in profane words with the mask character, instead of just the inner letters

blacklist (optional) is used to define words that you wish to filter in addition to the default dictionary; default is an empty array;

As middleware

As middleware, the options are specified just after 'require':

var profanity = require('profanity-middleware');
profanity.setOptions({
	mask: '$', 
	blacklist: ['foul', 'slang']
});
app.use(profanity.init); //then use it as app middleware or route middleware

As function

As function, the options are specified when calling the function

filter(html, {blacklist: ['foul', 'slang']});

Newly Added

Profane Words Count

When used as middleware, the res object of the route will be assigned a profaneWordsCount which is the count of foul words encountered in that commit.

You can try: console.log(res.profaneWordsCount) in any of your route, and can calculate a rating of foulness based on this.

When used as a direct filter function, you can pass in a parameter which will be changed by reference, as below: filter(str, {mask: '*', blacklist: ['custom']}, resStub) Or: filter(str, {}, resStub) //for default options

The count will be returned in the next line as: resStub.profaneWordsCount

Profane Words Rating

To count the rating, simply call the rating function as below:

var profanity = require('profanity-middleware');
var rating = profanity.rating;
var filter = profanity.filter;
var resStub = {}; //if you are getting data from the request object (as middleware), just use the 'res' object
var badword = filter('enterBadWordHere', {blacklist: ['Bad']}, resStub);
var computedRating = rating(resStub.profaneWordsCount);
console.log("Rating of profanity is ", computedRating);

Fully Masking Profane Words

By default, the middleware will leave the first and last letter of a profane word unmasked. To mask the full word, including first and last letters, add the fullyMasked option to the filter object and set it to true, like below:

...
filter('enterBadWordHere', {fullyMasked: true}, resStub);
...

Tests

npm test

  • Check for replacing case-sensitive words
  • Check for not replacing in-string words that could be part of other words
  • Check for recursively filtering strings inside objects
  • Check for custom words and custom mask
  • Check for count of profane words
  • Check rating of profane words
  • Check for fully masking words as alternate option

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.

Release History

  • 0.3.0 Added backwards-compatible option to fully mask profane words
  • 0.2.1 Profane words rating calculator function added
  • 0.2.0 Profane word counts added
  • 0.1.4 GHOST Enhancement
  • 0.1.3 Line breaks Enhancement