UNPKG

dataveil

Version:

A robust TypeScript library for masking sensitive data including card numbers, emails, passwords, phone numbers, and more.

26 lines (25 loc) 1.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.maskSubstring = maskSubstring; function maskSubstring(text, substring, options) { if (options === void 0) { options = {}; } if (!text || typeof text !== 'string') { throw new Error('Text must be a non-empty string'); } if (text.length > 10000) { throw new Error('Text too long (maximum: 10,000 characters)'); } if (!substring || typeof substring !== 'string') { throw new Error('Substring must be a non-empty string'); } if (substring.length > 1000) { throw new Error('Substring too long (maximum: 1,000 characters)'); } var _a = options.maskChar, maskChar = _a === void 0 ? "*" : _a, _b = options.maskOnlyFirstOccurrence, maskOnlyFirstOccurrence = _b === void 0 ? false : _b; if (!maskChar || maskChar.length !== 1) { throw new Error('Mask character must be a single character'); } var escapedSubstring = substring.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); var regex = new RegExp(escapedSubstring, maskOnlyFirstOccurrence ? "" : "g"); return text.replace(regex, maskChar.repeat(substring.length)); }