Socket
Socket
Sign inDemoInstall

censoring

Package Overview
Dependencies
0
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    censoring

Censor or highlight words and other patterns intelligently.


Version published
Weekly downloads
349
decreased by-38.66%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

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

PatternDescription
long_numberMatches long, consecutive numbers
phone_numberMatches phone numbers.
email_addressMatches email addresses in many formats.
urlMatches URL patterns.
wordsFinds 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)

Keywords

FAQs

Last updated on 04 Oct 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc