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

@gune/json-sanitizer

v1.0.3

Published

Sanitizes json keys

Readme

Data Sanitizer

A TypeScript library for sanitizing sensitive data in JSON objects. Protect sensitive information like passwords, credit cards, SSNs, and API keys before logging or transmitting data.

Installation

npm install @gune/json-sanitizer

Features

  • 🔒 Deep sanitization of nested objects and arrays
  • 🎭 Multiple sanitization strategies: mask or omit
  • ⚙️ Configurable masking options
  • 🔄 Circular reference handling
  • 📦 TypeScript support with full type definitions

Quick Start

import { DataSanitizer } from "@gune/json-sanitizer";

const sanitizer = new DataSanitizer({
	keys: ["password", "ssn", "credit_card"],
	strategy: "mask",
	maskOptions: {
		visibleChars: 4,
		position: "end",
	},
});

const dirtyData = {
	user: "Alice",
	password: "superSecretPassword",
	ssn: "123-456-7890",
};

const cleanData = sanitizer.sanitize(dirtyData);
console.log(cleanData);
// Output: { user: "Alice", password: "****************word", ssn: "************7890" }

Strategies

Mask Strategy

The mask strategy replaces sensitive data with mask characters while optionally keeping a portion visible for reference.

const sanitizer = new DataSanitizer({
	keys: ["credit_card", "apiKey"],
	strategy: "mask",
	maskOptions: {
		visibleChars: 4,
		maskChar: "*",
		position: "end",
	},
});

Mask Options

| Option | Type | Default | Description | |--------|------|---------|-------------| | visibleChars | number | 4 | Number of characters to keep visible | | maskChar | string | "*" | The character used for masking | | position | "start" | "end" | "start" | Which end of the string to keep visible |

Position Parameter

The position parameter controls which part of the sensitive data remains visible:

position: "start" - Keeps characters at the beginning visible

const sanitizer = new DataSanitizer({
	keys: ["apiKey"],
	strategy: "mask",
	maskOptions: {
		visibleChars: 7,
		position: "start",
	},
});

sanitizer.sanitize({ apiKey: "sk_live_abc123xyz" });
// Output: { apiKey: "sk_live**********" }

position: "end" - Keeps characters at the end visible

const sanitizer = new DataSanitizer({
	keys: ["credit_card"],
	strategy: "mask",
	maskOptions: {
		visibleChars: 4,
		position: "end",
	},
});

sanitizer.sanitize({ credit_card: "4111-1111-1111-1111" });
// Output: { credit_card: "***************1111" }

Omit Strategy

The omit strategy completely removes sensitive keys from the output.

const sanitizer = new DataSanitizer({
	keys: ["password", "ssn", "apiKey"],
	strategy: "omit", // Default strategy
});

const dirtyData = {
	id: 101,
	user: "Alice",
	password: "superSecretPassword",
	email: "[email protected]",
};

const cleanData = sanitizer.sanitize(dirtyData);
console.log(cleanData);
// Output: { id: 101, user: "Alice", email: "[email protected]" }

Advanced Usage

Deep Nested Sanitization

The sanitizer automatically handles deeply nested objects and arrays:

const sanitizer = new DataSanitizer({
	keys: ["credit_card", "ssn", "password"],
	strategy: "mask",
	maskOptions: {
		visibleChars: 4,
		position: "end",
	},
});

const dirtyData = {
	id: 101,
	user: "Alice",
	password: "superSecretPassword",
	meta: {
		apiKey: "sk_12345",
		timestamp: new Date(),
		nested: {
			ssn: "123-456-7890",
			credit_card: "4111-1111-1111-1111",
		},
	},
};

const cleanData = sanitizer.sanitize(dirtyData);
console.log(JSON.stringify(cleanData, null, 2));
/*
Output:
{
  "id": 101,
  "user": "Alice",
  "password": "******************word",
  "meta": {
    "timestamp": "2026-01-31T...",
    "nested": {
      "ssn": "************7890",
      "credit_card": "***************1111"
    }
  }
}
*/

Dynamic Key Addition

Add sensitive keys to an existing sanitizer instance:

const sanitizer = new DataSanitizer({
	keys: ["password"],
	strategy: "omit",
});

// Later, add more keys
sanitizer.addKeys(["apiKey", "token", "secret"]);

API Reference

DataSanitizer

Constructor

new DataSanitizer(config: SanitizerConfig)

SanitizerConfig:

interface SanitizerConfig {
	keys: string[];                  // Array of sensitive key names to sanitize
	strategy?: "omit" | "mask";      // Sanitization strategy (default: "omit")
	maskOptions?: MaskOptions;       // Options for mask strategy
}

Methods

sanitize<T>(data: T): T

Sanitizes the provided data according to the configured strategy.

addKeys(keys: string[]): void

Dynamically adds additional sensitive keys to the sanitizer.

How to Run Tests

Install Jest

npm install --save-dev jest ts-jest @types/jest

Initialize Jest Configuration

npx ts-jest config:init

Run Tests

npm test

License

MIT

Author

Buddika Gunawardena (BUDDGUN92)