pii-mask

createMasker

Factory function that returns a configured masker instance.

Usage

import { createMasker } from '@pii-mask/core';

const masker = createMasker({
  mode: 'mask',
  disable: ['person-name'],
});

Options

OptionTypeDefaultDescription
modeMaskMode'mask'Masking mode to use
disablestring[][]Detector IDs to disable
onlystring[]undefinedRun only these detectors
extendPIIDetector[][]Additional custom detectors
regionsstring[]undefinedISO 3166-1 alpha-2 codes — filter to detectors matching these regions (universal detectors always included)
keyNameOnlybooleanfalseSkip regex detection, use key heuristics only

Return value

The returned masker exposes these methods:

Masking

  • maskString(input, key?, session?) — mask a single string value
  • maskObject(input, session?) — deep-walk an object and mask all string values
  • maskArray(input, session?) — deep-walk an array and mask all string values

Detection only

  • detectString(input, key?) — detect PII without masking, returns { detections: string[] }

Restoring

  • restore(masked, tokenMap) — reverse tokenization (returns string)
  • restoreObject(masked, tokenMap) — restore and parse as object
  • restoreArray(masked, tokenMap) — restore and parse as array

Sessions

  • createSession() — create a MaskSession for cross-call consistency (shared token map and counters)

Sessions allow multiple masking calls to share the same token map and pseudonymization counters. Pass the session as the last argument to any masking method:

const session = masker.createSession();
masker.maskString('john@example.com', undefined, session);
masker.maskObject({ email: 'john@example.com' }, session);
// Both calls share the same token/label for the same value

MaskResult

Each masking method returns a MaskResult:

interface MaskResult {
  result: string;
  tokenMap: Record<string, string>;
  detections: string[];
}

Try it in the browser ↗