/google-libphonenumber

The up-to-date and reliable Google's libphonenumber package for node.js.

Primary LanguageJavaScriptOtherNOASSERTION

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

npm version build 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.

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

Release

npm version [<newversion> | major | minor | patch] -m "Release %s"

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.