libphonenumbers
Advanced tools
Comparing version 1.2.0 to 1.3.0
{ | ||
"name": "libphonenumbers", | ||
"version": "1.2.0", | ||
"version": "1.3.0", | ||
"description": "JavaScript port of Google's libphonenumber library for parsing, formatting, and validating international phone numbers in Node.js.", | ||
@@ -5,0 +5,0 @@ "author": "Bunlong <bunlong.van@gmail.com>", |
284
README.md
@@ -29,22 +29,26 @@ <p align="center"> | ||
* PhoneNumberFormat ( Enums is used to pass to [format(number, numberFormat)](#-formatnumber-numberformat) ) | ||
* E164 ( value is 0 ) | ||
* INTERNATIONAL ( value is 1 ) | ||
* NATIONAL ( value is 2 ) | ||
* RFC3966 ( value is 3 ) | ||
* AsYouTypeFormatter | ||
* [inputDigit(digit)](#-inputdigitdigit) is used to formats a phone number on-the-fly as each digit is entered. | ||
* [clear()](#-inputdigitdigit) is used to clears the internal state of the formatter, so it can be reused. | ||
* PhoneNumberType ( Enums is used to compare with the output of [getNumberType(number)](#-getnumbertypenumber) ) | ||
* 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 ) | ||
* PhoneNumberFormat ( The following enums is used to pass to [format(number, numberFormat)](#-formatnumber-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)](#-getnumbertypenumber) ) | ||
* 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 | ||
@@ -58,8 +62,8 @@ * [getCountryCode()](#-getcountrycode) is used to gets the value of the country code. | ||
* CountryCodeSource ( Enums in order to compare them with the output of [getCountryCodeSource()](#-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 ) | ||
* CountryCodeSource ( The following enums is used to compare with the output of [getCountryCodeSource()](#-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) | ||
@@ -70,4 +74,4 @@ * ShortNumberInfo | ||
* [isPossibleShortNumberForRegion(number, regionDialingFrom)](#-ispossibleshortnumberforregionnumber-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. | ||
* [isValidShortNumber(number)](#-isvalidshortnumbernumber) is used to checks whether a short number is a valid number. | ||
* [isValidShortNumberForRegion(number, regionDialingFrom)](#-isvalidshortnumberforregionnumber-regiondialingfrom) is used to checks whether a short number matches a valid pattern in a region. | ||
@@ -111,15 +115,15 @@ ## ❌ Missing Features: | ||
// Parse number with US country code and keep raw input | ||
const number = phoneUtil.parseAndKeepRawInput('300-577-8989', 'US'); | ||
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US'); | ||
// Format number in the RFC3966 format | ||
console.log(phoneUtil.format(number, PNF.RFC3966)); | ||
// => tel:+1-300-577-8989 | ||
// tel:+1-202-456-2121 | ||
// Format number in the national format | ||
console.log(phoneUtil.format(number, PNF.NATIONAL)); | ||
// => (300) 577-8989 | ||
// (202) 456-2121 | ||
// Format number in the international format | ||
console.log(phoneUtil.format(number, PNF.INTERNATIONAL)); | ||
// => +1 300-577-8989 | ||
// +1 202-456-2121 | ||
``` | ||
@@ -138,15 +142,15 @@ | ||
// Parse number with US country code and keep raw input | ||
const number = phoneUtil.parseAndKeepRawInput('300-577-8989', 'US'); | ||
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US'); | ||
// Format number in the RFC3966 format | ||
console.log(phoneUtil.format(number, PNF.RFC3966)); | ||
// => tel:+1-300-577-8989 | ||
// tel:+1-202-456-2121 | ||
// Format number in the national format | ||
console.log(phoneUtil.format(number, PNF.NATIONAL)); | ||
// => (300) 577-8989 | ||
// (202) 456-2121 | ||
// Format number in the international format | ||
console.log(phoneUtil.format(number, PNF.INTERNATIONAL)); | ||
// => +1 300-577-8989 | ||
// +1 202-456-2121 | ||
``` | ||
@@ -163,7 +167,7 @@ | ||
// Parse number with US country code and keep raw input | ||
const number = phoneUtil.parseAndKeepRawInput('300-577-8989', 'US'); | ||
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US'); | ||
// Format number in the original format | ||
console.log(phoneUtil.formatInOriginalFormat(number, 'US')); | ||
// => (300) 577-8989 | ||
// => (202) 456-2121 | ||
``` | ||
@@ -180,7 +184,7 @@ | ||
// Parse number with US country code and keep raw input | ||
const number = phoneUtil.parseAndKeepRawInput('300-577-8989', 'US'); | ||
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US'); | ||
// Format number in the original format | ||
console.log(phoneUtil.formatInOriginalFormat(number, 'US')); | ||
// => (300) 577-8989 | ||
// (202) 456-2121 | ||
``` | ||
@@ -197,11 +201,11 @@ | ||
// Parse number with US country code and keep raw input | ||
const number = phoneUtil.parseAndKeepRawInput('300-577-8989', 'US'); | ||
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US'); | ||
// Format number in the out-of-country format from US | ||
console.log(phoneUtil.formatOutOfCountryCallingNumber(number, 'US')); | ||
// => 1 (300) 577-8989 | ||
// 1 (202) 456-2121 | ||
// Format number in the out-of-country format from JP | ||
console.log(phoneUtil.formatOutOfCountryCallingNumber(number, 'JP')); | ||
// => 010 1 300-577-8989 | ||
// 010 1 202-456-2121 | ||
``` | ||
@@ -218,11 +222,11 @@ | ||
// Parse number with US country code and keep raw input | ||
const number = phoneUtil.parseAndKeepRawInput('300-577-8989', 'US'); | ||
const number = phoneUtil.parseAndKeepRawInput('202-456-2121', 'US'); | ||
// Format number in the out-of-country format from US | ||
console.log(phoneUtil.formatOutOfCountryCallingNumber(number, 'US')); | ||
// => 1 (300) 577-8989 | ||
// 1 (202) 456-2121 | ||
// Format number in the out-of-country format from JP | ||
console.log(phoneUtil.formatOutOfCountryCallingNumber(number, 'JP')); | ||
// => 010 1 300-577-8989 | ||
// 010 1 202-456-2121 | ||
``` | ||
@@ -243,3 +247,3 @@ | ||
console.log(phoneUtil.getNumberType(number)); | ||
// => 2 // FIXED_LINE_OR_MOBILE | ||
// 2 // FIXED_LINE_OR_MOBILE | ||
``` | ||
@@ -260,3 +264,3 @@ | ||
console.log(phoneUtil.getNumberType(number)); | ||
// => 2 // FIXED_LINE_OR_MOBILE | ||
// 2 // FIXED_LINE_OR_MOBILE | ||
``` | ||
@@ -277,3 +281,3 @@ | ||
console.log(phoneUtil.getRegionCodeForNumber(number)); | ||
// => US | ||
// US | ||
``` | ||
@@ -294,3 +298,3 @@ | ||
console.log(phoneUtil.getRegionCodeForNumber(number)); | ||
// => US | ||
// US | ||
``` | ||
@@ -311,3 +315,3 @@ | ||
console.log(phoneUtil.isPossibleNumber(number)); | ||
// => true | ||
// true | ||
``` | ||
@@ -328,3 +332,3 @@ | ||
console.log(phoneUtil.isPossibleNumber(number)); | ||
// => true | ||
// true | ||
``` | ||
@@ -345,3 +349,3 @@ | ||
console.log(phoneUtil.isValidNumber(number)); | ||
// => true | ||
// true | ||
``` | ||
@@ -362,3 +366,3 @@ | ||
console.log(phoneUtil.isValidNumber(number)); | ||
// => true | ||
// true | ||
``` | ||
@@ -379,3 +383,3 @@ | ||
console.log(phoneUtil.isValidNumberForRegion(number, 'US')); | ||
// => true | ||
// true | ||
``` | ||
@@ -396,3 +400,3 @@ | ||
console.log(phoneUtil.isValidNumberForRegion(number, 'US')); | ||
// => true | ||
// true | ||
``` | ||
@@ -448,2 +452,58 @@ | ||
### 🎀 AsYouTypeFormatter | ||
#### 📦 inputDigit(digit) | ||
Using Standard JavaScript: | ||
```js | ||
const AsYouTypeFormatter = require('libphonenumbers').AsYouTypeFormatter; | ||
// Create an instance object of AsYouTypeFormatter | ||
const formatter = new AsYouTypeFormatter('US'); | ||
console.log(formatter.inputDigit('2')); // => 2 | ||
console.log(formatter.inputDigit('0')); // => 20 | ||
console.log(formatter.inputDigit('2')); // => 202 | ||
console.log(formatter.inputDigit('-')); // => 202- | ||
console.log(formatter.inputDigit('4')); // => 202-4 | ||
console.log(formatter.inputDigit('5')); // => 202-45 | ||
console.log(formatter.inputDigit('6')); // => 202-456 | ||
console.log(formatter.inputDigit('-')); // => 202-456- | ||
console.log(formatter.inputDigit('2')); // => 202-456-2 | ||
console.log(formatter.inputDigit('1')); // => 202-456-21 | ||
console.log(formatter.inputDigit('2')); // => 202-456-212 | ||
console.log(formatter.inputDigit('1')); // => 202-456-2121 | ||
// Clear all input digits from instance | ||
formatter.clear(); | ||
``` | ||
Using ECMAScript (ES): | ||
```js | ||
import libphonenumbers from 'libphonenumbers'; | ||
const AsYouTypeFormatter = libphonenumbers.AsYouTypeFormatter; | ||
// Create an instance object of AsYouTypeFormatter | ||
const formatter = new AsYouTypeFormatter('US'); | ||
console.log(formatter.inputDigit('2')); // 2 | ||
console.log(formatter.inputDigit('0')); // 20 | ||
console.log(formatter.inputDigit('2')); // 202 | ||
console.log(formatter.inputDigit('-')); // 202- | ||
console.log(formatter.inputDigit('4')); // 202-4 | ||
console.log(formatter.inputDigit('5')); // 202-45 | ||
console.log(formatter.inputDigit('6')); // 202-456 | ||
console.log(formatter.inputDigit('-')); // 202-456- | ||
console.log(formatter.inputDigit('2')); // 202-456-2 | ||
console.log(formatter.inputDigit('1')); // 202-456-21 | ||
console.log(formatter.inputDigit('2')); // 202-456-212 | ||
console.log(formatter.inputDigit('1')); // 202-456-2121 | ||
// Clear all input digits from instance | ||
formatter.clear(); | ||
``` | ||
### 🎀 PhoneNumber | ||
@@ -464,3 +524,3 @@ | ||
console.log(number.getCountryCode()); | ||
// => 1 | ||
// 1 | ||
``` | ||
@@ -481,3 +541,3 @@ | ||
console.log(number.getCountryCode()); | ||
// => 1 | ||
// 1 | ||
``` | ||
@@ -498,3 +558,3 @@ | ||
console.log(number.getCountryCodeSource()); | ||
// => FROM_DEFAULT_COUNTRY | ||
// FROM_DEFAULT_COUNTRY | ||
``` | ||
@@ -515,3 +575,3 @@ | ||
console.log(number.getCountryCodeSource()); | ||
// => FROM_DEFAULT_COUNTRY | ||
// FROM_DEFAULT_COUNTRY | ||
``` | ||
@@ -530,5 +590,5 @@ | ||
// Print the phone's extension. | ||
// Print the phone's extension | ||
console.log(number.getExtension()); | ||
// => null | ||
// null | ||
``` | ||
@@ -547,3 +607,3 @@ | ||
// Print the phone's extension. | ||
// Print the phone's extension | ||
console.log(number.getExtension()); | ||
@@ -566,3 +626,3 @@ // => null | ||
console.log(number.getItalianLeadingZero()); | ||
// => null | ||
// null | ||
``` | ||
@@ -583,3 +643,3 @@ | ||
console.log(number.getItalianLeadingZero()); | ||
// => null | ||
// null | ||
``` | ||
@@ -600,3 +660,3 @@ | ||
console.log(number.getNationalNumber()); | ||
// => 2024562121 | ||
// 2024562121 | ||
``` | ||
@@ -617,3 +677,3 @@ | ||
console.log(number.getNationalNumber()); | ||
// => 2024562121 | ||
// 2024562121 | ||
``` | ||
@@ -634,3 +694,3 @@ | ||
console.log(number.getRawInput()); | ||
// => 202-456-2121 | ||
// 202-456-2121 | ||
``` | ||
@@ -651,3 +711,3 @@ | ||
console.log(number.getRawInput()); | ||
// => 202-456-2121 | ||
// 202-456-2121 | ||
``` | ||
@@ -667,3 +727,3 @@ | ||
console.log(shortInfo.connectsToEmergencyNumber('911', 'US')); | ||
// => true | ||
// true | ||
``` | ||
@@ -681,3 +741,3 @@ | ||
console.log(shortInfo.connectsToEmergencyNumber('911', 'US')); | ||
// => true | ||
// true | ||
``` | ||
@@ -698,3 +758,3 @@ | ||
console.log(shortInfo.isPossibleShortNumber(phoneUtil.parse('123456', 'FR'))); | ||
// => true | ||
// true | ||
``` | ||
@@ -715,3 +775,3 @@ | ||
console.log(shortInfo.isPossibleShortNumber(phoneUtil.parse('123456', 'FR'))); | ||
// => true | ||
// true | ||
``` | ||
@@ -732,3 +792,3 @@ | ||
console.log(shortInfo.isPossibleShortNumberForRegion(phoneUtil.parse('123456', 'FR'), 'FR')); | ||
// => true | ||
// true | ||
``` | ||
@@ -749,7 +809,83 @@ | ||
console.log(shortInfo.isPossibleShortNumberForRegion(phoneUtil.parse('123456', 'FR'), 'FR')); | ||
// => true | ||
// true | ||
``` | ||
## 🦄 Credit and Inspiration | ||
#### 📦 isValidShortNumber(number) | ||
Using Standard JavaScript: | ||
```js | ||
// Get an instance of ShortNumberInfo | ||
const shortInfo = require('libphonenumbers').ShortNumberInfo.getInstance(); | ||
// Get 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 202-456-2121 is valid short number | ||
console.log(shortInfo.isValidShortNumber(number)); | ||
// false | ||
``` | ||
Using ECMAScript (ES): | ||
```js | ||
import libphonenumbers from 'libphonenumbers'; | ||
// Get an instance of ShortNumberInfo | ||
const shortInfo = libphonenumbers.ShortNumberInfo.getInstance(); | ||
// 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 202-456-2121 is valid short number | ||
console.log(shortInfo.isValidShortNumber(number)); | ||
// false | ||
``` | ||
#### 📦 isValidShortNumberForRegion(number, regionDialingFrom) | ||
Using Standard JavaScript: | ||
```js | ||
// Get an instance of ShortNumberInfo | ||
const shortInfo = require('libphonenumbers').ShortNumberInfo.getInstance(); | ||
// Get 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 202-456-2121 is valid short number for US region | ||
console.log(shortInfo.isValidShortNumberForRegion(number, 'US')); | ||
// false | ||
``` | ||
Using ECMAScript (ES): | ||
```js | ||
import libphonenumbers from 'libphonenumbers'; | ||
// Get an instance of ShortNumberInfo | ||
const shortInfo = libphonenumbers.ShortNumberInfo.getInstance(); | ||
// 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 202-456-2121 is valid short number for US region | ||
console.log(shortInfo.isValidShortNumberForRegion(number, 'US')); | ||
// false | ||
``` | ||
## 🦄 Credits and Inspiration | ||
Inspired by [Google's libphonenumber](https://github.com/google/libphonenumber). | ||
@@ -756,0 +892,0 @@ |
1160368
858