What is libphonenumber-js?
The libphonenumber-js npm package is a JavaScript library that provides a variety of functions for handling and manipulating phone numbers. It is based on Google's libphonenumber library and allows users to parse, format, and validate phone numbers in international formats.
What are libphonenumber-js's main functionalities?
Parsing and formatting phone numbers
This feature allows users to parse a string into a phone number object and format it into an international format.
import { parsePhoneNumberFromString } from 'libphonenumber-js';
const phoneNumber = parsePhoneNumberFromString('+12133734253');
console.log(phoneNumber.formatInternational()); // '+1 213 373 4253'
Validating phone numbers
This feature enables users to check if a given phone number is valid according to international standards.
import { isValidPhoneNumber } from 'libphonenumber-js';
const valid = isValidPhoneNumber('+12133734253');
console.log(valid); // true
Getting phone number information
This feature provides information about the phone number, such as the country it is associated with and the type of number (e.g., mobile, fixed-line).
import { parsePhoneNumberFromString } from 'libphonenumber-js';
const phoneNumber = parsePhoneNumberFromString('+12133734253');
console.log(phoneNumber.country); // 'US'
console.log(phoneNumber.getType()); // 'MOBILE'
Other packages similar to libphonenumber-js
google-libphonenumber
This is the original phone number handling library from Google that libphonenumber-js is based on. It is more feature-rich but also larger in size, which might not be ideal for front-end usage due to its impact on bundle size.
awesome-phonenumber
Another library for phone number parsing, validation, and formatting. It provides similar functionalities to libphonenumber-js but with a different API design and potentially different bundle size implications.
libphonenumber-js
[![NPM Version][npm-badge]][npm]
[![Build Status][travis-badge]][travis]
[![Test Coverage][coveralls-badge]][coveralls]
A simpler (and smaller) rewrite of Google Android's famous libphonenumber
library.
Work in Progress, will be released in the next few days
LibPhoneNumber
libphonenumber
is a phone number formatting and parsing library released by Google, originally developed for (and currently used in) Google's Android mobile phone operating system. Obviously, implementing a rigorous phone number formatting and parsing library was crucial for the phone OS overall usability (back then, in the early 2000s, it was originally meant to be a phone after all, not just a SnapChat device).
libphonenumber-js
is a simplified javascript port of the original libphonenumber
library (written in C++ and Java because those are the programming languages used in Android OS). While libphonenumber
has an official javascript port which is being maintained by Google, it is tightly coupled to Google's closure
javascript utility framework. It still can be compiled into one big bundle which weighs 220 KiloBytes — quite a size for a phone number input component. It can be customized too in a sense of which countries metadata to include but that wouldn't be an option for a worldwide solution.
One part of me was curious about how all this phone matching machinery worked, and another part of me was curious if there's a way to reduce those 220 KiloBytes to something more reasonable while also getting rid of the closure
library and rewrite it all in pure javascript. So, that was my little hackathon for a couple of weeks, and seems that it succeeded. The resulting library does everything a modern web application needs while maintaining a much slimmer size of about 70 KiloBytes.
Installation
npm install libphonenumber-js --save
Usage
import { parse } from 'libphonenumber-js'
parse('8 (800) 555 35 35', 'RU') === { country: 'RU', phone: '8005553535' }
API
parse(text, options)
options
can be
country:
{
restrict — (a two-letter country code)
assume the phone number is for this country
default — (a two-letter country code)
default country to use for phone number parsing and validation
(if no country code could be derived from the phone number)
}
or just a two-letter country code which is gonna be country.restrict
.
Returns { country, phone }
where country
is a two-letter country code, and phone
is a national (significant) number.
Contributing
After cloning this repo, ensure dependencies are installed by running:
npm install
This module is written in ES6 and uses Babel for ES5
transpilation. Widely consumable JavaScript can be produced by running:
npm run build
Once npm run build
has run, you may import
or require()
directly from
node.
After developing, the full test suite can be evaluated by running:
npm test
When you're ready to test your new functionality on a real project, you can run
npm pack
It will build
, test
and then create a .tgz
archive which you can then install in your project folder
npm install [module name with version].tar.gz
License
MIT
[npm]: https://www.npmjs.org/package/libphonenumber-js
[npm-badge]: https://img.shields.io/npm/v/libphonenumber-js.svg?style=flat-square
[travis]: https://travis-ci.org/halt-hammerzeit/libphonenumber-js
[travis-badge]: https://img.shields.io/travis/halt-hammerzeit/libphonenumber-js/master.svg?style=flat-square
[coveralls]: https://coveralls.io/r/halt-hammerzeit/libphonenumber-js?branch=master
[coveralls-badge]: https://img.shields.io/coveralls/halt-hammerzeit/libphonenumber-js/master.svg?style=flat-square