What is awesome-phonenumber?
The awesome-phonenumber npm package is a comprehensive library for parsing, formatting, and validating phone numbers. It supports a wide range of international phone number formats and provides utilities for handling phone number data effectively.
What are awesome-phonenumber's main functionalities?
Parsing Phone Numbers
This feature allows you to parse a phone number from a string and create a PhoneNumber object. The example demonstrates how to parse a US phone number.
const PhoneNumber = require('awesome-phonenumber');
const pn = new PhoneNumber('+12025550123');
console.log(pn.getNumber()); // Outputs: +12025550123
Formatting Phone Numbers
This feature allows you to format phone numbers in different formats such as E.164, international, and national. The example shows how to format a US phone number in various formats.
const PhoneNumber = require('awesome-phonenumber');
const pn = new PhoneNumber('+12025550123');
console.log(pn.getNumber('e164')); // Outputs: +12025550123
console.log(pn.getNumber('international')); // Outputs: +1 202-555-0123
console.log(pn.getNumber('national')); // Outputs: (202) 555-0123
Validating Phone Numbers
This feature allows you to validate whether a phone number is valid or not. The example demonstrates how to check the validity of a US phone number.
const PhoneNumber = require('awesome-phonenumber');
const pn = new PhoneNumber('+12025550123');
console.log(pn.isValid()); // Outputs: true
Getting Region Code
This feature allows you to get the region code of a phone number. The example shows how to retrieve the region code for a US phone number.
const PhoneNumber = require('awesome-phonenumber');
const pn = new PhoneNumber('+12025550123');
console.log(pn.getRegionCode()); // Outputs: US
Getting Country Code
This feature allows you to get the country code of a phone number. The example demonstrates how to retrieve the country code for a US phone number.
const PhoneNumber = require('awesome-phonenumber');
const pn = new PhoneNumber('+12025550123');
console.log(pn.getCountryCode()); // Outputs: 1
Other packages similar to awesome-phonenumber
libphonenumber-js
libphonenumber-js is a simpler and smaller alternative to Google's libphonenumber library. It provides similar functionalities for parsing, formatting, and validating phone numbers. Compared to awesome-phonenumber, libphonenumber-js is more lightweight and may be preferable for projects where bundle size is a concern.
google-libphonenumber
google-libphonenumber is a comprehensive library developed by Google for parsing, formatting, and validating international phone numbers. It is the most feature-rich and widely used library for phone number handling. Compared to awesome-phonenumber, google-libphonenumber offers more extensive support and features but comes with a larger bundle size.
phone
The phone package is a simple library for parsing and formatting phone numbers. It is less feature-rich compared to awesome-phonenumber but provides basic functionalities for handling phone numbers. It is suitable for projects that require minimal phone number processing capabilities.
Awesome phonenumber parser
This library is a pre-compiled version of Google's libphonenumber
, with a slightly simpler interface. It has a minimal footprint - is by far the smallest libphonenumber-based library available on npmjs (other libraries are orders of magnitude larger!), and has no dependencies.
Uses libphonenumber 7.2.1
Comparison with other libraries
Since this library is pre-compiled, it doesn't depend on the closure compiler, and needs not load it on start. This makes the library faster and saves you a lot of space. It also means this library is trivial to use in any browserify
project (or using any other means to run in the browser).
Among all the phone number libraries using Google's libphonenumber
, only this one, google-libphonenumber
(0.2.2) and node-phonenumber
(0.2.1) had decent README's with examples. Other libraries embedding the closure compiler should get comparable figures.
google-libphonenumber
and node-phonenumber
naturally become faster once loaded and after first parsing, but for many applications, the first time matters a lot. Loading the closure compiler also adds to the application memory usage (RSS is measured here). The library footprints are also bigger, making npm install
slower and increasing deploy times.
A test program loading a library, then parsing a phone number is called 5 times for each library, the mean values are:
Action | awesome-phonenumber | google-libphonenumber | node-phonenumber |
---|
Load library first time | 21 ms | 101 ms | 83 ms |
Parse first phone number | 5 ms | 11 ms | 8 ms |
Increased memory usage | 9.8 M | 19.1 M | 19.5 M |
node_modules size | 296 K | 16 M | 53 M |
node_modules files | 15 | 967 | 4340 |
time npm install | 0.7 s | 2.4 s | 5.9 s |
Basic usage
var PhoneNumber = require( 'awesome-phonenumber' );
var pn = new PhoneNumber( '0707123456', 'SE' );
pn.isValid( );
pn.isMobile( );
pn.getNumber( );
pn.getNumber( 'e164' );
pn.getNumber( 'international' );
pn.getNumber( 'national' );
pn.getNumber( 'rfc3966' );
pn.getNumber( 'significant' );
pn.toJSON( );
JSON.stringify( pn, null, 4 );
API types
The API consists of the PhoneNumber
class which sometimes uses enums. These are:
Phone number types
'fixed-line'
'fixed-line-or-mobile'
'mobile'
'pager'
'personal-number'
'premium-rate'
'shared-cost'
'toll-free'
'uan'
'voip'
'unknown'
Phone number possibilities
'is-possible'
'invalid-country-code'
'too-long'
'too-short'
'unknown'
Phone number formats
'international'
'national'
'e164'
'rfc3966'
'significant'
API functions
Library
var PhoneNumber = require( 'awesome-phonenumber' );
Country codes
There are conversion functions between the 2-character ISO 3166-1 region codes (e.g. 'SE' for Sweden) and the corresponding country calling codes.
PhoneNumber.getCountryCodeForRegionCode( regionCode );
PhoneNumber.getRegionCodeForCountryCode( countryCode );
Example
PhoneNumber.getCountryCodeForRegionCode( 'SE' );
PhoneNumber.getRegionCodeForCountryCode( 46 );
Phone numbers
An instance of the PhoneNumber
class will be created even if PhoneNumber
is called as a function.
var pn = PhoneNumber( number, regionCode );
var pn = new PhoneNumber( number, regionCode );
PhoneNumber objects can also be created using the getExample( regionCode[, type ] )
function, see section Example phone numbers for country below.
pn.toJSON( );
pn.isValid( );
pn.isPossible( );
pn.getType( );
pn.isMobile( );
pn.isFixedLine( );
pn.getNumber( [ format ] );
pn.getNumberFrom( fromRegionCode );
Example
PhoneNumber( '0707123456', 'SE' ).getNumberFrom( 'JP' );
Example phone numbers for country
Sometimes you want to display a formatted example phone number for a certain country (and maybe also a certain type of phone number). The getExample
function is used for this.
PhoneNumber.getExample( regionCode[, phoneNumberType] );
The phoneNumberType
is any of the types defined above.
Example
PhoneNumber.getExample( 'SE' ).getNumber( );
PhoneNumber.getExample( 'SE', 'mobile' ).getNumber( );
PhoneNumber.getExample( 'SE', 'mobile' ).getNumber( 'national' );
As-you-type formatting
You can use an AsYouType
class to format a phone number as it is being typed. An instance of this class is retrieved by var ayt = PhoneNumber.getAsYouType( regionCode )
, and has the following methods:
ayt.addChar( nextChar );
ayt.number( );
ayt.removeChar( );
ayt.reset( [ number ] );
ayt.getPhoneNumber( );
All the functions above except getPhoneNumber( )
return the current formatted number (as a String of course, as it may include spaces and other characters).
Example
var ayt = PhoneNumber.getAsYouType( 'SE' );
ayt.addChar( '0' );
ayt.addChar( '7' );
ayt.addChar( '0' );
ayt.addChar( '7' );
ayt.addChar( '1' );
ayt.addChar( '2' );
ayt.addChar( '3' );
ayt.addChar( '4' );
ayt.addChar( '5' );
ayt.addChar( '6' );
ayt.removeChar( );
ayt.addChar( '7' );