npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@onereach/phonenumber-interpreter

v0.0.17

Published

Google's libphonenumber pre-compiled with the closure compiler

Downloads

245

Readme

npm version downloads build status Language grade: JavaScript Install size

Basic usage

var PhoneNumber = require( '@onereach/phonenumber-interpreter' );

const phoneNumber = {
  input: '3034567779',
  chosenOption: 'chooseCountry';
  isoCountry: 'us',
  allowedCountries: ['us', 'ca'],
  allowSpecialNumbers: true
};

// chosenOption = 'allowAll' (default) || 'chooseCountry' || 'allowOnlySelected';
// 'isoCountry' is the region code which is taken into account, when the 'chooseCountry' option is chosen;
// 'allowedCountries' is an array which contains allowed country codes & taken into account when the 'allowOnlySelected' option is chosen;
// 'allowSpecialNumbers' is a boolean value which defines whether the toll-free, shared-cost and premium numbers are allowed;

var pn = new PhoneNumber( '3034567779', regionCode = '+1', allowSpecialNumbers = true);
// regionCode & allowSpecialNumbers args are not required. Default values will be assigned, if there are no params;
var pn = new PhoneNumber(phoneNumber);

pn.isValid( );  // -> true
pn.isMobile( ); // -> true
pn.canBeIntlDialled( ); // -> true
pn.getNumber( );                   // -> '+13034567779'
pn.getNumber( 'e164' );            // -> '+13034567779' (default)
pn.getNumber( 'international' );   // -> '+1 303-456-7779'
pn.getNumber( 'national' );        // -> '(303) 456-7779'
pn.getNumber( 'rfc3966' );         // -> 'tel:+1-303-456-7779'
pn.getNumber( 'significant' );     // -> '3034567779'
pn.getRegionCode( );               // -> 'US'
pn.getCountryCode( );              // -> +1

pn.toJSON( );                  // -> json blob, so that:
JSON.stringify( pn, null, 4 ); // -> This:
// {
//     "input": "3034567779",
//     "normalized": "+13034567779"
//     "type": "FIXED_LINE_OR_MOBILE",
//     "canBeInternationallyDialled": true,
//     "possiblyMobile": true,
//     "isSpecial": false,
//     "validatity": {
//       "valid": true,
//       "possible": true,
//       "code": "valid",
//       "message": "Phone number is valid and possible",
//     },
//     "formats": {
//       "e164": "+13034567779",
//       "national": "(303) 456-7779",
//       "international": "+1 303-456-7779",
//       "significant": "3034567779",
//       "rfc3966": "tel:+1-303-456-7779"
//     },
//     "location": {
//       "country": {
//          "name": "United States of America",
//          "iso2": "US",
//          "iso3": "USA",
//          "dialCode": "1"
//        }
//     }
// }

Detect country

When constructed with a phone number on e164 format (i.e. prefixed with a +), awesome-phonenumber will auto-detect the country:

PhoneNumber( '3034567779' ).getRegionCode( ); // -> 'US'

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'
'premium-rate'
'shared-cost'
'toll-free'
'uan'
'voip'
'unknown

Phone number possibilities

'is-possible'
'invalid-country-code'
'too-long'
'too-short'
'unknown'
'special-numbers-disallowed'
'country-not-allowed'
'country-not-matched'

Phone number formats

'international'
'national'
'e164'
'rfc3966'
'significant'

API functions

Library

var PhoneNumber = require( '@onereach/phonenumber-interpreter' );

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 );  // -> countryCode
PhoneNumber.getRegionCodeForCountryCode( countryCode ); // -> regionCode

Example

PhoneNumber.getCountryCodeForRegionCode( 'US' ); // -> +1
PhoneNumber.getRegionCodeForCountryCode( 1 );   // -> 'US'

Supported calling codes

PhoneNumber.getSupportedCallingCodes( ); // -> [ calling codes... ]

Supported region codes

PhoneNumber.getSupportedRegionCodes( ); // -> [ region codes... ]

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, allowSpecialNumbers );
var pn = new PhoneNumber ( phoneNumberObject ); // {input: String, chosenOption: String, isoCountry: String, allowedCountries: [], allowSpecialNumbers: Boolean}

PhoneNumber objects can also be created using the getExample( regionCode[, type ] ) function, see section Example phone numbers for country below.

pn.toJSON( );               // -> json blob as seen in "Basic usage" above
pn.isValid( );              // -> Boolean
pn.isPossible( );           // -> Boolean
pn.getType( );              // -> Any of the "Phone number types" defined above
pn.isMobile( );             // -> true if type is 'mobile' or 'fixed-line-or-mobile'
pn.isFixedLine( );          // -> true if type is 'fixed-line' or 'fixed-line-or-mobile'
pn.getNumber( [ format ] ); // -> Formatted number, see "Basic usage" for examples

// Returns the number formatted to how to dial it from another region.
pn.getNumberFrom( fromRegionCode );

Example

// Calling the American number 3034567779 from Japan:
PhoneNumber( '3034567779' ).getNumberFrom( 'JP' ); // '010 +1+303-456-7779'

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] ); // PhoneNumber object

The phoneNumberType is any of the types defined above.

Example

PhoneNumber.getExample( 'US' ).getNumber( );                      // '+13034567779'
PhoneNumber.getExample( 'SE', 'MOBILE' ).getNumber( );            // '+13034567779'
PhoneNumber.getExample( 'SE', 'MOBILE ).getNumber( 'national' ); // '(303) 456-7779'