@unikue/ts-lang-utils
    Preparing search index...

    Function maskMobile

    • 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 text may be:

      • a plain local number, e.g. 13800138000;
      • an E.164 number with a + prefix, e.g. +8613800138000;
      • an E.164 number followed by a dash, e.g. +86-13812345678.

      The optional alphaOrDial hints the country of the number:

      • a two-letter ISO country code (case-insensitive, e.g. CN, US); or
      • a dial code (e.g. +86, 86). When it is a dial code and text has no + prefix, it is prepended to text so that libphonenumber-js can detect the country automatically.

      When alphaOrDial is omitted, text is parsed directly and the country is auto-detected, which requires text to carry a + country code; otherwise the number cannot be resolved and undefined is returned.

      A number is masked only when libphonenumber-js can 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) yields undefined.

      Parameters

      • 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

      Returns string | undefined

      a masked local mobile number, or undefined if the given text is not a resolvable mobile number

      David Hsing

      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