Package Exports
- google-libphonenumber
- google-libphonenumber/dist/libphonenumber
- google-libphonenumber/dist/libphonenumber.js
- google-libphonenumber/package.json
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (google-libphonenumber) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
google-libphonenumber
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.
Status
Installation
Install the package via npm
:
npm install google-libphonenumber --save
Usage
Here'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
Using the "As You Type" Formatter
// 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();
Notes
Differences from other forks
- All classes available from
libphonenumber
are exported as-is. No magic methods. - Always based on the latest
google-closure
library version available from Google with performance and bug fixes. - Relies on a simplified and well documented update process to keep the underlying
libphonenumber
library always up-to-date. - Throws errors instead of strings.
Errors
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.
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
Acknowledgments
The exceptional work on libphonenumber
was made possible by these committers and contributors.
Licenses
This package is licensed under MIT. The bundled libphonenumber library is licensed under Apache 2.0.