Skip to content

Instantly share code, notes, and snippets.

@tanpld
Last active November 19, 2021 00:11
Show Gist options
  • Save tanpld/eb9a25f399260b1cf814edc48926745a to your computer and use it in GitHub Desktop.
Save tanpld/eb9a25f399260b1cf814edc48926745a to your computer and use it in GitHub Desktop.
const maskedFirst4Char = (value: string): string => {
return value.replace(/.(?=.{4})/g, '*');
}
const maskPhoneNumber = (phone: string): string => {
if (!phone) return 'N/A';
const code = phone.slice(0, 2);
const number = phone.slice(2);
const maskedFirst4Number = maskedFirst4Char(number)
.replace(/.(?=(.{4})+$)/g, '$& '); // add a space after every 4 number
return `+${code} ${maskedFirst4Number}`;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment