telefonicaid/libphonenumber-js

Name: libphonenumber-js

Owner: Telefónica I+D

Description: Builds a compact javascript version of libphonenumber without requiring to use Closure

Created: 2015-05-18 13:36:04.0

Updated: 2017-05-26 14:57:04.0

Pushed: 2016-10-08 16:19:03.0

Homepage: null

Size: 157

Language: Shell

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

libphonenumber

TL;DR run ./build.sh <version> to generate the bundle file

Google provides a libphonenumber version for Javascript however it's implemented using Google's Closure framework, which allows the Closure compiler to compress the generated code greatly, since methods not used are removed and those used get their names mangled. The result is that we cannot directly use the library once optimized from outside Closure code.

Unfortunately using other minifiers on the un-minified library code doesn't give good enough results, so the only real way to get a minified build is to use the Closure compiler and somehow expose the APIs we're interested in.

How to use

When including the generated file on your code it'll create a global variable named libphonenumber to hold all the entities in the library. It's equivalent to the i18n.phonenumbers namespace when working with it using Google Closure.

The Javascript version is a port of the Java one so the easier way to know the API is to check the Javadoc generated documentation.

How it works

In bootstrap.js we have defined for export the whole public API of the library, the code is pretty straight forward, just making sure we alias with the normal names the mangled ones.

Then on build.sh we send this file to Google's Closure compiler service referencing the required libphonenumber files from the GitHub repository, hence we can easily update to new versions. All the compilation is done remotely to avoid the need to install any dependency locally. The result is a file named libphonenumber.js which contains a highly minified version of the code.

phoneUtil = libphonenumber.PhoneNumberUtil.getInstance();
phone = phoneUtil.parse('+447568116268');
ole.log(phone.getNationalNumber());
ole.log(phoneUtil.format(phone));

How to sync to changes between versions

Usually the only thing that changes is the metadata, in that case we just have to run the build script again to get an updated version.

When some methods or enums are changed then we need to reflect those changes on bootstrap.js, it's pretty easy to do and probably is not worth to automate the process. To know what changed on new versions you can refer to the release notes.

Obviously if Google refactors the structure of the code, its file names and so on, then the build script will need to reflect those changes.

License

GNU Affero General Public License 3, see the LICENSE file for details.

The bundled libphonenumber is copyright The Libphonenumber Authors and licensed under Apache License 2.0. See its license file for details.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.