A browserify-compatible wrapper for Google's libphonenumber, a library to parse, format, store and validate international phone numbers. Zero dependencies, always up-to-date.
Install the package via npm:
npm install google-libphonenumber --saveHere's a simple example on how to format a US-based number in the international phone number format:
// Require `PhoneNumberFormat`.
var PNF = require('google-libphonenumber').PhoneNumberFormat;
// Get an instance of `PhoneNumberUtil`.
var phoneUtil = require('google-libphonenumber').PhoneNumberUtil.getInstance();
// Parse number with country code.
var phoneNumber = phoneUtil.parse('202-456-1414', 'US');
// Print number in the international format.
console.log(phoneUtil.format(phoneNumber, PNF.INTERNATIONAL));
// => +1 202-456-1414// Require `AsYouTypeFormatter`.
var AsYouTypeFormatter = require('google-libphonenumber').AsYouTypeFormatter;
var formatter = new AsYouTypeFormatter('US');
console.log(formatter.inputDigit('6')); // => 6
console.log(formatter.inputDigit('5')); // => 65
console.log(formatter.inputDigit('0')); // => 650
console.log(formatter.inputDigit('2')); // => 650-2
console.log(formatter.inputDigit('5')); // => 650-25
console.log(formatter.inputDigit('3')); // => 650-253
console.log(formatter.inputDigit('2')); // => 650-2532
console.log(formatter.inputDigit('2')); // => (650) 253-22
formatter.clear();- 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. - Throws errors instead of strings.
The javascript port of the original libphonenumber library throws errors as string, e.g. throw "Invalid country code" and, as Guillermo Rauch puts it, a string is not an error. However, the libphonenumber team has refused change this behaviour due to breaking changes, so a patch is applied on top of the source files to make sure that stacktraces are available and useful.
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 testnpm 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.