Code Monkey home page Code Monkey logo

google-libphonenumber's Introduction

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.
  • 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

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.

google-libphonenumber's People

Contributors

giggsey avatar marcacyr avatar ruimarinho avatar scop avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.