A Tiny TypeScript Utility to Mask Emails - GDPR-Friendly by Design

Published: (February 5, 2026 at 02:45 AM EST)
1 min read
Source: Dev.to

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:

OptionTypeDefaultDescription
visibleCharsnumber2Number of characters to keep visible at the start of the local part.
maskSymbolstring*Symbol used for masking.
maskDomainbooleanfalseIf true, masks the domain part as also.
viewablebooleanfalseWhen 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.

Back to Blog

Related posts

Read more »