BBizKit

redact.phone

v0.1.0 latest

Redact a phone number using ITU-T E.164 structure awareness, preserving country code and last 4 subscriber digits.

redactphonepiie164gdprmaskingtelephony

Redact a phone number using ITU-T E.164 structure awareness, preserving country code and last 4 subscriber digits.

Signature

function phone(v: string, opt?: PhoneRedactOptions): string

Type Definitions:

Problem

Phone numbers in logs and audit trails expose subscriber identity. Different countries use different numbering plans, so naive digit-count masking fails to preserve the structurally useful country code prefix. Without structure-aware redaction, debugging international telephony routing requires the full number.

How It Works

Strips non-digit characters, detects the country code from a built-in E.164 prefix table (~40 countries), then applies mode-specific masking. 'partial' (default) preserves the country code and last 4 subscriber digits. 'keepAreaCode' preserves the country code and area code, masking the subscriber portion entirely. Numbers with 4 or fewer digits are returned unchanged.

Boundaries

  • Does not validate that the number is a real E.164 number.
  • Country code detection covers ~40 common prefixes; unlisted prefixes fall back to generic masking.
  • Numbers without a '+' prefix are treated as local numbers (no country code extraction).

Replaces

Common boilerplate this function replaces:

const digits = v.replace(/\D/g, ''); '+' + digits.slice(0, ccLen) + '*'.repeat(digits.length - ccLen - 4) + digits.slice(-4)

Examples

redact.phone("+1-202-555-0173");  // "+1*****0173"
redact.phone("+44 20 7946 0958", { mode: "keepAreaCode" });  // "+44207******"
redact.phone("555-0173");  // "***50173"
redact.phone("");  // "***"
redact.phone("1234");  // "1234"

Standards

Caveats

  • The E.164 prefix table uses longest-prefix-first matching; ambiguous prefixes (e.g. '7' for Russia vs Kazakhstan) resolve to the first match.
  • Area code lengths are approximations from a built-in table (~20 countries); countries not in the table default to 2-digit area codes.
  • Numbers with ≤4 digits are returned as-is — too short to redact meaningfully.

FAQ

How to mask a phone number in JavaScript while keeping the country code?

redact.phone('+12025550173') returns '+1*****0173' — preserves the country code prefix and last 4 digits by default.

How to redact international phone numbers with different country codes?

The function auto-detects country code from the '+' prefix using a built-in E.164 prefix table covering ~40 countries.

How to keep the area code visible when redacting a phone number?

Use mode 'keepAreaCode': redact.phone('+442079460958', { mode: 'keepAreaCode' }) preserves the country code and area code, masking the subscriber portion.

What happens when redacting a phone number without a country code prefix?

Numbers without '+' are treated as local: all digits except the last 4 are masked.

How to handle phone number redaction for GDPR compliance?

redact.phone() applies partial masking consistent with GDPR data minimization — preserving only the minimum structural information needed for debugging.