Gathering detailed insights and metrics for google-libphonenumber
Gathering detailed insights and metrics for google-libphonenumber
Gathering detailed insights and metrics for google-libphonenumber
Gathering detailed insights and metrics for google-libphonenumber
@types/google-libphonenumber
TypeScript definitions for google-libphonenumber
yup-phone
Adds a phone number validation check to yup validator using google-libphonenumber.
tel-fns
`tel-fns` just contains convenience functions/constants for handling phone numbers using the Javascript implementation of Google's phone number lib, called: `google-libphonenumber`. This should probably be it's own repo (why it shouldn't be prefixed with
@tepez/joi-phone-number-extensions
A joi extension for validating and formatting phone numbers using google-libphonenumber
The up-to-date and reliable Google's libphonenumber package for node.js.
npm install google-libphonenumber
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
1,408 Stars
496 Commits
146 Forks
31 Watching
13 Branches
13 Contributors
Updated on 27 Nov 2024
Minified
Minified + Gzipped
JavaScript (99.02%)
HTML (0.63%)
Shell (0.35%)
Cumulative downloads
Total Downloads
Last day
-5.5%
205,331
Compared to previous day
Last week
2.6%
1,169,671
Compared to previous week
Last month
4.9%
4,934,619
Compared to previous month
Last year
5.7%
52,742,934
Compared to previous year
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
:
1npm 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.
⚠️ Most libphonenumber functions expect to receive an instance of libphonenumber.PhoneNumber
which can be obtained by calling phoneUtil.parse
or phoneUtil.parseAndKeepRawInput
on a raw (string) number, otherwise it will throw errors like TypeError: a.getCountryCodeOrDefault is not a function
.
This will work:
1phoneUtil.isValidNumberForRegion(phoneUtil.parse('202-456-1414', 'US'), 'US');
This will not work:
1phoneUtil.isValidNumberForRegion('202-456-1414', 'US');
More API examples after parsing the raw string:
1// Require `PhoneNumberFormat`. 2const PNF = require('google-libphonenumber').PhoneNumberFormat; 3 4// Get an instance of `PhoneNumberUtil`. 5const phoneUtil = require('google-libphonenumber').PhoneNumberUtil.getInstance(); 6 7// Parse number with country code and keep raw input. 8const number = phoneUtil.parseAndKeepRawInput('202-456-1414', 'US'); 9 10// Print the phone's country code. 11console.log(number.getCountryCode()); 12// => 1 13 14// Print the phone's national number. 15console.log(number.getNationalNumber()); 16// => 2024561414 17 18// Print the phone's extension. 19console.log(number.getExtension()); 20// => 21 22// Print the phone's extension when compared to i18n.phonenumbers.CountryCodeSource. 23console.log(number.getCountryCodeSource()); 24// => FROM_DEFAULT_COUNTRY 25 26// Print the phone's italian leading zero. 27console.log(number.getItalianLeadingZero()); 28// => false 29 30// Print the phone's raw input. 31console.log(number.getRawInput()); 32// => 202-456-1414 33 34// Result from isPossibleNumber(). 35console.log(phoneUtil.isPossibleNumber(number)); 36// => true 37 38// Result from isValidNumber(). 39console.log(phoneUtil.isValidNumber(number)); 40// => true 41 42// Result from isValidNumberForRegion(). 43console.log(phoneUtil.isValidNumberForRegion(number, 'US')); 44// => true 45 46// Result from getRegionCodeForNumber(). 47console.log(phoneUtil.getRegionCodeForNumber(number)); 48// => US 49 50// Result from getNumberType() when compared to i18n.phonenumbers.PhoneNumberType. 51console.log(phoneUtil.getNumberType(number)); 52// => FIXED_LINE_OR_MOBILE 53 54// Format number in the E164 format. 55console.log(phoneUtil.format(number, PNF.E164)); 56// => +12024561414 57 58// Format number in the original format. 59console.log(phoneUtil.formatInOriginalFormat(number, 'US')); 60// => (202) 456-1414 61 62// Format number in the national format. 63console.log(phoneUtil.format(number, PNF.NATIONAL)); 64// => (202) 456-1414 65 66// Format number in the international format. 67console.log(phoneUtil.format(number, PNF.INTERNATIONAL)); 68// => +1 202-456-1414 69 70// Format number in the out-of-country format from US. 71console.log(phoneUtil.formatOutOfCountryCallingNumber(number, 'US')); 72// => 1 (202) 456-1414 73 74// Format number in the out-of-country format from CH. 75console.log(phoneUtil.formatOutOfCountryCallingNumber(number, 'CH')); 76// => 00 1 202-456-1414
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.
1// Require `AsYouTypeFormatter`. 2const AsYouTypeFormatter = require('google-libphonenumber').AsYouTypeFormatter; 3const formatter = new AsYouTypeFormatter('US'); 4 5console.log(formatter.inputDigit('2')); // => 2 6console.log(formatter.inputDigit('0')); // => 20 7console.log(formatter.inputDigit('2')); // => 202 8console.log(formatter.inputDigit('-')); // => 202- 9console.log(formatter.inputDigit('4')); // => 202-4 10console.log(formatter.inputDigit('5')); // => 202-45 11console.log(formatter.inputDigit('6')); // => 202-456 12console.log(formatter.inputDigit('-')); // => 202-456- 13console.log(formatter.inputDigit('1')); // => 202-456-1 14console.log(formatter.inputDigit('4')); // => 202-456-14 15console.log(formatter.inputDigit('1')); // => 202-456-141 16console.log(formatter.inputDigit('4')); // => 202-456-1414 17 18// Cleanup all input digits from instance. 19formatter.clear();
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.
Highlights:
The type of the phone returned after a string number has been parsed via PhoneNumberUtil.parse()
or PhoneNumberUtil.parseAndKeepRawInput()
.
Highlights:
Lists the following enums in order to compare them with the output of Phone.getCountryCodeSource()
:
CountryCodeSource.UNSPECIFIED
CountryCodeSource.FROM_NUMBER_WITH_PLUS_SIGN
CountryCodeSource.FROM_NUMBER_WITH_IDD
CountryCodeSource.FROM_NUMBER_WITHOUT_PLUS_SIGN
CountryCodeSource.FROM_DEFAULT_COUNTRY
Lists the following enums in order to pass them to PhoneNumberUtil.format()
:
PhoneNumberFormat.E164
PhoneNumberFormat.INTERNATIONAL
PhoneNumberFormat.NATIONAL
PhoneNumberFormat.RFC3966
Lists the following enums in order to compare them with the output of PhoneNumberUtil.getNumberType()
:
PhoneNumberType.FIXED_LINE
PhoneNumberType.MOBILE
PhoneNumberType.FIXED_LINE_OR_MOBILE
PhoneNumberType.TOLL_FREE
PhoneNumberType.PREMIUM_RATE
PhoneNumberType.SHARED_COST
PhoneNumberType.VOIP
PhoneNumberType.PERSONAL_NUMBER
PhoneNumberType.PAGER
PhoneNumberType.UAN
PhoneNumberType.VOICEMAIL
PhoneNumberType.UNKNOWN
Highlights:
1// Get an instance of `ShortNumberInfo`. 2const shortInfo = require('google-libphonenumber').ShortNumberInfo.getInstance(); 3 4// Get an instance of `PhoneNumberUtil`. 5const phoneUtil = require('google-libphonenumber').PhoneNumberUtil.getInstance(); 6 7// Result from connectsToEmergencyNumber(). 8console.log(shortInfo.connectsToEmergencyNumber('911', 'US')); 9// => true 10 11// Result from isPossibleShortNumber(). 12console.log(shortInfo.isPossibleShortNumber(phoneUtil.parse('123456', 'FR'))); 13// => true 14 15// Result from isPossibleShortNumberForRegion(). 16console.log(shortInfo.isPossibleShortNumberForRegion(phoneUtil.parse('123456', 'FR'), 'FR')); 17// => true
The following methods or classes are unavailable on the original JS port of Google's libphonenumber:
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.
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.
libphonenumber
are exported as-is. No magic methods.google-closure
library version available from Google with performance and bug fixes.libphonenumber
library 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.
Google Closure Compiler API, a serviced provided by Google to compile code online via its Closure library, may not always return fully compliant UTF-8-encoded output.
Loading extensions using this library on Google Chrome and other Chromium-based browsers may result in the following error when compiled with webpack:
Could not load file 'file.js' for content script. It isn't UTF-8 encoded.
While the local Java-based version supports a parameter which would let us workaround this issue at the source using --charset=US-ASCII
, the online API version, which is a lot more convenient to use, does not offer support for an equivalent parameter (e.g. output_charset=US-ASCII
).
In order to workaround this issue when using webpack, make sure to output US-ASCII characters only when defining TerserPlugin
options, as demonstrated below:
1optimization: { 2 minimize: process.env.NODE_ENV !== 'development', 3 minimizer: [ 4 new TerserPlugin({ 5 terserOptions: { 6 output: { 7 ascii_only: true 8 } 9 }, 10 }), 11 ] 12}
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.
1npm test
1npm 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.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
Found 0/19 approved changesets -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
security policy file not detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
12 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More