libphonenumber-js

repository·master·Indexed 25 days ago

https://github.com/catamphetamine/libphonenumber-js

A lightweight, high-performance JavaScript/TypeScript rewrite of Google's libphonenumber library. It provides phone number parsing, formatting, and validation with configurable metadata density (min, max, mobile, or custom) to optimize bundle size. Key features include the AsYouType formatter, phone number extraction from text, and support for both official ISO 3166-1 alpha-2 and unofficial country codes.

Tokens
14.4K
Snippets
34
Records
100
Agent score
79%

What's inside libphonenumber-js

  1. Understand Country Calling Codes

    master

    A Country calling code refers to the digits located between the + symbol and the national (significant) number in an international format number.

    • Example (US): In +1 213 373 4253, the country calling code is 1.
    • Example (FR): In +33 1 45 45 32 45, the country calling code is 33.

    Note that multiple countries may share the same country calling code (e.g., USA, Canada, and several Caribbean nations all use 1).

  2. Understand National (Significant) Numbers

    master

    A National (significant) number consists of all digits in a national phone number excluding the "national prefix".

    • Example (US): For international number +1 213 373 4253, the national number is (213) 373-4253 and the national (significant) number is 213 373 4253.
    • Example (FR): For international number +33 1 45 45 32 45, the national number is 01 45 45 32 45. The national (significant) number is 1 45 45 32 45 (the leading 0 is removed as it is the national prefix).
  3. Understand the metadata structure

    master
    The library uses a metadata.json file (generated from Google's PhoneNumberMetadata.xml) as the source of truth for phone number rules. While metadata.json itself is an intermediary file and not included in the final distribution, the library provides optimized versions like metadata.min.json. The metadata contains information regarding country calling codes, national prefixes, and number patterns.
  4. Include libphonenumber-js via CDN

    master

    You can include the library directly in a web page using a <script> tag from a CDN like unpkg.com or jsdeliv.com. You must specify the version and the bundle type (min, max, or mobile).

    <script src="https://unpkg.com/libphonenumber-js@[version]/bundle/libphonenumber-[type].js"></script>
    
    <script>
      alert(new libphonenumber.AsYouType('US').input('213-373-4253'))
    </script>
  5. Use custom metadata with the core package

    master
    If you have generated a custom, minimal metadata set to support only specific countries, import from libphonenumber-js/core. The functions in this sub-package do not come with pre-packaged metadata; instead, they accept metadata as the last argument of each exported function.
  6. Choose the appropriate metadata set for your project

    master

    To optimize bundle size, libphonenumber-js provides different metadata sets. Choose the one that matches your validation and feature requirements:

    • min (Default): Smallest size (~80kB). Use when you only need basic length validation via .isPossible() and do not need to detect phone number types (e.g., via .getType()) or perform strict digit validation via .isValid().
    • max: Complete metadata (~145kB). Use when you need strict digit validation via .isValid() and phone number type detection via .getType().
    • mobile: Mobile-focused metadata (~95kB). Use when you need max capabilities but only intend to support mobile numbers. Note that non-mobile numbers might return false for .isValid() or undefined for .getType().
    • custom (Advanced): Use when you only need to support a specific subset of countries and want to minimize bundle size by generating your own metadata.
  7. Choose between .isPossible() and .isValid() for validation

    master

    When validating phone numbers, you can choose between two methods:

    • .isPossible(): Validates only the length of the phone number. This is recommended for business requirements to avoid issues where telephone numbering plans change and the library becomes outdated. It is more resilient for long-term maintenance.
    • .isValid(): Validates both the length and the actual digits. Use this if you require strict validation, but be aware that you must keep the library updated to reflect changes in global numbering plans.
  8. Import from specific sub-packages based on metadata needs

    master

    Instead of importing from the main package, import from the sub-package corresponding to the metadata set you want to use to control bundle size:

    Metadata SetImport Path
    minlibphonenumber-js/min (or libphonenumber-js)
    maxlibphonenumber-js/max
    mobilelibphonenumber-js/mobile
    customlibphonenumber-js/core

    If you only need to support ES6-compliant browsers, append /es6 to the path (e.g., libphonenumber-js/max/es6).