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.
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 ***.
}
npm install --save censoring
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. |
A Censoring
instance has the following methods.
Enable a filter from the list of filters. By default, they're all disabled.
var scan = new Censoring();
scan.enableFilter('email_address');
Enable multiple filters from the list of filters. By default, they're all disabled.
var scan = new Censoring();
scan.enableFilters(['phone_number', 'email_address']);
Disable a previously enabled filter.
var scan = new Censoring();
scan.enableFilter('email_address');
scan.disableFilter('email_address');
Add multiple words to filter on.
var scan = new Censoring();
scan.enableFilter('words');
scan.addFilterWords(['stoopid head', 'big meany']);
Add a word to filter on.
var scan = new Censoring();
scan.enableFilter('words');
scan.addFilterWord('doody face');
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));
Get the currently set replacement string.
var scan = new Censoring();
scan.getReplacementString(); // Returns '***'
Set the color for highlighted occurrences. Defaults to #F2B8B8
.
var scan = new Censoring();
scan.setHighlightColor('#ff0');
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 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 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 ***
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 ***!
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/
});
If you have any questions or feature requests (such as publishing on bower..?) you can:
- Check out the issues
- Join us on freenode (#spoonx)