hush

0.2.3 • Public • Published

hush

This node module redacts text according to the regexes you supply and fires your callback when done.

Install

$ npm install hush

Usage

var hush = require('hush')

Now hush expects three arguments:

  1. A source file
  2. An options object
  3. And a callback: cb(err, data)
hush(source, options, callback)

The source file and callback are pretty self-explanatory (anyway, see below), so about that options object...

options.targets is required. That is the regular expression or array of regular expressions to be redacted. As the second argument to hush, you will definitely want something that looks like this:

{ targets: regexOrArrayOfRegexes }

options.replacement is optional. If given, it should be in the form of a string.

If you provide it as a single character, that character will be repeated for the length of the match that is replaced. This is actually the default behavior: if you do not provide options.replacement, it defaults to an asterisk. Thus, if the match is a four letter word, the match will be replaced by four asterisks.

However, if you provide a string of greater than one character, that string will be used as is.

For example, you could replace each instance of a match with [ REDACTED ] through use of the following options object:

{ targets : someRegEx
, replacement : '[ REDACTED ]'
}

Examples

Given a file, 'example.txt', that looks like this:

This is a US social security number sort of thing: 123-44-5555

This is a US phone number sort of thing: 555-555-5555

Here is another US social security number sort of thing: 987-65-4321

And another US phone number sort of thing: 123-456-7890

Here are some 123-456-7890 interspersed 123-456-7890 with random 987-65-4321 text 123-456-7890 just
to see 987-65-4321 how 123-456-7890 that 987-65-4321
sort of thing goes 987-65-4321...

You could do something like this:

const
  hush = require('hush')
, fs = require('fs')

, SS_Regex = /\d{3}-?\d{2}-?\d{4}/g
, PH_Regex = /\d{3}-?\d{3}-?\d{4}/g


hush(
  'example.txt'
, { targets: [SS_Regex, PH_Regex] // can be an array of regexes or a single regex
  , replacement: 'NOPE'           
  }
, function(err, data) {
    if (err) return console.error(err)

    fs.writeFile('redacted.txt', data, function(err) {
      if (err) return console.error(err)
      console.log('Check yer directory...')
    })
  }
)

in which case you would end up with this:

This is a US social security number sort of thing: NOPE

This is a US phone number sort of thing: NOPE

Here is another US social security number sort of thing: NOPE

And another US phone number sort of thing: NOPE

Here are some NOPE interspersed NOPE with random NOPE text NOPE just
to see NOPE how NOPE that NOPE
sort of thing goes NOPE...

Or, if you were to do this:

hush(
  'example.txt'
, { targets: [SS_Regex, PH_Regex] 
  , replacement: '@'  // If omitted, replacement defaults to an asterisk      
  }
, function(err, data) {
    if (err) return console.error(err)

    fs.writeFile('redacted.txt', data, function(err) {
      if (err) return console.error(err)
      console.log('Check yer directory...')
    })
  }
)

then you'd end up with this:

This is a US social security number sort of thing: @@@@@@@@@@@

This is a US phone number sort of thing: @@@@@@@@@@@@

Here is another US social security number sort of thing: @@@@@@@@@@@

And another US phone number sort of thing: @@@@@@@@@@@@

Here are some @@@@@@@@@@@@ interspersed @@@@@@@@@@@@ with random @@@@@@@@@@@ text @@@@@@@@@@@@ just
to see @@@@@@@@@@@ how @@@@@@@@@@@@ that @@@@@@@@@@@
sort of thing goes @@@@@@@@@@@...

Attention

  1. The effectiveness of this module hinges upon the accuracy of any regular expressions you provide.
  2. Be sure to thoroughly inspect program output to avoid potentially exposing any private data.

ISC License (ISC)

Copyright 2014, Instancetype

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

Package Sidebar

Install

npm i hush

Weekly Downloads

2

Version

0.2.3

License

ISC

Last publish

Collaborators

  • instancetype