UNPKG

@handy-common-utils/misc-utils

Version:
127 lines (126 loc) 5.72 kB
"use strict"; /* eslint-disable no-mixed-operators */ /* eslint-disable unicorn/prefer-string-slice */ Object.defineProperty(exports, "__esModule", { value: true }); exports.maskCreditCard = exports.maskAll = exports.maskFullName = exports.maskEmail = exports.masker = exports.mask = void 0; /** * Mask the content of a string * @param input The input which could also be null or undefined * @param keepLeft Number of characters on the left to be kept in the output without masking. * Default value is 1. * @param keepRight Number of characters on the right to be kept in the output without masking. * Default value is 0. * @param minLength Minimal length of the string for keepLeft and keepRight to be effective. * If the input string is shorter than this length, the whole string would be masked. * Default value is 3. * @param maskLengthOrMaskString The string to be used for replacing the part in the input that needs to be masked, * or the length of the mask string if a fixed length is desired, * or null/undefined if the mask string should have the same length as the part to be masked. * Default value is null. * @param maskPattern The pattern to be repeated as the mask. * Default value is '*'. * @returns String with masked content */ function mask(input, keepLeft = 1, keepRight = 0, minLength = 3, maskLengthOrMaskString = null, maskPattern = '*') { if (minLength <= keepLeft + keepRight) { throw new Error(`Invalid parameter: expected minLength > keepLeft + keepRight, but got ${minLength}, ${keepLeft}, ${keepRight}`); } // pass through for undefined and null if (input == null) { return input; } if (input.length < minLength) { keepLeft = 0; keepRight = 0; } let maskString; if (typeof maskLengthOrMaskString === 'string') { maskString = maskLengthOrMaskString; } else { const maskLength = maskLengthOrMaskString == null ? (input.length - keepLeft - keepRight) : maskLengthOrMaskString; maskString = maskPattern.length === 1 ? maskPattern.repeat(maskLength) : maskPattern.repeat(maskLength / maskPattern.length + 1).substring(0, maskLength); } return `${input.substring(0, keepLeft)}${maskString}${input.substring(input.length - keepRight, input.length)}`; } exports.mask = mask; /** * Create a mask function with pre-set parameters. * @example * const maskApiKey = masker(2, 2, 10); * const maskedString = maskApiKey(myApiKey); * * @param keepLeft Number of characters on the left to be kept in the output without masking. * Default value is 1. * @param keepRight Number of characters on the right to be kept in the output without masking. * Default value is 0. * @param minLength Minimal length of the string for keepLeft and keepRight to be effective. * If the input string is shorter than this length, the whole string would be masked. * Default value is 3. * @param maskLengthOrMaskString The string to be used for replacing the part in the input that needs to be masked, * or the length of the mask string if a fixed length is desired, * or null/undefined if the mask string should have the same length as the part to be masked. * Default value is null. * @param maskPattern The pattern to be repeated as the mask. * Default value is '*'. * @returns A mask function that has specified parameters as pre-set */ function masker(keepLeft, keepRight, minLength, maskLengthOrMaskString, maskPattern) { return input => mask(input, keepLeft, keepRight, minLength, maskLengthOrMaskString, maskPattern); } exports.masker = masker; /** * Mask sensitive information in an email address while keeping some information for troubleshooting * @param email the email address which could also be null or undefined * @returns masked email address */ function maskEmail(email) { // pass through for undefined and null if (email == null) { return email; } const segments = email.split('@'); if (segments.length === 2 && segments[0].length > 0) { const parts = segments[0].split('.'); segments[0] = parts.map(s => mask(s, 1)).join('.'); return segments.join('@'); } // does not look like an email return mask(email); } exports.maskEmail = maskEmail; /** * Mask sensitive information in the full name while keeping useful information for troubleshooting * @param name the full name which could also be null or undefined * @returns masked full name */ function maskFullName(name) { // pass through for undefined and null if (name == null) { return name; } const segments = name.split(' '); return segments.map(s => mask(s, 1)).join(' '); } exports.maskFullName = maskFullName; /** * Replace each character of the input with '*' * @param input a string or null or undefined * @returns masked string or null or undefined */ function maskAll(input) { return mask(input, 0); } exports.maskAll = maskAll; /** * Mask credit card number string * @param input credit card number string which could also be null or undefined * @returns Something like ****-****-****-1234, or null/undefined if the input is null/undefined */ function maskCreditCard(input) { if (input == null) { return input; } return '****-****-****-' + input.padStart(4, '*').slice(-4); } exports.maskCreditCard = maskCreditCard;