libphonenumbers
libphonenumbers – Google's common JavaScript library for parsing, formatting, and validating international phone numbers for Node.js.
libphonenumbers is JS port of Google's libphonenumber.
🎁 Features
libphonenumbers is compatible with both JavaScript and TypeScript.
-
PhoneNumberUtil
- format(number, numberFormat)
- formatInOriginalFormat(number, regionCallingFrom)
- formatOutOfCountryCallingNumber(number, regionCallingFrom)
- getNumberType(number)
- getRegionCodeForNumber(number)
- isPossibleNumber(number)
- isValidNumber(number)
- isValidNumberForRegion(number, regionCode)
- parseAndKeepRawInput(numberToParse, defaultRegion)
- parse(numberToParse, defaultRegion)
-
ShortNumberInfo
- connectsToEmergencyNumber(number, regionCode)
- isPossibleShortNumberForRegion(number, regionDialingFrom)
- isValidShortNumber(number)
- isPossibleShortNumber(number)
- isValidShortNumberForRegion(number, regionDialingFrom)
-
PhoneNumberType
- FIXED_LINE
- MOBILE
- SHARED_COST
- FIXED_LINE_OR_MOBILE
- TOLL_FREE
- PREMIUM_RATE
- PAGER
- VOIP
- PERSONAL_NUMBER
- VOICEMAIL
- UAN
- UNKNOWN
-
PhoneNumberFormat
- PhoneNumberFormat.E164
- PhoneNumberFormat.INTERNATIONAL
- PhoneNumberFormat.NATIONAL
- PhoneNumberFormat.RFC3966
-
CountryCodeSource
- UNSPECIFIED
- FROM_NUMBER_WITH_IDD
- FROM_NUMBER_WITHOUT_PLUS_SIGN
- FROM_NUMBER_WITH_PLUS_SIGN
- FROM_DEFAULT_COUNTRY
-
PhoneNumber
- getCountryCode()
- getCountryCodeSource()
- getExtension()
- getItalianLeadingZero()
- getNationalNumber()
- getRawInput()
❌ Missing Features:
JS port of Google's libphonenumber does not support the following functions and classes:
- findNumbers
- PhoneNumberOfflineGeocoder
- PhoneNumberToTimeZonesMapper
- PhoneNumberToCarrierMapper
🔧 Install
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
💡 Usage
🎀 PhoneNumberUtil
format(number, numberFormat)
Using Standard JavaScript:
const PNF = require('libphonenumbers').PhoneNumberFormat;
const phoneUtil = require('libphonenumbers').PhoneNumberUtil.getInstance();
const number = phoneUtil.parseAndKeepRawInput('300-577-8989', 'US');
console.log(phoneUtil.format(number, PNF.RFC3966));
console.log(phoneUtil.format(number, PNF.NATIONAL));
console.log(phoneUtil.format(number, PNF.INTERNATIONAL));
Using ECMAScript (ES):
import libphonenumbers from 'libphonenumbers';
const PNF = libphonenumbers.PhoneNumberFormat;
const phoneUtil = libphonenumbers.PhoneNumberUtil.getInstance();
const number = phoneUtil.parseAndKeepRawInput('300-577-8989', 'US');
console.log(phoneUtil.format(number, PNF.RFC3966));
console.log(phoneUtil.format(number, PNF.NATIONAL));
console.log(phoneUtil.format(number, PNF.INTERNATIONAL));
🦄 Credit and Inspiration
Inspired by Google's libphonenumber.
⚖️ License
The MIT License