redact.email
v0.1.0 latestRedact an RFC 5321 email address by masking the local part while preserving the domain, per GDPR data minimization principles.
Redact an RFC 5321 email address by masking the local part while preserving the domain, per GDPR data minimization principles.
Signature
function email(v: string): string
Problem
Email addresses in logs, error reports, and analytics payloads expose user identity. Partial masking that preserves the domain portion allows debugging routing issues without revealing the full local part. Ad-hoc regex masking varies across codebases, creating inconsistent PII handling.
How It Works
Trims the input, locates the last '@' sign, preserves the first character of the local part, replaces the rest with '@', and appends the full domain. Empty strings and inputs without a valid '@' return the fixed mask ''.
Boundaries
- Does not validate email format per RFC 5321.
- Does not normalize Unicode or punycode domains.
- Returns '***' for empty string or missing '@' — never throws on valid string input.
Replaces
Common boilerplate this function replaces:
v.replace(/^(.).*@/, '$1***@')
Examples
redact.email("john@example.com"); // "j***@example.com"
redact.email("a@b.co"); // "a***@b.co"
redact.email(""); // "***"
redact.email("@example.com"); // "***"
redact.email(" alice@work.org "); // "a***@work.org"
Standards
Caveats
- Uses lastIndexOf('@') — addresses with multiple '@' characters mask everything before the last one.
- The first character is always exposed; single-character local parts (e.g. 'a@b.com') still reveal the full local part.
FAQ
How to mask an email address in JavaScript for logging?
redact.email('user@example.com') returns 'u***@example.com' — keeps the first character and full domain for debugging while hiding the rest.
How to redact PII from email fields before sending to analytics?
Pass the email string through redact.email() which masks the local part while preserving the domain, consistent with GDPR data minimization.
What happens when redacting an invalid email address?
Inputs without '@' or with '@' at position 0 return ''. Empty strings also return ''. Non-string input throws TypeError.
How to partially mask email addresses in error logs?
redact.email() preserves the first character and domain (e.g. 'j***@example.com'), providing enough context for routing diagnosis without full PII exposure.
How to handle email redaction consistently across microservices?
Use redact.email() as the single masking function. It produces deterministic output: same input always yields the same masked form.