What is google-libphonenumber?
The google-libphonenumber npm package is a JavaScript port of Google's libphonenumber library, which provides robust tools for parsing, formatting, and validating international phone numbers. It is designed to help developers work with phone numbers in various formats and provides functionalities such as number validation, formatting numbers for display, and extracting possible number types.
What are google-libphonenumber's main functionalities?
Parsing and validating phone numbers
This feature allows you to parse a phone number from a string and validate whether it is a possible number. The example demonstrates parsing and validating a US phone number.
const { PhoneNumberUtil } = require('google-libphonenumber');
const phoneUtil = PhoneNumberUtil.getInstance();
const number = phoneUtil.parseAndKeepRawInput('202-456-1111', 'US');
const isValid = phoneUtil.isValidNumber(number);
Formatting phone numbers
This feature enables the formatting of parsed phone numbers into various formats, such as international and national formats. The code sample shows how to format a US phone number into an international format.
const { PhoneNumberFormat, PhoneNumberUtil } = require('google-libphonenumber');
const phoneUtil = PhoneNumberUtil.getInstance();
const number = phoneUtil.parse('2024561111', 'US');
const formattedNumber = phoneUtil.format(number, PhoneNumberFormat.INTERNATIONAL);
Getting the type of a number
This functionality allows you to determine the type of a phone number (e.g., mobile, fixed-line). The example demonstrates how to parse a US phone number and get its type.
const { PhoneNumberType, PhoneNumberUtil } = require('google-libphonenumber');
const phoneUtil = PhoneNumberUtil.getInstance();
const number = phoneUtil.parse('202-456-1111', 'US');
const numberType = phoneUtil.getNumberType(number);
Other packages similar to google-libphonenumber
awesome-phonenumber
Similar to google-libphonenumber, awesome-phonenumber provides functionalities for parsing, validating, and formatting international phone numbers. It is lighter and has a simpler API, but might not be as comprehensive in terms of number validation rules and country coverage.
libphonenumber-js
libphonenumber-js is another alternative that offers parsing, formatting, and validation of phone numbers. It aims to be a smaller and faster implementation compared to google-libphonenumber, by focusing on the most common use cases and dropping some of the less frequently used features.
google-libphonenumber
A lightweight wrapper for Google's libphonenumber, a library to parse, format, store and validate international phone numbers.
Status
Installation
Install the package via npm
:
npm install --save google-libphonenumber
Usage
Here's a simple example on how to format a US-based number in the international phone number format:
var PNF = require('google-libphonenumber').PhoneNumberFormat;
var phoneUtil = require('google-libphonenumber').phoneUtil;
var phoneNumber = phoneUtil.parse('202-456-1414', 'US');
console.log('Result is', phoneUtil.format(phoneNumber, PNF.INTERNATIONAL));
Notes
Differencies from other forks
- Added built-in integration with
google-closure
's own node.js wrapper. - Added a simplified update process to keep
libphonenumber
more up-to-date. - Added all exported classes from
libphonenumber
to the module's exports. - Moved
google-closure
to the vendor space to facilitate version tracking and control. - Updated the dependency management system to require
libphonenumber
dependencies directly from google-closure
.
Updating package dependencies
Learn more updating package dependencies.
Errors
The javascript port of libphonenumber
throws errors as string, e.g. throw "Invalid country code"
. As Guillermo Rauch puts it, a string is not an error so, in an attempt to avoid future issues when developing an application, this module converts all string-based errors that occur on the PhoneNumberUtil
class to instances of Error
.
Tests
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 test
Acknowledgements
The original library wrapper was created by Socialcam who first got it working on node.js and then improved by mattbornski. This package would not exist without the work of these previous contributions.
The exceptional work on libphonenumber
was made possible by these committers and contributors.
Licenses
MIT (package) and Apache License 2.0 (libphonenumber).