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

censoring

v1.1.1

Published

Censor or highlight words and other patterns intelligently.

Downloads

1,854

Readme

Censoring

This module allows you to detect patterns in texts, even when attempted to be hidden and then either highlight (markup) or censor (replace) them. It checks for upper casing, lower casing, 1337 replacements, or s,p-l.'i*t.

Note: This module works in the browser as a global, or AMD module, as well as in node.js.

Example:

var Censoring    = require('censoring'),
    scan         = new Censoring(),
    testSentence = '';

// Enable filters we want to use
scan.enableFilters(['phone_number', 'email_address', 'words']);

// Word
testSentence += 'The 1nt3r.n.e.t will not be censored! ';

// Phone number
testSentence += 'Call me on 555-123456';

// Email address
testSentence += ', or send an email to me[at]example(dot)com.';

// Let's make the word internet illegal.
scan.addFilterWord('internet');

// Tell the scanner we're done, and it can prepare the results.
scan.prepare(testSentence);

// Did we have a match?
if (scan.test()) {
  console.log(
    'We had a match! Here it is, but censored:',
    scan.replace()
  );

  // The *** will not be censored! Call me on ***, or send an email to ***.
}

Installation

npm install --save censoring

Filters

| Pattern | Description | | :------------ | :----------------------------------------| | long_number | Matches long, consecutive numbers | | phone_number | Matches phone numbers. | | email_address | Matches email addresses in many formats. | | url | Matches URL patterns. | | words | Finds words, even when in disguise. |

Methods

A Censoring instance has the following methods.

.enableFilter(string filterName)

Enable a filter from the list of filters. By default, they're all disabled.

var scan = new Censoring();

scan.enableFilter('email_address');

.enableFilters(Array filterNames)

Enable multiple filters from the list of filters. By default, they're all disabled.

var scan = new Censoring();

scan.enableFilters(['phone_number', 'email_address']);

.disableFilter(string filterName)

Disable a previously enabled filter.

var scan = new Censoring();

scan.enableFilter('email_address');
scan.disableFilter('email_address');

.addFilterWords(Array filterWords)

Add multiple words to filter on.

var scan = new Censoring();

scan.enableFilter('words');
scan.addFilterWords(['stoopid head', 'big meany']);

.addFilterWord(string filterWord)

Add a word to filter on.

var scan = new Censoring();

scan.enableFilter('words');
scan.addFilterWord('doody face');

.setReplacementString(string|function(match: string):string replacementString)

Set a replacement function, or set a string to replace matches with. Defaults to ***.

var scan = new Censoring();

scan.setReplacementString('pony');
scan.setReplacementString((match) => '*'.repeat(match.length));

.getReplacementString()

Get the currently set replacement string.

var scan = new Censoring();

scan.getReplacementString(); // Returns '***'

.setHighlightColor(string hexCode)

Set the color for highlighted occurrences. Defaults to #F2B8B8.

var scan = new Censoring();

scan.setHighlightColor('#ff0');

.prepare(string inputString[, bool highlight])

Prepare a string, and optionally supply highlight to not replace occurrences, but highlight them using html.

var scan = new Censoring();

scan.enableFilter('email_address');
scan.prepare('me@example[dot]com', true);

.test()

Test if the string you've prepared matches any of the filters.

var scan = new Censoring();

scan.enableFilter('email_address');
scan.prepare('me@example[dot]com');

if (scan.test()) {
  console.log('We have a match!');
}

.replace()

Replace all occurrences found in the prepared string.

Note: This will return HTML with the matches highlighted if the scan was prepared with .prepare(txt, true).

var scan = new Censoring();

scan.enableFilter('email_address');
scan.prepare('Email me at me@example[dot]com');

console.log(scan.replace());
// Outputs: Email me at ***

.filterString(string inputString[, bool highlight])

Filter a string directly, without preparing it first.

Note: Bad for performance When combined with .test() and .replace.

var scan = new Censoring(),
    testString = "I'm going to tell mommy that you're a big meany!",
    result;

scan.enableFilter('words');
scan.addFilterWords(['stoopid head', 'big meany']);

result = scan.filterString(testString);

console.log(result);
// Outputs: I'm going to tell mommy that you're a ***!

.addFilter(string name)

Add a new filter. A filter is essentially a name and a pattern.

var scan = new Censoring();

scan.addFilter('bigot', {
    enabled: true,
    pattern: /^I'm not a racist,? but/
});

Support / contributing

If you have any questions or feature requests (such as publishing on bower..?) you can:

  • Check out the issues
  • Join us on freenode (#spoonx)