The up-to-date and reliable Google's libphonenumber package for node.js. Zero dependencies.
Google's libphonenumber is a library that parses, formats, stores and validates international phone numbers. It is used by Android since version 4.0 and is a phenomenal repository of carrier metadata.
Although it compiles down to Java, C++ and JS, its JS port is tightly coupled to the Google Closure library. This makes it more difficult to directly require and use the code on a node.js project.
Google eventually started publishing google-closure-library directly to NPM, ending years of ill-maintained community packages. However, running the original library on node.js remains a cumbersome process.
After all these years, Google's libphonenumber is still not officially available on NPM. What is the best way to use Google's libphonenumber on node.js then? If you're looking for a convenient and easy method, that's what this package is all about.
Install the package via
npm install --save-prod google-libphonenumber
The following is a simple phone information extraction example similar to what can be viewed on the official demo page.
// Require `PhoneNumberFormat`.const PNF = PhoneNumberFormat;// Get an instance of `PhoneNumberUtil`.const phoneUtil = PhoneNumberUtil;// Parse number with country code and keep raw input.const number = phoneUtil;// Print the phone's country code.console;// => 1// Print the phone's national number.console;// => 2024561414// Print the phone's extension.console;// =>// Print the phone's extension when compared to i18n.phonenumbers.CountryCodeSource.console;// => FROM_DEFAULT_COUNTRY// Print the phone's italian leading zero.console;// => false// Print the phone's raw input.console;// => 202-456-1414// Result from isPossibleNumber().console;// => true// Result from isValidNumber().console;// => true// Result from isValidNumberForRegion().console;// => true// Result from getRegionCodeForNumber().console;// => US// Result from getNumberType() when compared to i18n.phonenumbers.PhoneNumberType.console;// => FIXED_LINE_OR_MOBILE// Format number in the E164 format.console;// => +12024561414// Format number in the original format.console;// => (202) 456-1414// Format number in the national format.console;// => (202) 456-1414// Format number in the international format.console;// => +1 202-456-1414// Format number in the out-of-country format from US.console;// => 1 (202) 456-1414// Format number in the out-of-country format from CH.console;// => 00 1 202-456-1414
Using the "As You Type" Formatter
The "As You Type" formatter is a specialized tool that show the formatting progress as it attempts to discover the right format for the given number. It requires registering every keystroke (input digit) on a new instance of the
AsYouTypeFormatter as shown below.
// Require `AsYouTypeFormatter`.const AsYouTypeFormatter = AsYouTypeFormatter;const formatter = 'US';console; // => 2console; // => 20console; // => 202console; // => 202-console; // => 202-4console; // => 202-45console; // => 202-456console; // => 202-456-console; // => 202-456-1console; // => 202-456-14console; // => 202-456-141console; // => 202-456-1414// Cleanup all input digits from instance.formatterclear;
A quick glance at Google's libphonenumber rich API. Descriptions sourced from original files.
The class that offers the main utilities to work with phone numbers, such as formatting, parsing and validating.
- format(number, numberFormat) - formats a phone number in the specified format using default rules.
- formatInOriginalFormat(number, regionCallingFrom) - formats a phone number using the original phone number format that the number is parsed from.
- formatOutOfCountryCallingNumber(number, regionCallingFrom) - formats a phone number for out-of-country dialing purposes.
- getNumberType(number) - gets the type of a valid phone number.
- getRegionCodeForNumber(number) - returns the region where a phone number is from.
- isPossibleNumber(number) - returns true if the number is either a possible fully-qualified number (containing the area code and country code), or if the number could be a possible local number (with a country code, but missing an area code).
- isValidNumber(number) - tests whether a phone number matches a valid pattern.
- isValidNumberForRegion(number, regionCode) - tests whether a phone number is valid for a certain region.
- parseAndKeepRawInput(numberToParse, defaultRegion) - parses a string and returns it in proto buffer format while keeping the raw input value.
- parse(numberToParse, defaultRegion) - parses a string and returns it in proto buffer format.
The type of the phone returned after a string number has been parsed via
Lists the following enums in order to compare them with the output of
Lists the following enums in order to pass them to
Lists the following enums in order to compare them with the output of
Unavailable methods and classes
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.
- ShortNumberInfo - enables short number informatio such as detecting if a number is an emergency number or if it connects to emergency services.
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).
This note will be posted on every issue regarding metadata troubles and it will be automatically closed.
Differences from other packages
google-libphonenumber does not try to be more than what it really is - a pre-compiled Google libphonenumber bundle that works on node.js. It is a 1:1 mirror of the original library without any further simplifications or optimizations.
- All classes available from
libphonenumberare exported as-is. No magic methods.
- Always based on the latest
google-closurelibrary version available from Google with performance and bug fixes.
- Relies on a simplified and well-documented update process to keep the underlying
libphonenumberlibrary always up-to-date.
If you're looking for a slightly simpler API, you should try awesome-phonenumber. It is based on the same concepts of this package but changes the API in order to make it more user friendly. You run the risk of bumping into other bugs and you'll have to learn new API types, but that's the necessary trade-off that the author made for achieving a generally better looking API.
libphonenumber-js is a much more radical approach to Google's libphonenumber. It is a rewrite of the original library based on its source phone metadata but implemented without depending on the Google Closure library. It also offers a tool to reduce the metadata to a set of countries which might be useful for frontend projects. It has several caveats, many of which make a lot of sense depending on the project, but you will have to ascertain those yourself.
There have been some users reporting successful but also unsuccessful usage with Webpack. While I don't personally use it, I'm 100% supportive of pull requests adding modifications that allow this package to better interact with it.
A small subset of tests guarantees that the main library functions are working as expected and are correctly exported. The actual heavy lifting is done by
libphonenumber's extensive test suite.
npm version [<newversion> | major | minor | patch] -m "Release %s"
The exceptional work on
libphonenumber was made possible by these committers and contributors.
This package is licensed under MIT. The bundled libphonenumber library is licensed under Apache 2.0.