Security News
The Push to Ban Ransom Payments Is Gaining Momentum
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
libphonenumbers
Advanced tools
Readme
libphonenumbers is JS port of Google's libphonenumber.
libphonenumbers – JavaScript port of Google's libphonenumber library for parsing, formatting, and validating international phone numbers in Node.js.
libphonenumbers is compatible with both JavaScript and TypeScript.
PhoneNumberUtil
PhoneNumberFormat ( Enums is used to pass to format(number, numberFormat) )
PhoneNumberType ( Enums is used to compare with the output of getNumberType(number):
PhoneNumber
CountryCodeSource ( Enums in order to compare them with the output of getCountryCodeSource() )
ShortNumberInfo
JS port of Google's libphonenumber does not support the following functions and classes:
libphonenumbers is available on npm. It can be installed with the following command:
npm install libphonenumbers --save
libphonenumbers is available on yarn as well. It can be installed with the following command:
yarn add libphonenumbers
Using Standard JavaScript:
const PNF = require('libphonenumbers').PhoneNumberFormat;
// Create an instance of PhoneNumberUtil
const phoneUtil = require('libphonenumbers').PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('300-577-8989', 'US');
// Format number in the RFC3966 format
console.log(phoneUtil.format(number, PNF.RFC3966));
// => tel:+1-300-577-8989
// Format number in the national format
console.log(phoneUtil.format(number, PNF.NATIONAL));
// => (300) 577-8989
// Format number in the international format
console.log(phoneUtil.format(number, PNF.INTERNATIONAL));
// => +1 300-577-8989
Using ECMAScript (ES):
import libphonenumbers from 'libphonenumbers';
const PNF = libphonenumbers.PhoneNumberFormat;
// Create an instance of PhoneNumberUtil
const phoneUtil = libphonenumbers.PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('300-577-8989', 'US');
// Format number in the RFC3966 format
console.log(phoneUtil.format(number, PNF.RFC3966));
// => tel:+1-300-577-8989
// Format number in the national format
console.log(phoneUtil.format(number, PNF.NATIONAL));
// => (300) 577-8989
// Format number in the international format
console.log(phoneUtil.format(number, PNF.INTERNATIONAL));
// => +1 300-577-8989
Using Standard JavaScript:
// Create an instance of PhoneNumberUtil
const phoneUtil = require('libphonenumbers').PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('300-577-8989', 'US');
// Format number in the original format
console.log(phoneUtil.formatInOriginalFormat(number, 'US'));
// => (300) 577-8989
Using ECMAScript (ES):
import libphonenumbers from 'libphonenumbers';
// Create an instance of PhoneNumberUtil
const phoneUtil = libphonenumbers.PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('300-577-8989', 'US');
// Format number in the original format
console.log(phoneUtil.formatInOriginalFormat(number, 'US'));
// => (300) 577-8989
Using Standard JavaScript:
// Create an instance of PhoneNumberUtil
const phoneUtil = require('libphonenumbers').PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('300-577-8989', 'US');
// Format number in the out-of-country format from US
console.log(phoneUtil.formatOutOfCountryCallingNumber(number, 'US'));
// => 1 (300) 577-8989
// Format number in the out-of-country format from JP
console.log(phoneUtil.formatOutOfCountryCallingNumber(number, 'JP'));
// => 010 1 300-577-8989
Using ECMAScript (ES):
import libphonenumbers from 'libphonenumbers';
// Create an instance of PhoneNumberUtil
const phoneUtil = libphonenumbers.PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('300-577-8989', 'US');
// Format number in the out-of-country format from US
console.log(phoneUtil.formatOutOfCountryCallingNumber(number, 'US'));
// => 1 (300) 577-8989
// Format number in the out-of-country format from JP
console.log(phoneUtil.formatOutOfCountryCallingNumber(number, 'JP'));
// => 010 1 300-577-8989
Using Standard JavaScript:
// Create an instance of PhoneNumberUtil
const phoneUtil = require('libphonenumbers').PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US');
// Get type of phone number
console.log(phoneUtil.getNumberType(number));
// => 2 // FIXED_LINE_OR_MOBILE
Using ECMAScript (ES):
import libphonenumbers from 'libphonenumbers';
// Create an instance of PhoneNumberUtil
const phoneUtil = libphonenumbers.PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US');
// Get type of phone number
console.log(phoneUtil.getNumberType(number));
// => 2 // FIXED_LINE_OR_MOBILE
Using Standard JavaScript:
// Create an instance of PhoneNumberUtil
const phoneUtil = require('libphonenumbers').PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US');
// Get region code of number
console.log(phoneUtil.getRegionCodeForNumber(number));
// => US
Using ECMAScript (ES):
import libphonenumbers from 'libphonenumbers';
// Create an instance of PhoneNumberUtil
const phoneUtil = libphonenumbers.PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US');
// Get region code of number
console.log(phoneUtil.getRegionCodeForNumber(number));
// => US
Using Standard JavaScript:
// Create an instance of PhoneNumberUtil
const phoneUtil = require('libphonenumbers').PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US');
// Check is possible number
console.log(phoneUtil.isPossibleNumber(number));
// => true
Using ECMAScript (ES):
import libphonenumbers from 'libphonenumbers';
// Create an instance of PhoneNumberUtil
const phoneUtil = libphonenumbers.PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US');
// Get region code of number
console.log(phoneUtil.getRegionCodeForNumber(number));
// => US
Using Standard JavaScript:
// Create an instance of PhoneNumberUtil
const phoneUtil = require('libphonenumbers').PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US');
// Get is valid number
console.log(phoneUtil.isValidNumber(number));
// => true
Using ECMAScript (ES):
import libphonenumbers from 'libphonenumbers';
// Create an instance of PhoneNumberUtil
const phoneUtil = libphonenumbers.PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US');
// Get is valid number
console.log(phoneUtil.isValidNumber(number));
// => true
Using Standard JavaScript:
// Create an instance of PhoneNumberUtil
const phoneUtil = require('libphonenumbers').PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US');
// Check number of a region is valid
console.log(phoneUtil.isValidNumberForRegion(number, 'US'));
// => true
Using ECMAScript (ES):
import libphonenumbers from 'libphonenumbers';
// Create an instance of PhoneNumberUtil
const phoneUtil = libphonenumbers.PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US');
// Check number of a region is valid
console.log(phoneUtil.isValidNumberForRegion(number, 'US'));
// => true
Using Standard JavaScript:
// Create an instance of PhoneNumberUtil
const phoneUtil = require('libphonenumbers').PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US');
Using ECMAScript (ES):
import libphonenumbers from 'libphonenumbers';
// Create an instance of PhoneNumberUtil
const phoneUtil = libphonenumbers.PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US');
Using Standard JavaScript:
// Create an instance of PhoneNumberUtil
const phoneUtil = require('libphonenumbers').PhoneNumberUtil.getInstance();
// Get prototype buffer format
console.log(phoneUtil.parse('123456', 'US'));
Using ECMAScript (ES):
import libphonenumbers from 'libphonenumbers';
// Create an instance of PhoneNumberUtil
const phoneUtil = libphonenumbers.PhoneNumberUtil.getInstance();
// Get proto buffer format
console.log(phoneUtil.parse('123456', 'US'));
Using Standard JavaScript:
// Create an instance of PhoneNumberUtil
const phoneUtil = require('libphonenumbers').PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US');
// Get the phone's country code
console.log(number.getCountryCode());
// => 1
Using ECMAScript (ES):
import libphonenumbers from 'libphonenumbers';
// Create an instance of PhoneNumberUtil
const phoneUtil = libphonenumbers.PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US');
// Get the phone's country code
console.log(number.getCountryCode());
// => 1
Using Standard JavaScript:
// Create an instance of PhoneNumberUtil
const phoneUtil = require('libphonenumbers').PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US');
// Get the phone's extension
console.log(number.getCountryCodeSource());
// => FROM_DEFAULT_COUNTRY
Using ECMAScript (ES):
import libphonenumbers from 'libphonenumbers';
// Create an instance of PhoneNumberUtil
const phoneUtil = libphonenumbers.PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US');
// Print the phone's extension
console.log(number.getCountryCodeSource());
// => FROM_DEFAULT_COUNTRY
Using Standard JavaScript:
// Create an instance of PhoneNumberUtil
const phoneUtil = require('libphonenumbers').PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US');
// Print the phone's extension.
console.log(number.getExtension());
// => null
Using ECMAScript (ES):
import libphonenumbers from 'libphonenumbers';
// Create an instance of PhoneNumberUtil
const phoneUtil = libphonenumbers.PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US');
// Print the phone's extension.
console.log(number.getExtension());
// => null
Using Standard JavaScript:
// Create an instance of PhoneNumberUtil
const phoneUtil = require('libphonenumbers').PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US');
// Get phone's italian leading zero
console.log(number.getItalianLeadingZero());
// => null
Using ECMAScript (ES):
import libphonenumbers from 'libphonenumbers';
// Create an instance of PhoneNumberUtil
const phoneUtil = libphonenumbers.PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US');
// Get phone's italian leading zero
console.log(number.getItalianLeadingZero());
// => null
Using Standard JavaScript:
// Create an instance of PhoneNumberUtil
const phoneUtil = require('libphonenumbers').PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US');
// Get the phone's national number
console.log(number.getNationalNumber());
// => 2024562121
Using ECMAScript (ES):
import libphonenumbers from 'libphonenumbers';
// Create an instance of PhoneNumberUtil
const phoneUtil = libphonenumbers.PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US');
// Get the phone's national number
console.log(number.getNationalNumber());
// => 2024562121
Using Standard JavaScript:
// Create an instance of PhoneNumberUtil
const phoneUtil = require('libphonenumbers').PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US');
// Get the phone's raw input
console.log(number.getRawInput());
// => 202-456-2121
Using ECMAScript (ES):
import libphonenumbers from 'libphonenumbers';
// Create an instance of PhoneNumberUtil
const phoneUtil = libphonenumbers.PhoneNumberUtil.getInstance();
// Parse number with US country code and keep raw input
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US');
// Get the phone's raw input
console.log(number.getRawInput());
// => 202-456-2121
Using Standard JavaScript:
// Get an instance of ShortNumberInfo
const shortInfo = require('libphonenumbers').ShortNumberInfo.getInstance();
// Check 911 is emergency number in US
console.log(shortInfo.connectsToEmergencyNumber('911', 'US'));
// => true
Using ECMAScript (ES):
import libphonenumbers from 'libphonenumbers';
// Get an instance of ShortNumberInfo
const shortInfo = libphonenumbers.ShortNumberInfo.getInstance();
// Check 911 is emergency number in US
console.log(shortInfo.connectsToEmergencyNumber('911', 'US'));
// => true
Using Standard JavaScript:
// Get an instance of ShortNumberInfo
const shortInfo = require('libphonenumbers').ShortNumberInfo.getInstance();
// Get an instance of PhoneNumberUtil
const phoneUtil = require('libphonenumbers').PhoneNumberUtil.getInstance();
// Check 123456 is possible short number in FR
console.log(shortInfo.isPossibleShortNumber(phoneUtil.parse('123456', 'FR')));
// => true
Using ECMAScript (ES):
import libphonenumbers from 'libphonenumbers';
// Get an instance of ShortNumberInfo
const shortInfo = libphonenumbers.ShortNumberInfo.getInstance();
// Get an instance of PhoneNumberUtil
const phoneUtil = libphonenumbers.PhoneNumberUtil.getInstance();
// Check 123456 is possible short number in FR
console.log(shortInfo.isPossibleShortNumber(phoneUtil.parse('123456', 'FR')));
// => true
Using Standard JavaScript:
// Get an instance of ShortNumberInfo
const shortInfo = require('libphonenumbers').ShortNumberInfo.getInstance();
// Get an instance of PhoneNumberUtil
const phoneUtil = require('libphonenumbers').PhoneNumberUtil.getInstance();
// Check 123456 is possible short number for region in FR
console.log(shortInfo.isPossibleShortNumberForRegion(phoneUtil.parse('123456', 'FR'), 'FR'));
// => true
Using ECMAScript (ES):
import libphonenumbers from 'libphonenumbers';
// Get an instance of ShortNumberInfo
const shortInfo = libphonenumbers.ShortNumberInfo.getInstance();
// Get an instance of PhoneNumberUtil
const phoneUtil = libphonenumbers.PhoneNumberUtil.getInstance();
// Check 123456 is possible short number for region in FR
console.log(shortInfo.isPossibleShortNumberForRegion(phoneUtil.parse('123456', 'FR'), 'FR'));
// => true
Inspired by Google's libphonenumber.
FAQs
JavaScript port of Google's libphonenumber library for parsing, formatting, and validating international phone numbers in Node.js.
The npm package libphonenumbers receives a total of 1,650 weekly downloads. As such, libphonenumbers popularity was classified as popular.
We found that libphonenumbers demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Ransomware costs victims an estimated $30 billion per year and has gotten so out of control that global support for banning payments is gaining momentum.
Application Security
New SEC disclosure rules aim to enforce timely cyber incident reporting, but fear of job loss and inadequate resources lead to significant underreporting.
Security News
The Python Software Foundation has secured a 5-year sponsorship from Fastly that supports PSF's activities and events, most notably the security and reliability of the Python Package Index (PyPI).