This package simplifies phone number handling by wrapping Google's libphonenumber. While offering complete access to i18n.phonenumbers.PhoneNumber
and i18n.phonenumbers.PhoneNumberUtil
through getParsedPhoneNumber() and getPhoneNumberUtil(), it prioritizes ease of use. The constructor and getPhoneNumberInfo()
provide a quick way to perform common tasks and access essential information. Designed to abstract frequently used features, this package welcomes user feedback and encourages feature requests.
-
Phone Number Parsing:
- Parses phone numbers from various formats into a structured
IPhoneNumber
object. - Supports parsing with or without keeping the raw input.
- Handles lenient parsing, allowing for punctuation, whitespace, and leading text.
- Parses phone numbers from various formats into a structured
-
Phone Number Formatting:
- Formats parsed phone numbers into E.164, international, national, and RFC3966 formats.
- Formats numbers in their original format.
-
Phone Number Information Retrieval:
- Retrieves detailed information about a phone number, including:
- Country code.
- National number.
- Number type (mobile, fixed-line, etc.).
- Validity checks (possible, valid, valid for region).
- Raw Input.
- Region Code.
- Extension.
- Retrieves detailed information about a phone number, including:
-
Phone Number Type Detection:
- Identifies the type of phone number (mobile, fixed-line, toll-free, etc.).
-
Validation:
- Checks if a number is valid, possible, and valid for a region.
-
As-You-Type Formatting:
- Formats phone numbers in real-time as they are being typed.
- Clears the input in the formatter.
-
TypeScript Support:
- Provides type definitions for enhanced development experience.
-
Future Features:
-
ShortNumberInfo
will be added in future releases.
-
npm install @arctics/google-phonelib
or
yarn add @arctics/google-phonelib
import {
PhoneNumberHandler,
PhoneNumberFormat,
AsYouTypeHandler,
} from '@arctics/google-phonelib';
// Parsing and retrieving information
const handler = new PhoneNumberHandler('+12025550100', 'US');
const info = handler.getPhoneNumberInfo();
console.log(info.nationalNumber); // Output: 2025550100
console.log(info.countryCode); // Output: 1
console.log(info.numberType); // Output: FIXED_LINE_OR_MOBILE
// Formatting
const formattedNational = handler.format(PhoneNumberFormat.NATIONAL);
console.log(formattedNational); // Output: (202) 555-0100
const formattedE164 = handler.format(PhoneNumberFormat.E164);
console.log(formattedE164); // Output: +12025550100
//Original formatting
const originalFormat = handler.formatInOriginalFormat('US');
console.log(originalFormat);
// AsYouTypeHandler usage
const asYouTypeHandler = new AsYouTypeHandler('US');
console.log(asYouTypeHandler.typeNumber('202')); // Output: 202
console.log(asYouTypeHandler.typeNumber('555')); // Output: 202-555
console.log(asYouTypeHandler.typeNumber('0100')); // Output: (202) 555-0100
asYouTypeHandler.clearInput();
-
Constructor:
-
constructor(phoneNumber: string, regionCode: string, mode?: string)
- Creates a new
PhoneNumberHandler
instance. -
phoneNumber
: The phone number string to parse. -
regionCode
: The region code (e.g., 'US', 'GB'). -
mode
: Parsing mode ('parse' or 'parseAndKeepRawInput'). Defaults to 'parseAndKeepRawInput'.
- Creates a new
-
-
Methods:
-
getParsedPhoneNumber(): IPhoneNumber | null
- Returns the parsed
IPhoneNumber
object.
- Returns the parsed
-
getPhoneNumberUtil(): IPhoneNumberUtil
- Returns the PhoneNumberUtil instance.
-
format(numberFormat: NumberFormat): string
- Formats the phone number according to the specified
NumberFormat
(E164, INTERNATIONAL, NATIONAL, RFC3966).
- Formats the phone number according to the specified
-
formatInOriginalFormat(regionCallingFrom: string): string
- Formats the phone number in its original format.
-
formatOutOfCountryCallingNumber(regionCallingFrom: string): string
- Formats a phone number for out-of-country dialing purposes.
-
getPhoneNumberInfo(): IPhoneNumberInfo
- Returns an object containing detailed information about the phone number.
-
-
Constructor:
-
constructor(regionCode: string)
- Creates a new
AsYouTypeHandler
instance. -
regionCode
: The region code for formatting the phone number.
- Creates a new
-
-
Methods:
-
typeNumber(phoneNumber: string): string
- Formats the phone number as you type.
-
phoneNumber
: The phone number to format. - Returns the formatted phone number.
-
clearInput(): void
- Clears the input in the formatter.
-
-
PhoneNumberType
Enum- FIXED_LINE
- MOBILE
- FIXED_LINE_OR_MOBILE
- TOLL_FREE
- PREMIUM_RATE
- SHARED_COST
- VOIP
- PERSONAL_NUMBER
- PAGER
- UAN
- VOICEMAIL
- UNKNOWN
-
PhoneNumberFormat
Enum- E164
- INTERNATIONAL
- NATIONAL
- RFC3966
-
CountryCodeSource
Enum- FROM_DEFAULT_COUNTRY
- FROM_NUMBER_WITHOUT_PLUS_SIGN
- FROM_NUMBER_WITH_IDD
- FROM_NUMBER_WITH_PLUS_SIGN
- UNSPECIFIED
-
Interfaces
- IPhoneNumber
- IPhoneNumberInfo
- IPhoneNumberUtil
(Refer to the source code for detailed interface definitions.)
The following methods or classes are unavailable on the original JS port of Google's libphonenumber:
- findNumbers - finds numbers in text (useful for highlighting or linking phone numbers inside text messages).
- PhoneNumberOfflineGeocoder - provides geographical information related to a phone number.
- PhoneNumberToCarrierMapper - provides carrier information related to a phone number.
- PhoneNumberToTimeZonesMapper - provides timezone information related to a phone number.
Most of the issues submitted to this repository are related to carrier metadata - things like unexpected phone validations, errors in formatting numbers, unknown carriers and so on.
First, try the same input using the official demo page. If the result is different, then it might mean that a metadata update is due on this package, as the demo page always runs on the latest and official metadata version.
If the result is the same, it means there might be an issue with the currently available metadata. In that case, you should report your issue in the original project's issue tracker (moved out of GitHub on 05/12/2017).
Contributions are welcome! Please open an issue or submit a pull request.
This package is licensed under MIT License.
The bundled libphonenumber library is licensed under Apache 2.0.