e164num
Version:
**Light validation and manipulation of E.164 phone numbers.**
15 lines (14 loc) • 542 B
JavaScript
/**
* Masks a phone number that is assumed to be a full valid E164 format. The
* result will have only the plus sign, the first digit right after it, and the
* last 3 digits visible. All others will be masked with an asterisk.
*
* @param e164PhoneNumber The E164 phone number to mask.
*
* @example
* ```ts
* maskPhoneNumber('+17871234567')
* // returns '+1*******567'
* ```
*/
export const maskPhoneNumber = (e164PhoneNumber) => e164PhoneNumber.replace(/^\+(\d)(.*)(.{3})$/, (_, a, b, c) => `+${a}${b.replace(/\d/gm, '*')}${c}`);