npm package: @gune/json-sanitizer
Description: Sanitizes json keys
Author: BUDDGUN92
Latest version: 1.0.3

# 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

```bash
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

```typescript
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.

```typescript
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
```typescript
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
```typescript
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.

```typescript
const sanitizer = new DataSanitizer({
	keys: ["password", "ssn", "apiKey"],
	strategy: "omit", // Default strategy
});

const dirtyData = {
	id: 101,
	user: "Alice",
	password: "superSecretPassword",
	email: "alice@example.com",
};

const cleanData = sanitizer.sanitize(dirtyData);
console.log(cleanData);
// Output: { id: 101, user: "Alice", email: "alice@example.com" }
```

## Advanced Usage

### Deep Nested Sanitization

The sanitizer automatically handles deeply nested objects and arrays:

```typescript
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:

```typescript
const sanitizer = new DataSanitizer({
	keys: ["password"],
	strategy: "omit",
});

// Later, add more keys
sanitizer.addKeys(["apiKey", "token", "secret"]);
```

## API Reference

### `DataSanitizer`

#### Constructor

```typescript
new DataSanitizer(config: SanitizerConfig)
```

**SanitizerConfig:**
```typescript
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

```bash
npm install --save-dev jest ts-jest @types/jest
```

### Initialize Jest Configuration

```bash
npx ts-jest config:init
```

### Run Tests

```bash
npm test
```

## License

MIT

## Author

Buddika Gunawardena (BUDDGUN92)