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

anonymize

v1.0.0

Published

Anonymize values in a list based on key name patterns

Downloads

6,192

Readme

Anonymize

Anonymize values in an object based on property name patterns.

build status

Installation

npm install anonymize

Basic usage

Let's say you want to anonymize the environment variables so that passwords, tokens and other secrets are not exposed, in effect turning this:

{
  AWS_S3_KEY: 'BF73GVD903GFKSHFU2FJ',
  AWS_S3_SECRET: 'f6+ddWfgd2&dfbs3/dfgj&jkdbcds1234dsfgsss',
  DB_PASSWORD: 'FT^&*UHGFDRTYUJHVG',
  NODE_ENV: 'production'
}

Into this:

{
  AWS_S3_KEY: 'BF73GVD903GFKSHFU2FJ',
  AWS_S3_SECRET: '******',
  DB_PASSWORD: '******',
  NODE_ENV: 'production'
}

You would simply:

var anonymize = require('anonymize')();
var anonEnv = anonymize(process.env);

Configuration

You can control both the patterns used to match the properties and the replacement values.

By default properties containing the words secret, pass, auth or token have their values replaced with ****** The default patterns are case insensitive.

Both the patterns and the replacement value can be individually overridden by initializing the module with either an array of patterns or a replacement string (or an options hash containing both an array of patterns and a replacement value).

Override patterns

Create an anonymizer that replaces the value of any property whos name either contains foo or bar:

// ['foo', 'bar'] is equivalent to [/foo/i, /bar/i]
var anonymize = require('anonymize')(['foo', 'bar']);

Override replacement value

Create an anonymizer that replaces any property value with the string hidden:

var anonymize = require('anonymize')('hidden');

Override both patterns and replacement value

Combine both custom patterns and a custom replacement string:

var anonymize = require('anonymize')({
  patterns: ['foo', 'bar'],
  replace: 'hidden'
});

Delete matched properties

If the replacement value is undefined, the property will be deleted instead of having its value replaced.

Conditional replacement

If you instead of a string for the replacement use a function, you can control how a value is overwritten by conditionally returning the value that should be used for overwriting.

var anonymize = require('anonymize')({
  replace: function (key, val) {
    if (typeof val === 'number') return 0;
    return 'hidden';
  }
});

Deeply nested objects

Yes, anonymize will also work on nested objects. So:

{
  foo: { password: 'secret' },
  bar: [ { token: 'secret' }]
}

Will be correctly anonymized to:

{
  foo: { password: '******' },
  bar: [ { token: '******' } ]
}

License

MIT