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 🙏

© 2026 – Pkg Stats / Ryan Hefner

data-sanitization

v0.4.0

Published

Sanitization library for obfuscating/removing/securing data.

Readme

data-sanitization

Pattern-based sanitization for sensitive data in objects and strings. Masks or removes fields matching configurable patterns, making data safe for logging or external exposure.

Works with both JavaScript and TypeScript — ships with compiled JS, TypeScript declarations, and source maps.

Table of Contents

Installation

npm install data-sanitization
yarn add data-sanitization

Usage

Sanitize an object

import { sanitizeData } from 'data-sanitization';

const input = {
  username: 'mark',
  password: 'super-secret',
  api_key: 'sk_live_abc123',
};

const result = sanitizeData(input);
// => { username: 'mark', password: '**********', api_key: '**********' }

Sanitize a string

Works with JSON strings and form-encoded strings:

sanitizeData('{"password":"secret","username":"mark"}');
// => '{"password":"**********","username":"mark"}'

sanitizeData('password=secret&username=mark');
// => 'password=**********&username=mark'

Remove fields instead of masking

sanitizeData(
  { password: 'secret', token: 'abc', username: 'mark' },
  { removeMatches: true },
);
// => { username: 'mark' }

Options

| Option | Type | Default | Description | | -------------------- | --------------------------- | ------------ | ------------------------------------------------- | | patternMask | string | ********** | String used to replace matched field values | | removeMatches | boolean | false | Remove matched fields entirely instead of masking | | customPatterns | string[] | | Additional field name patterns to match | | customMatchers | DataSanitizationMatcher[] | | Additional regex matchers for custom data formats | | useDefaultPatterns | boolean | true | Whether to include the built-in default patterns | | useDefaultMatchers | boolean | true | Whether to include the built-in default matchers |

Default patterns

The following field name patterns are matched by default (case-insensitive, substring match):

  • apikey
  • api_key
  • password
  • secret
  • token

A field named db_password or client_secret_key would also match because these patterns match as substrings.

Default matchers

Two matchers are included by default:

  • JSON matcher — matches "fieldName":"value" patterns in JSON and JSON-like strings
  • Form-encoded matcher — matches fieldName=value and fieldName:value patterns in URL-encoded and similarly delimited strings

Custom patterns and matchers

import { sanitizeData } from 'data-sanitization';

// Add a custom pattern alongside defaults
sanitizeData(data, {
  customPatterns: ['ssn', 'credit_card'],
});

// Use only custom patterns, no defaults
sanitizeData(data, {
  customPatterns: ['ssn'],
  useDefaultPatterns: false,
});

// Use a custom mask
sanitizeData(data, {
  patternMask: '[REDACTED]',
});

For custom data formats, provide a DataSanitizationMatcher — a function that takes a pattern string and returns a global, case-insensitive RegExp. The regex must use capture groups $1 and $2 to preserve the field name and trailing delimiter while replacing the value.

Error handling

sanitizeData throws a DataSanitizationError when:

  • The input is not a string or object (e.g., number, boolean, undefined)
  • An unexpected error occurs during sanitization (e.g., malformed JSON that cannot be re-parsed)
import { sanitizeData } from 'data-sanitization';
import { DataSanitizationError } from 'data-sanitization/errors';

try {
  sanitizeData(123 as any);
} catch (error) {
  if (error instanceof DataSanitizationError) {
    console.error(error.message); // 'Invalid data type'
    console.error(error.details); // { originalData: 123 }
  }
}

How it works

  1. String input is sanitized directly via regex replacement.
  2. Object input is converted to a JSON string via JSON.stringify, sanitized, then parsed back with JSON.parse.
  3. Each configured pattern is tested against each matcher to produce regex instances that find and replace sensitive field values.

Contributing

For development setup, testing, and release process, see docs/development.md.

License

MIT