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
| Option | Type | Default | Description |
|---|---|---|---|
mode | MaskMode | 'mask' | Masking mode to use |
disable | string[] | [] | Detector IDs to disable |
only | string[] | undefined | Run only these detectors |
extend | PIIDetector[] | [] | Additional custom detectors |
regions | string[] | undefined | ISO 3166-1 alpha-2 codes — filter to detectors matching these regions (universal detectors always included) |
keyNameOnly | boolean | false | Skip regex detection, use key heuristics only |
Return value
The returned masker exposes these methods:
Masking
maskString(input, key?, session?)— mask a single string valuemaskObject(input, session?)— deep-walk an object and mask all string valuesmaskArray(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 objectrestoreArray(masked, tokenMap)— restore and parse as array
Sessions
createSession()— create aMaskSessionfor 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 valueMaskResult
Each masking method returns a MaskResult:
interface MaskResult {
result: string;
tokenMap: Record<string, string>;
detections: string[];
}