@azizbecha/strkit
Version:
strkit is a utility library offering a collection of essential string functions including validation, case conversion, truncation, and more. Ideal for both JavaScript and TypeScript developers to simplify string operations in their applications.
38 lines • 1.39 kB
JavaScript
/**
* Masks an email address for privacy by replacing part of the email with asterisks.
*
* The function preserves the first character, the domain, and the `@` symbol,
* while masking the rest of the email's username.
*
* @param email - The email address to mask.
* @returns The masked email address.
*
* @example
* maskEmail('test@example.com'); // "t***@example.com"
* maskEmail('johndoe@gmail.com'); // "j******@gmail.com"
*/
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = maskEmail;
function maskEmail(email) {
if (!email.includes('@')) {
throw new Error('Invalid email format');
}
const [username, domain] = email.split('@');
if (username.length < 2) {
throw new Error('Email username must have at least two characters');
}
const maskedUsername = username.charAt(0) + '*'.repeat(username.length - 1);
return `${maskedUsername}@${domain}`;
}
});
//# sourceMappingURL=maskEmail.js.map