A Tiny TypeScript Utility to Mask Emails - GDPR-Friendly by Design
Source: Dev.to
Overview
mask-email is a tiny, dependency‑free TypeScript utility that masks email addresses to help you avoid leaking personal data in logs, UI, or analytics. By default it keeps a few characters visible and replaces the rest with a mask symbol, providing just enough information to identify a user without exposing the full address.
Usage
import { maskEmail } from '@ekaone/mask-email';
maskEmail('ekaone3033@gmail.com');
// => ek********@gmail.com
maskEmail('john.doe@example.com');
// => jo******@example.com
Conditional masking
You can disable masking for specific cases:
maskEmail('secret@company.com', { viewable: true });
// => secret@company.com
Options
The utility accepts an options object to fine‑tune the output:
| Option | Type | Default | Description |
|---|---|---|---|
visibleChars | number | 2 | Number of characters to keep visible at the start of the local part. |
maskSymbol | string | * | Symbol used for masking. |
maskDomain | boolean | false | If true, masks the domain part as also. |
viewable | boolean | false | When true, returns the original email unchanged. |
Example with custom options
maskEmail('admin@mail.company.com', {
visibleChars: 1,
maskDomain: true,
});
// => a****@m***.c******.com
Installation
npm install @ekaone/mask-email
# or
yarn add @ekaone/mask-email
# or
pnpm add @ekaone/mask-email
The source code is openly available on GitHub:
https://github.com/ekaone/mask-email
Happy coding — and may your emails stay masked.