Optionaltext: string | null
The mobile phone number to mask
OptionalalphaOrDial: string
The ISO alpha-2 country code or dial code that hints the number's country
a masked local mobile number, or undefined if the given text is not a resolvable mobile number
maskMobile(undefined); // undefined
maskMobile('13800138000', 'CN'); // '138****8000'
maskMobile('+8613800138000'); // '138****8000'
maskMobile('+86-13812345678'); // '138****5678'
maskMobile('13812345678', '+86'); // '138****5678'
maskMobile('13812345678', 'cn'); // '138****5678'
maskMobile('5551234567', 'US'); // '55****4567'
maskMobile('+12025550123'); // '20****0123'
maskMobile('+447911123456'); // '79****3456'
maskMobile('invalid'); // undefined
Masks an international mobile phone number, regardless of its country.
The local number keeps its leading digits in plain text, replaces the middle 4 digits with
*, and keeps the trailing 4 digits in plain text. The country calling code is dropped from the result, so the masked value is a pure local number.The
textmay be:13800138000;+prefix, e.g.+8613800138000;+86-13812345678.The optional
alphaOrDialhints the country of the number:CN,US); or+86,86). When it is a dial code andtexthas no+prefix, it is prepended totextso thatlibphonenumber-jscan detect the country automatically.When
alphaOrDialis omitted,textis parsed directly and the country is auto-detected, which requirestextto carry a+country code; otherwise the number cannot be resolved andundefinedis returned.A number is masked only when
libphonenumber-jscan resolve it to a country and extract its local number; input that cannot be parsed (e.g. a bare local number without a country hint, or an overly short number) yieldsundefined.