libphonenumbers
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.
🎁 Features
libphonenumbers is compatible with both JavaScript and TypeScript.
-
PhoneNumberUtil
- format(number, numberFormat) is used to formats a phone number in the specified format using default rules.
- formatInOriginalFormat(number, regionCallingFrom) is used to formats a phone number using the original phone number format that the number is parsed from.
- formatOutOfCountryCallingNumber(number, regionCallingFrom) is used to formats a phone number for out-of-country dialing purposes.
- getNumberType(number) is used to gets the type of a valid phone number.
- getRegionCodeForNumber(number) is used to gets the region where a phone number is from.
- isPossibleNumber(number) is used to checks whether a phone number is possible.
- isValidNumber(number) is used to checks whether a phone number matches a valid pattern.
- isValidNumberForRegion(number, regionCode) is used to checks whether a phone number is valid for a certain region.
- parseAndKeepRawInput(numberToParse, defaultRegion) is used to parses a string and returns it in prototype buffer format while keeping the raw input value.
- parse(numberToParse, defaultRegion) is used to parses a string and returns it in proto buffer format.
-
AsYouTypeFormatter
- inputDigit(digit) is used to formats a phone number on-the-fly as each digit is entered.
- clear() is used to clears the internal state of the formatter, so it can be reused.
-
PhoneNumberFormat ( The following enums is used to pass to format(number, numberFormat) )
- E164 (value is 0)
- INTERNATIONAL (value is 1)
- NATIONAL (value is 2)
- RFC3966 (value is 3)
-
PhoneNumberType ( The following enums is used to compare with the output of getNumberType(number) )
- FIXED_LINE (value is 0)
- MOBILE (value is 1)
- FIXED_LINE_OR_MOBILE (value is 2)
- TOLL_FREE (value is 3)
- PREMIUM_RATE (value is 4)
- SHARED_COST (value is 5)
- VOIP (value is 6)
- PERSONAL_NUMBER (value is 7)
- PAGER (value is 8)
- UAN (value is 9)
- VOICEMAIL (value is 10)
- UNKNOWN (value is -1)
-
PhoneNumber
- getCountryCode() is used to gets the value of the country code.
- getCountryCodeSource() is used to gets the value of the country code source.
- getExtension() is used to gets the value of the extension.
- getItalianLeadingZero() is used to gets the value of the italian leading zero.
- getNationalNumber() is used to gets the value of the national number.
- getRawInput() is used to gets the value of the raw input.
-
CountryCodeSource ( The following enums is used to compare with the output of getCountryCodeSource() )
- UNSPECIFIED (value is 0)
- FROM_NUMBER_WITH_PLUS_SIGN (value is 1)
- FROM_NUMBER_WITH_IDD (value is 5)
- FROM_NUMBER_WITHOUT_PLUS_SIGN (value is 10)
- FROM_DEFAULT_COUNTRY (value is 20)
-
ShortNumberInfo
- connectsToEmergencyNumber(number, regionCode) is used to checks whether the short number can be used to connect to emergency services when dialed from the given region.
- isPossibleShortNumber(number) is used to checks whether a short number is a possible number.
- isPossibleShortNumberForRegion(number, regionDialingFrom) is used to checks whether a short number is a possible number when dialed from the given region.
- isValidShortNumber(number) is used to checks whether a short number is a valid number.
- isValidShortNumberForRegion(number, regionDialingFrom) is used to checks whether a short number matches a valid pattern in a region.
❌ 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 = PhoneNumberFormat;// Create an instance of PhoneNumberUtilconst phoneUtil = PhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Format number in the RFC3966 formatconsole;// tel:+1-202-456-2121 // Format number in the national formatconsole;// (202) 456-2121 // Format number in the international formatconsole;// +1 202-456-2121
Using ECMAScript (ES):
; const PNF = libphonenumbersPhoneNumberFormat; // Create an instance of PhoneNumberUtilconst phoneUtil = libphonenumbersPhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Format number in the RFC3966 formatconsole;// tel:+1-202-456-2121 // Format number in the national formatconsole;// (202) 456-2121 // Format number in the international formatconsole;// +1 202-456-2121
📦 formatInOriginalFormat(number, regionCallingFrom)
Using Standard JavaScript:
// Create an instance of PhoneNumberUtilconst phoneUtil = PhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Format number in the original formatconsole;// => (202) 456-2121
Using ECMAScript (ES):
; // Create an instance of PhoneNumberUtilconst phoneUtil = libphonenumbersPhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Format number in the original formatconsole;// (202) 456-2121
📦 formatOutOfCountryCallingNumber(number, regionCallingFrom)
Using Standard JavaScript:
// Create an instance of PhoneNumberUtilconst phoneUtil = PhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Format number in the out-of-country format from USconsole;// 1 (202) 456-2121 // Format number in the out-of-country format from JPconsole;// 010 1 202-456-2121
Using ECMAScript (ES):
; // Create an instance of PhoneNumberUtilconst phoneUtil = libphonenumbersPhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Format number in the out-of-country format from USconsole;// 1 (202) 456-2121 // Format number in the out-of-country format from JPconsole;// 010 1 202-456-2121
📦 getNumberType(number)
Using Standard JavaScript:
// Create an instance of PhoneNumberUtilconst phoneUtil = PhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Get type of phone numberconsole;// 2 // FIXED_LINE_OR_MOBILE
Using ECMAScript (ES):
; // Create an instance of PhoneNumberUtilconst phoneUtil = libphonenumbersPhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Get type of phone numberconsole;// 2 // FIXED_LINE_OR_MOBILE
📦 getRegionCodeForNumber(number)
Using Standard JavaScript:
// Create an instance of PhoneNumberUtilconst phoneUtil = PhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Get region code of numberconsole;// US
Using ECMAScript (ES):
; // Create an instance of PhoneNumberUtilconst phoneUtil = libphonenumbersPhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Get region code of numberconsole;// US
📦 isPossibleNumber(number)
Using Standard JavaScript:
// Create an instance of PhoneNumberUtilconst phoneUtil = PhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Check is possible numberconsole;// true
Using ECMAScript (ES):
; // Create an instance of PhoneNumberUtilconst phoneUtil = libphonenumbersPhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Check is possible numberconsole;// true
📦 isValidNumber(number)
Using Standard JavaScript:
// Create an instance of PhoneNumberUtilconst phoneUtil = PhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Get is valid numberconsole;// true
Using ECMAScript (ES):
; // Create an instance of PhoneNumberUtilconst phoneUtil = libphonenumbersPhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Get is valid numberconsole;// true
📦 isValidNumberForRegion(number, regionCode)
Using Standard JavaScript:
// Create an instance of PhoneNumberUtilconst phoneUtil = PhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Check number of a region is validconsole;// true
Using ECMAScript (ES):
; // Create an instance of PhoneNumberUtilconst phoneUtil = libphonenumbersPhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Check number of a region is validconsole;// true
📦 parseAndKeepRawInput(numberToParse, defaultRegion)
Using Standard JavaScript:
// Create an instance of PhoneNumberUtilconst phoneUtil = PhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil;
Using ECMAScript (ES):
; // Create an instance of PhoneNumberUtilconst phoneUtil = libphonenumbersPhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil;
📦 parse(numberToParse, defaultRegion)
Using Standard JavaScript:
// Create an instance of PhoneNumberUtilconst phoneUtil = PhoneNumberUtil; // Get prototype buffer formatconsole;
Using ECMAScript (ES):
; // Create an instance of PhoneNumberUtilconst phoneUtil = libphonenumbersPhoneNumberUtil; // Get proto buffer formatconsole;
🎀 AsYouTypeFormatter
📦 inputDigit(digit)
Using Standard JavaScript:
const AsYouTypeFormatter = AsYouTypeFormatter; // Create an instance object of AsYouTypeFormatterconst formatter = 'US'; console; // => 2console; // => 20console; // => 202console; // => 202-console; // => 202-4console; // => 202-45console; // => 202-456console; // => 202-456-console; // => 202-456-2console; // => 202-456-21console; // => 202-456-212console; // => 202-456-2121 // Clear all input digits from instanceformatterclear;
Using ECMAScript (ES):
; const AsYouTypeFormatter = libphonenumbersAsYouTypeFormatter; // Create an instance object of AsYouTypeFormatterconst formatter = 'US'; console; // 2console; // 20console; // 202console; // 202-console; // 202-4console; // 202-45console; // 202-456console; // 202-456-console; // 202-456-2console; // 202-456-21console; // 202-456-212console; // 202-456-2121 // Clear all input digits from instanceformatterclear;
🎀 PhoneNumber
📦 getCountryCode()
Using Standard JavaScript:
// Create an instance of PhoneNumberUtilconst phoneUtil = PhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Get the phone's country codeconsole;// 1
Using ECMAScript (ES):
; // Create an instance of PhoneNumberUtilconst phoneUtil = libphonenumbersPhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Get the phone's country codeconsole;// 1
📦 getCountryCodeSource()
Using Standard JavaScript:
// Create an instance of PhoneNumberUtilconst phoneUtil = PhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Get the phone's extensionconsole;// FROM_DEFAULT_COUNTRY
Using ECMAScript (ES):
; // Create an instance of PhoneNumberUtilconst phoneUtil = libphonenumbersPhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Print the phone's extensionconsole;// FROM_DEFAULT_COUNTRY
📦 getExtension()
Using Standard JavaScript:
// Create an instance of PhoneNumberUtilconst phoneUtil = PhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Print the phone's extensionconsole;// null
Using ECMAScript (ES):
; // Create an instance of PhoneNumberUtilconst phoneUtil = libphonenumbersPhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Print the phone's extensionconsole;// => null
📦 getItalianLeadingZero()
Using Standard JavaScript:
// Create an instance of PhoneNumberUtilconst phoneUtil = PhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Get phone's italian leading zeroconsole;// null
Using ECMAScript (ES):
; // Create an instance of PhoneNumberUtilconst phoneUtil = libphonenumbersPhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Get phone's italian leading zeroconsole;// null
📦 getNationalNumber()
Using Standard JavaScript:
// Create an instance of PhoneNumberUtilconst phoneUtil = PhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Get the phone's national numberconsole;// 2024562121
Using ECMAScript (ES):
; // Create an instance of PhoneNumberUtilconst phoneUtil = libphonenumbersPhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Get the phone's national numberconsole;// 2024562121
📦 getRawInput()
Using Standard JavaScript:
// Create an instance of PhoneNumberUtilconst phoneUtil = PhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Get the phone's raw inputconsole;// 202-456-2121
Using ECMAScript (ES):
; // Create an instance of PhoneNumberUtilconst phoneUtil = libphonenumbersPhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Get the phone's raw inputconsole;// 202-456-2121
🎀 ShortNumberInfo
📦 connectsToEmergencyNumber(number, regionCode)
Using Standard JavaScript:
// Get an instance of ShortNumberInfoconst shortInfo = ShortNumberInfo; // Check 911 is emergency number in USconsole;// true
Using ECMAScript (ES):
; // Get an instance of ShortNumberInfoconst shortInfo = libphonenumbersShortNumberInfo; // Check 911 is emergency number in USconsole;// true
📦 isPossibleShortNumber(number)
Using Standard JavaScript:
// Get an instance of ShortNumberInfoconst shortInfo = ShortNumberInfo; // Get an instance of PhoneNumberUtilconst phoneUtil = PhoneNumberUtil; // Check 123456 is possible short number in FRconsole;// true
Using ECMAScript (ES):
; // Get an instance of ShortNumberInfoconst shortInfo = libphonenumbersShortNumberInfo; // Get an instance of PhoneNumberUtilconst phoneUtil = libphonenumbersPhoneNumberUtil; // Check 123456 is possible short number in FRconsole;// true
📦 isPossibleShortNumberForRegion(number, regionDialingFrom)
Using Standard JavaScript:
// Get an instance of ShortNumberInfoconst shortInfo = ShortNumberInfo; // Get an instance of PhoneNumberUtilconst phoneUtil = PhoneNumberUtil; // Check 123456 is possible short number for region in FRconsole;// true
Using ECMAScript (ES):
; // Get an instance of ShortNumberInfoconst shortInfo = libphonenumbersShortNumberInfo; // Get an instance of PhoneNumberUtilconst phoneUtil = libphonenumbersPhoneNumberUtil; // Check 123456 is possible short number for region in FRconsole;// true
📦 isValidShortNumber(number)
Using Standard JavaScript:
// Get an instance of ShortNumberInfoconst shortInfo = ShortNumberInfo; // Get an instance of PhoneNumberUtilconst phoneUtil = PhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Check 202-456-2121 is valid short numberconsole;// false
Using ECMAScript (ES):
; // Get an instance of ShortNumberInfoconst shortInfo = libphonenumbersShortNumberInfo; // Create an instance of PhoneNumberUtilconst phoneUtil = libphonenumbersPhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Check 202-456-2121 is valid short numberconsole;// false
📦 isValidShortNumberForRegion(number, regionDialingFrom)
Using Standard JavaScript:
// Get an instance of ShortNumberInfoconst shortInfo = ShortNumberInfo; // Get an instance of PhoneNumberUtilconst phoneUtil = PhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Check 202-456-2121 is valid short number for US regionconsole;// false
Using ECMAScript (ES):
; // Get an instance of ShortNumberInfoconst shortInfo = libphonenumbersShortNumberInfo; // Create an instance of PhoneNumberUtilconst phoneUtil = libphonenumbersPhoneNumberUtil; // Parse number with US country code and keep raw inputconst number = phoneUtil; // Check 202-456-2121 is valid short number for US regionconsole;// false
🦄 Credits and Inspiration
Inspired by Google's libphonenumber.