libphonenumber-js 1.13.3 → 1.13.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitlab-ci.yml +24 -9
- package/CHANGELOG.md +20 -15
- package/README.md +27 -10
- package/build/AsYouType.js +100 -27
- package/build/AsYouType.js.map +1 -1
- package/build/AsYouType.test.js.map +1 -1
- package/build/AsYouTypeFormatter.complete.js +2 -2
- package/build/AsYouTypeFormatter.complete.js.map +1 -1
- package/build/AsYouTypeFormatter.js +1 -1
- package/build/AsYouTypeFormatter.js.map +1 -1
- package/build/AsYouTypeState.js +15 -1
- package/build/AsYouTypeState.js.map +1 -1
- package/build/PhoneNumber.js +26 -39
- package/build/PhoneNumber.js.map +1 -1
- package/build/format.js +3 -3
- package/build/format.js.map +1 -1
- package/build/formatPhoneNumberForMobileDialing.js +4 -3
- package/build/formatPhoneNumberForMobileDialing.js.map +1 -1
- package/build/helpers/checkNumberLength.js +21 -6
- package/build/helpers/checkNumberLength.js.map +1 -1
- package/build/helpers/checkNumberLength.test.js.map +1 -1
- package/build/helpers/extractCountryCallingCode.js +16 -11
- package/build/helpers/extractCountryCallingCode.js.map +1 -1
- package/build/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +21 -10
- package/build/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js.map +1 -1
- package/build/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.test.js.map +1 -0
- package/build/helpers/extractNationalNumber.js +12 -3
- package/build/helpers/extractNationalNumber.js.map +1 -1
- package/build/helpers/extractNationalNumber.test.js.map +1 -1
- package/build/helpers/getCountryAndCallingCodeFromOneOfThem.js +35 -0
- package/build/helpers/getCountryAndCallingCodeFromOneOfThem.js.map +1 -0
- package/build/helpers/getCountryByCallingCode.js +10 -7
- package/build/helpers/getCountryByCallingCode.js.map +1 -1
- package/build/helpers/getCountryByNationalNumber.js +13 -9
- package/build/helpers/getCountryByNationalNumber.js.map +1 -1
- package/build/helpers/getIddPrefix.js +1 -1
- package/build/helpers/getIddPrefix.js.map +1 -1
- package/build/helpers/getNumberType.js +3 -3
- package/build/helpers/getNumberType.js.map +1 -1
- package/build/helpers/getPossibleCountriesForNumber.js +4 -7
- package/build/helpers/getPossibleCountriesForNumber.js.map +1 -1
- package/build/helpers/isCallingCode.js +11 -0
- package/build/helpers/isCallingCode.js.map +1 -0
- package/build/helpers/isCallingCode.test.js.map +1 -0
- package/build/helpers/isCountryCode.js +11 -0
- package/build/helpers/isCountryCode.js.map +1 -0
- package/build/helpers/isCountryCode.test.js.map +1 -0
- package/build/helpers/stripIddPrefix.js +9 -4
- package/build/helpers/stripIddPrefix.js.map +1 -1
- package/build/isPossible.js +14 -8
- package/build/isPossible.js.map +1 -1
- package/build/isValid.js +3 -3
- package/build/isValid.js.map +1 -1
- package/build/metadata.js +80 -31
- package/build/metadata.js.map +1 -1
- package/build/metadata.test.js.map +1 -1
- package/build/parse.js +36 -12
- package/build/parse.js.map +1 -1
- package/build/validatePhoneNumberLength.js +3 -3
- package/build/validatePhoneNumberLength.js.map +1 -1
- package/bundle/libphonenumber-js.min.js +1 -1
- package/bundle/libphonenumber-js.min.js.map +1 -1
- package/bundle/libphonenumber-max.js +1 -1
- package/bundle/libphonenumber-max.js.map +1 -1
- package/bundle/libphonenumber-min.js +1 -1
- package/bundle/libphonenumber-min.js.map +1 -1
- package/bundle/libphonenumber-mobile.js +1 -1
- package/bundle/libphonenumber-mobile.js.map +1 -1
- package/core/index.d.cts +1 -0
- package/core/index.d.ts +1 -0
- package/custom.d.ts +3 -1
- package/es6/AsYouType.js +100 -27
- package/es6/AsYouType.js.map +1 -1
- package/es6/AsYouType.test.js.map +1 -1
- package/es6/AsYouTypeFormatter.complete.js +2 -2
- package/es6/AsYouTypeFormatter.complete.js.map +1 -1
- package/es6/AsYouTypeFormatter.js +1 -1
- package/es6/AsYouTypeFormatter.js.map +1 -1
- package/es6/AsYouTypeState.js +15 -1
- package/es6/AsYouTypeState.js.map +1 -1
- package/es6/PhoneNumber.js +26 -39
- package/es6/PhoneNumber.js.map +1 -1
- package/es6/format.js +3 -3
- package/es6/format.js.map +1 -1
- package/es6/formatPhoneNumberForMobileDialing.js +4 -3
- package/es6/formatPhoneNumberForMobileDialing.js.map +1 -1
- package/es6/helpers/checkNumberLength.js +22 -6
- package/es6/helpers/checkNumberLength.js.map +1 -1
- package/es6/helpers/checkNumberLength.test.js.map +1 -1
- package/es6/helpers/extractCountryCallingCode.js +16 -11
- package/es6/helpers/extractCountryCallingCode.js.map +1 -1
- package/es6/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +21 -10
- package/es6/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js.map +1 -1
- package/es6/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.test.js.map +1 -0
- package/es6/helpers/extractNationalNumber.js +12 -3
- package/es6/helpers/extractNationalNumber.js.map +1 -1
- package/es6/helpers/extractNationalNumber.test.js.map +1 -1
- package/es6/helpers/getCountryAndCallingCodeFromOneOfThem.js +28 -0
- package/es6/helpers/getCountryAndCallingCodeFromOneOfThem.js.map +1 -0
- package/es6/helpers/getCountryByCallingCode.js +10 -7
- package/es6/helpers/getCountryByCallingCode.js.map +1 -1
- package/es6/helpers/getCountryByNationalNumber.js +13 -9
- package/es6/helpers/getCountryByNationalNumber.js.map +1 -1
- package/es6/helpers/getIddPrefix.js +1 -1
- package/es6/helpers/getIddPrefix.js.map +1 -1
- package/es6/helpers/getNumberType.js +3 -3
- package/es6/helpers/getNumberType.js.map +1 -1
- package/es6/helpers/getPossibleCountriesForNumber.js +4 -7
- package/es6/helpers/getPossibleCountriesForNumber.js.map +1 -1
- package/es6/helpers/isCallingCode.js +5 -0
- package/es6/helpers/isCallingCode.js.map +1 -0
- package/es6/helpers/isCallingCode.test.js.map +1 -0
- package/es6/helpers/isCountryCode.js +5 -0
- package/es6/helpers/isCountryCode.js.map +1 -0
- package/es6/helpers/isCountryCode.test.js.map +1 -0
- package/es6/helpers/stripIddPrefix.js +9 -4
- package/es6/helpers/stripIddPrefix.js.map +1 -1
- package/es6/isPossible.js +14 -8
- package/es6/isPossible.js.map +1 -1
- package/es6/isValid.js +3 -3
- package/es6/isValid.js.map +1 -1
- package/es6/metadata.js +80 -31
- package/es6/metadata.js.map +1 -1
- package/es6/metadata.test.js.map +1 -1
- package/es6/parse.js +36 -12
- package/es6/parse.js.map +1 -1
- package/es6/validatePhoneNumberLength.js +3 -3
- package/es6/validatePhoneNumberLength.js.map +1 -1
- package/es6-modern/AsYouType.js +100 -26
- package/es6-modern/AsYouType.js.map +1 -1
- package/es6-modern/AsYouType.test.js.map +1 -1
- package/es6-modern/AsYouTypeFormatter.complete.js +2 -2
- package/es6-modern/AsYouTypeFormatter.complete.js.map +1 -1
- package/es6-modern/AsYouTypeFormatter.js +1 -1
- package/es6-modern/AsYouTypeFormatter.js.map +1 -1
- package/es6-modern/AsYouTypeState.js +15 -1
- package/es6-modern/AsYouTypeState.js.map +1 -1
- package/es6-modern/PhoneNumber.js +25 -36
- package/es6-modern/PhoneNumber.js.map +1 -1
- package/es6-modern/format.js +3 -3
- package/es6-modern/format.js.map +1 -1
- package/es6-modern/formatPhoneNumberForMobileDialing.js +4 -3
- package/es6-modern/formatPhoneNumberForMobileDialing.js.map +1 -1
- package/es6-modern/helpers/checkNumberLength.js +22 -6
- package/es6-modern/helpers/checkNumberLength.js.map +1 -1
- package/es6-modern/helpers/checkNumberLength.test.js.map +1 -1
- package/es6-modern/helpers/extractCountryCallingCode.js +16 -11
- package/es6-modern/helpers/extractCountryCallingCode.js.map +1 -1
- package/es6-modern/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +21 -10
- package/es6-modern/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js.map +1 -1
- package/es6-modern/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.test.js.map +1 -0
- package/es6-modern/helpers/extractNationalNumber.js +12 -3
- package/es6-modern/helpers/extractNationalNumber.js.map +1 -1
- package/es6-modern/helpers/extractNationalNumber.test.js.map +1 -1
- package/es6-modern/helpers/getCountryAndCallingCodeFromOneOfThem.js +28 -0
- package/es6-modern/helpers/getCountryAndCallingCodeFromOneOfThem.js.map +1 -0
- package/es6-modern/helpers/getCountryByCallingCode.js +10 -7
- package/es6-modern/helpers/getCountryByCallingCode.js.map +1 -1
- package/es6-modern/helpers/getCountryByNationalNumber.js +13 -10
- package/es6-modern/helpers/getCountryByNationalNumber.js.map +1 -1
- package/es6-modern/helpers/getIddPrefix.js +1 -1
- package/es6-modern/helpers/getIddPrefix.js.map +1 -1
- package/es6-modern/helpers/getNumberType.js +3 -3
- package/es6-modern/helpers/getNumberType.js.map +1 -1
- package/es6-modern/helpers/getPossibleCountriesForNumber.js +4 -7
- package/es6-modern/helpers/getPossibleCountriesForNumber.js.map +1 -1
- package/es6-modern/helpers/isCallingCode.js +5 -0
- package/es6-modern/helpers/isCallingCode.js.map +1 -0
- package/es6-modern/helpers/isCallingCode.test.js.map +1 -0
- package/es6-modern/helpers/isCountryCode.js +5 -0
- package/es6-modern/helpers/isCountryCode.js.map +1 -0
- package/es6-modern/helpers/isCountryCode.test.js.map +1 -0
- package/es6-modern/helpers/stripIddPrefix.js +9 -4
- package/es6-modern/helpers/stripIddPrefix.js.map +1 -1
- package/es6-modern/isPossible.js +14 -8
- package/es6-modern/isPossible.js.map +1 -1
- package/es6-modern/isValid.js +3 -3
- package/es6-modern/isValid.js.map +1 -1
- package/es6-modern/metadata.js +78 -31
- package/es6-modern/metadata.js.map +1 -1
- package/es6-modern/metadata.test.js.map +1 -1
- package/es6-modern/parse.js +41 -12
- package/es6-modern/parse.js.map +1 -1
- package/es6-modern/validatePhoneNumberLength.js +3 -3
- package/es6-modern/validatePhoneNumberLength.js.map +1 -1
- package/index.d.cts +1 -0
- package/index.d.ts +1 -0
- package/max/index.d.cts +1 -0
- package/max/index.d.ts +1 -0
- package/min/index.d.cts +1 -0
- package/min/index.d.ts +1 -0
- package/mobile/index.d.cts +1 -0
- package/mobile/index.d.ts +1 -0
- package/package.json +1 -1
- package/source/AsYouType.js +108 -27
- package/source/AsYouTypeFormatter.complete.js +2 -2
- package/source/AsYouTypeFormatter.js +1 -1
- package/source/AsYouTypeState.js +15 -1
- package/source/PhoneNumber.js +24 -39
- package/source/format.js +3 -3
- package/source/formatPhoneNumberForMobileDialing.js +4 -3
- package/source/helpers/checkNumberLength.js +21 -6
- package/source/helpers/extractCountryCallingCode.js +16 -11
- package/source/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +20 -10
- package/source/helpers/extractNationalNumber.js +11 -3
- package/source/helpers/getCountryAndCallingCodeFromOneOfThem.js +31 -0
- package/source/helpers/getCountryByCallingCode.js +10 -7
- package/source/helpers/getCountryByNationalNumber.js +14 -11
- package/source/helpers/getIddPrefix.js +1 -1
- package/source/helpers/getNumberType.js +3 -3
- package/source/helpers/getPossibleCountriesForNumber.js +4 -7
- package/source/helpers/isCallingCode.js +5 -0
- package/source/helpers/isCountryCode.js +5 -0
- package/source/helpers/stripIddPrefix.js +10 -4
- package/source/isPossible.js +13 -7
- package/source/isValid.js +3 -3
- package/source/metadata.js +77 -33
- package/source/parse.js +41 -11
- package/source/validatePhoneNumberLength.js +3 -3
- package/types.d.cts +5 -34
- package/types.d.ts +5 -34
package/source/metadata.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import compare from './tools/semver-compare.js'
|
|
2
2
|
import isObject from './helpers/isObject.js'
|
|
3
|
+
import isCallingCode from './helpers/isCallingCode.js'
|
|
3
4
|
|
|
4
5
|
// Added "possibleLengths" and renamed
|
|
5
6
|
// "country_phone_code_to_countries" to "country_calling_codes".
|
|
@@ -13,12 +14,13 @@ const V4 = '1.7.35'
|
|
|
13
14
|
|
|
14
15
|
const DEFAULT_EXT_PREFIX = ' ext. '
|
|
15
16
|
|
|
16
|
-
const CALLING_CODE_REG_EXP = /^\d+$/
|
|
17
|
-
|
|
18
17
|
/**
|
|
19
18
|
* See: https://gitlab.com/catamphetamine/libphonenumber-js/blob/master/METADATA.md
|
|
20
19
|
*/
|
|
21
20
|
export default class Metadata {
|
|
21
|
+
/**
|
|
22
|
+
* @param {object} metadata — Metadata JSON.
|
|
23
|
+
*/
|
|
22
24
|
constructor(metadata) {
|
|
23
25
|
validateMetadata(metadata)
|
|
24
26
|
this.metadata = metadata
|
|
@@ -75,17 +77,35 @@ export default class Metadata {
|
|
|
75
77
|
return this.selectNumberingPlan(countryCode)
|
|
76
78
|
}
|
|
77
79
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
80
|
+
/**
|
|
81
|
+
* Selects a telephone numbering plan based on either a country code or a calling code.
|
|
82
|
+
* @param {string} argument — Country code or calling code.
|
|
83
|
+
* @param {string} legacyArgumentCallingCode — Legacy argument: calling code, if the first argument is not passed.
|
|
84
|
+
*/
|
|
85
|
+
selectNumberingPlan(argument, legacyArgumentCallingCode) {
|
|
86
|
+
let countryCode
|
|
87
|
+
let callingCode
|
|
88
|
+
if (argument) {
|
|
89
|
+
if (isCallingCode(argument)) {
|
|
90
|
+
callingCode = argument
|
|
91
|
+
} else {
|
|
92
|
+
countryCode = argument
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
// Previously, the signature of this method was: `.selectNumberingPlan(country, callingCode)`.
|
|
96
|
+
// And if `country` argument wasn't passed, it looked into the second argument `callingCode`.
|
|
97
|
+
// Now it's preferred to only pass either `country` or `callingCode` as the first argument.
|
|
98
|
+
if (legacyArgumentCallingCode) {
|
|
99
|
+
callingCode = legacyArgumentCallingCode
|
|
83
100
|
}
|
|
101
|
+
|
|
102
|
+
// Select a telephone numbering plan.
|
|
84
103
|
if (countryCode && countryCode !== '001') {
|
|
85
|
-
|
|
104
|
+
const countryMetadata = this.getCountryMetadata(countryCode)
|
|
105
|
+
if (!countryMetadata) {
|
|
86
106
|
throw new Error(`Unknown country: ${countryCode}`)
|
|
87
107
|
}
|
|
88
|
-
this.numberingPlan = new NumberingPlan(
|
|
108
|
+
this.numberingPlan = new NumberingPlan(countryMetadata, this)
|
|
89
109
|
} else if (callingCode) {
|
|
90
110
|
if (!this.hasCallingCode(callingCode)) {
|
|
91
111
|
throw new Error(`Unknown calling code: ${callingCode}`)
|
|
@@ -94,6 +114,7 @@ export default class Metadata {
|
|
|
94
114
|
} else {
|
|
95
115
|
this.numberingPlan = undefined
|
|
96
116
|
}
|
|
117
|
+
|
|
97
118
|
return this
|
|
98
119
|
}
|
|
99
120
|
|
|
@@ -124,6 +145,10 @@ export default class Metadata {
|
|
|
124
145
|
}
|
|
125
146
|
|
|
126
147
|
getNumberingPlanMetadata(callingCode) {
|
|
148
|
+
// If a calling code is specified, choose the telephone numbering plan
|
|
149
|
+
// of the "default" country for this calling code.
|
|
150
|
+
// For example, if calling code `1` is passed,
|
|
151
|
+
// the telephone numbering plan of `US` will be selected.
|
|
127
152
|
const countryCode = this.getCountryCodeForCallingCode(callingCode)
|
|
128
153
|
if (countryCode) {
|
|
129
154
|
return this.getCountryMetadata(countryCode)
|
|
@@ -232,16 +257,27 @@ class NumberingPlan {
|
|
|
232
257
|
return this.metadata[0]
|
|
233
258
|
}
|
|
234
259
|
|
|
235
|
-
//
|
|
236
|
-
//
|
|
237
|
-
//
|
|
238
|
-
//
|
|
239
|
-
//
|
|
240
|
-
//
|
|
241
|
-
|
|
260
|
+
// When multiple countries share the same calling code,
|
|
261
|
+
// all phone number formatting rules are included in the metadata
|
|
262
|
+
// of the "default" country for that calling code.
|
|
263
|
+
// Any other countries' metdata doesn't include any formatting rules.
|
|
264
|
+
// Google developers said that such storage architecture was chosen for performance reasons.
|
|
265
|
+
//
|
|
266
|
+
// For example, for NANPA region ("North American Numbering Plan Administration",
|
|
267
|
+
// which includes USA, Canada, Cayman Islands, Bahamas, etc) all formatting rules
|
|
268
|
+
// are contained in the metadata of `US` country.
|
|
269
|
+
//
|
|
270
|
+
// This is not public API.
|
|
271
|
+
//
|
|
272
|
+
_getDefaultCountryMetadataForThisCallingCode() {
|
|
242
273
|
return this.globalMetadataObject.getNumberingPlanMetadata(this.callingCode())
|
|
243
274
|
}
|
|
244
275
|
|
|
276
|
+
// Deprecated.
|
|
277
|
+
getDefaultCountryMetadataForRegion() {
|
|
278
|
+
return this._getDefaultCountryMetadataForThisCallingCode()
|
|
279
|
+
}
|
|
280
|
+
|
|
245
281
|
// Is always present.
|
|
246
282
|
IDDPrefix() {
|
|
247
283
|
if (this.v1 || this.v2) return
|
|
@@ -273,7 +309,7 @@ class NumberingPlan {
|
|
|
273
309
|
// formats are all stored in the "main" country for that region.
|
|
274
310
|
// E.g. "RU" and "KZ", "US" and "CA".
|
|
275
311
|
formats() {
|
|
276
|
-
const formats = this._getFormats(this.metadata) || this._getFormats(this.
|
|
312
|
+
const formats = this._getFormats(this.metadata) || this._getFormats(this._getDefaultCountryMetadataForThisCallingCode()) || []
|
|
277
313
|
return formats.map(_ => new Format(_, this))
|
|
278
314
|
}
|
|
279
315
|
|
|
@@ -289,7 +325,7 @@ class NumberingPlan {
|
|
|
289
325
|
// national prefix formatting rule is stored in the "main" country for that region.
|
|
290
326
|
// E.g. "RU" and "KZ", "US" and "CA".
|
|
291
327
|
nationalPrefixFormattingRule() {
|
|
292
|
-
return this._getNationalPrefixFormattingRule(this.metadata) || this._getNationalPrefixFormattingRule(this.
|
|
328
|
+
return this._getNationalPrefixFormattingRule(this.metadata) || this._getNationalPrefixFormattingRule(this._getDefaultCountryMetadataForThisCallingCode())
|
|
293
329
|
}
|
|
294
330
|
|
|
295
331
|
_nationalPrefixForParsing() {
|
|
@@ -316,7 +352,7 @@ class NumberingPlan {
|
|
|
316
352
|
// E.g. "RU" and "KZ", "US" and "CA".
|
|
317
353
|
nationalPrefixIsOptionalWhenFormattingInNationalFormat() {
|
|
318
354
|
return this._getNationalPrefixIsOptionalWhenFormatting(this.metadata) ||
|
|
319
|
-
this._getNationalPrefixIsOptionalWhenFormatting(this.
|
|
355
|
+
this._getNationalPrefixIsOptionalWhenFormatting(this._getDefaultCountryMetadataForThisCallingCode())
|
|
320
356
|
}
|
|
321
357
|
|
|
322
358
|
leadingDigits() {
|
|
@@ -479,8 +515,8 @@ const typeOf = _ => typeof _
|
|
|
479
515
|
* // Returns " ext. "
|
|
480
516
|
* getExtPrefix("US")
|
|
481
517
|
*/
|
|
482
|
-
export function getExtPrefix(country,
|
|
483
|
-
metadata = new Metadata(
|
|
518
|
+
export function getExtPrefix(country, metadataJson) {
|
|
519
|
+
const metadata = new Metadata(metadataJson)
|
|
484
520
|
if (metadata.hasCountry(country)) {
|
|
485
521
|
return metadata.selectNumberingPlan(country).ext()
|
|
486
522
|
}
|
|
@@ -491,26 +527,39 @@ export function getExtPrefix(country, metadata) {
|
|
|
491
527
|
* Returns "country calling code" for a country.
|
|
492
528
|
* Throws an error if the country doesn't exist or isn't supported by this library.
|
|
493
529
|
* @param {string} country
|
|
494
|
-
* @param {object}
|
|
530
|
+
* @param {object} metadataJson
|
|
495
531
|
* @return {string}
|
|
496
532
|
* @example
|
|
497
533
|
* // Returns "44"
|
|
498
534
|
* getCountryCallingCode("GB")
|
|
499
535
|
*/
|
|
500
|
-
export function getCountryCallingCode(country,
|
|
501
|
-
metadata = new Metadata(
|
|
536
|
+
export function getCountryCallingCode(country, metadataJson) {
|
|
537
|
+
const metadata = new Metadata(metadataJson)
|
|
502
538
|
if (metadata.hasCountry(country)) {
|
|
503
539
|
return metadata.selectNumberingPlan(country).countryCallingCode()
|
|
504
540
|
}
|
|
505
541
|
throw new Error(`Unknown country: ${country}`)
|
|
506
542
|
}
|
|
507
543
|
|
|
508
|
-
|
|
509
|
-
|
|
544
|
+
/**
|
|
545
|
+
* Tells if a country code is supported.
|
|
546
|
+
* @param {string} country
|
|
547
|
+
* @param {object} metadataJson
|
|
548
|
+
* @return {boolean}
|
|
549
|
+
* @example
|
|
550
|
+
* // Returns `true`
|
|
551
|
+
* isSupportedCountry("GB")
|
|
552
|
+
*/
|
|
553
|
+
export function isSupportedCountry(country, metadataJson) {
|
|
554
|
+
// const metadata = new Metadata(metadataJson)
|
|
510
555
|
// return metadata.hasCountry(country)
|
|
511
|
-
return
|
|
556
|
+
return metadataJson.countries.hasOwnProperty(country)
|
|
512
557
|
}
|
|
513
558
|
|
|
559
|
+
/**
|
|
560
|
+
* Sets `v1`/`v2`/`v3`/`v4` properties on `this` object.
|
|
561
|
+
* @param {object} metadata — Metadata JSON
|
|
562
|
+
*/
|
|
514
563
|
function setVersion(metadata) {
|
|
515
564
|
const { version } = metadata
|
|
516
565
|
if (typeof version === 'number') {
|
|
@@ -529,9 +578,4 @@ function setVersion(metadata) {
|
|
|
529
578
|
this.v4 = true
|
|
530
579
|
}
|
|
531
580
|
}
|
|
532
|
-
}
|
|
533
|
-
|
|
534
|
-
// const ISO_COUNTRY_CODE = /^[A-Z]{2}$/
|
|
535
|
-
// function isCountryCode(countryCode) {
|
|
536
|
-
// return ISO_COUNTRY_CODE.test(countryCodeOrCountryCallingCode)
|
|
537
|
-
// }
|
|
581
|
+
}
|
package/source/parse.js
CHANGED
|
@@ -61,15 +61,15 @@ const USE_NON_GEOGRAPHIC_COUNTRY_CODE = false
|
|
|
61
61
|
*
|
|
62
62
|
* @param {string} input
|
|
63
63
|
* @param {object} [options]
|
|
64
|
-
* @param {object}
|
|
64
|
+
* @param {object} metadataJson
|
|
65
65
|
* @return {object|PhoneNumber?} If `options.v2: true` flag is passed, it returns a `PhoneNumber?` instance. Otherwise, returns an object of shape `{ phone: '...', country: '...' }` (or just `{}` if no phone number was parsed).
|
|
66
66
|
*/
|
|
67
|
-
export default function parse(text, options,
|
|
67
|
+
export default function parse(text, options, metadataJson) {
|
|
68
68
|
// If assigning the `{}` default value is moved to the arguments above,
|
|
69
69
|
// code coverage would decrease for some weird reason.
|
|
70
70
|
options = options || {}
|
|
71
71
|
|
|
72
|
-
metadata = new Metadata(
|
|
72
|
+
const metadata = new Metadata(metadataJson)
|
|
73
73
|
|
|
74
74
|
// Validate `defaultCountry`.
|
|
75
75
|
if (options.defaultCountry && !metadata.hasCountry(options.defaultCountry)) {
|
|
@@ -103,6 +103,9 @@ export default function parse(text, options, metadata) {
|
|
|
103
103
|
formattedPhoneNumber,
|
|
104
104
|
options.defaultCountry,
|
|
105
105
|
options.defaultCallingCode,
|
|
106
|
+
// If `country` is returned, its numbering plan will also be selected in `metadata`.
|
|
107
|
+
// Otherwise, if `countryCallingCode` is returned, its numbering plan will also be selected in `metadata`.
|
|
108
|
+
// Otherwise, if neither `country` nor `countryCallingCode` are returned, no numbering plan will be selected in `metadata`.
|
|
106
109
|
metadata
|
|
107
110
|
)
|
|
108
111
|
|
|
@@ -181,7 +184,7 @@ export default function parse(text, options, metadata) {
|
|
|
181
184
|
possible: valid ? true : (
|
|
182
185
|
options.extended === true &&
|
|
183
186
|
metadata.possibleLengths() &&
|
|
184
|
-
isPossibleNumber(nationalNumber,
|
|
187
|
+
isPossibleNumber(nationalNumber, metadata) ? true : false
|
|
185
188
|
),
|
|
186
189
|
phone: nationalNumber,
|
|
187
190
|
ext
|
|
@@ -278,7 +281,7 @@ function result(country, nationalNumber, ext) {
|
|
|
278
281
|
* @param {string} formattedPhoneNumber — Example: "(213) 373-4253".
|
|
279
282
|
* @param {string} [defaultCountry]
|
|
280
283
|
* @param {string} [defaultCallingCode]
|
|
281
|
-
* @param {Metadata} metadata
|
|
284
|
+
* @param {Metadata} metadata — Metadata instance with no pre-selected numbering plan. If `country` is returned, its numbering plan will also be selected in `metadata`. Otherwise, if `countryCallingCode` is returned, its numbering plan will also be selected in `metadata`. Otherwise, if neither `country` nor `countryCallingCode` are returned, no numbering plan will be selected in `metadata`.
|
|
282
285
|
* @return {object} Returns `{ country: string?, countryCallingCode: string?, nationalNumber: string? }`.
|
|
283
286
|
*/
|
|
284
287
|
function parsePhoneNumber(
|
|
@@ -287,10 +290,30 @@ function parsePhoneNumber(
|
|
|
287
290
|
defaultCallingCode,
|
|
288
291
|
metadata
|
|
289
292
|
) {
|
|
290
|
-
//
|
|
291
|
-
|
|
293
|
+
// Attempt to extract a calling code from a phone number.
|
|
294
|
+
// * If the phone number is found to be "international":
|
|
295
|
+
// * It will attempt to identify the calling code part of it and whether that part is complete and valid.
|
|
296
|
+
// * If the calling code part is complete and valid, it will return two properties:
|
|
297
|
+
// that calling code part (without `+`) and the rest of the digits.
|
|
298
|
+
// * Otherwise, i.e. if the calling code part is incomplete or invalid,
|
|
299
|
+
// it will return an empty object.
|
|
300
|
+
// * Otherwise, i.e. if the phone number is national, there's no callind code to extract
|
|
301
|
+
// so it will just return the originally-passed `number` string as the only property.
|
|
302
|
+
let {
|
|
303
|
+
// `countryCallingCodeSource` tells how the returned calling code was extracted (if it was extracted).
|
|
304
|
+
countryCallingCodeSource,
|
|
305
|
+
// `countryCallingCode` is the calling code that was extracted from the input phone number string.
|
|
306
|
+
countryCallingCode,
|
|
307
|
+
// `number` is the originally passed `number` without the extracted calling code (and without a `+`).
|
|
308
|
+
// If the calling code is present but incomplete or invalid, both `countryCallingCode` and `number`
|
|
309
|
+
// will be returned as `undefined`.
|
|
310
|
+
number
|
|
311
|
+
} = extractCountryCallingCode(
|
|
292
312
|
parseIncompletePhoneNumber(formattedPhoneNumber),
|
|
293
313
|
undefined,
|
|
314
|
+
// `defaultCountry` and `defaultCallingCode` are only used to detect
|
|
315
|
+
// if it's an "international" phone number or not. They won't be used
|
|
316
|
+
// to derive the resulting `countryCallingCode` from them, or anything like that.
|
|
294
317
|
defaultCountry,
|
|
295
318
|
defaultCallingCode,
|
|
296
319
|
metadata.metadata
|
|
@@ -307,21 +330,28 @@ function parsePhoneNumber(
|
|
|
307
330
|
// Else, if `formattedPhoneNumber` is passed in "national" format,
|
|
308
331
|
// then `number` is defined and `countryCallingCode` is `undefined`.
|
|
309
332
|
else if (number && (defaultCountry || defaultCallingCode)) {
|
|
310
|
-
metadata.selectNumberingPlan(defaultCountry, defaultCallingCode)
|
|
311
333
|
if (defaultCountry) {
|
|
312
334
|
country = defaultCountry
|
|
335
|
+
metadata.selectNumberingPlan(defaultCountry)
|
|
336
|
+
countryCallingCode = metadata.numberingPlan.callingCode()
|
|
313
337
|
} else {
|
|
338
|
+
metadata.selectNumberingPlan(defaultCallingCode)
|
|
339
|
+
countryCallingCode = defaultCallingCode
|
|
314
340
|
/* istanbul ignore if */
|
|
315
341
|
if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
|
|
316
|
-
if (metadata.isNonGeographicCallingCode(
|
|
342
|
+
if (metadata.isNonGeographicCallingCode(countryCallingCode)) {
|
|
317
343
|
country = '001'
|
|
318
344
|
}
|
|
319
345
|
}
|
|
320
346
|
}
|
|
321
|
-
countryCallingCode = defaultCallingCode || getCountryCallingCode(defaultCountry, metadata.metadata)
|
|
322
347
|
}
|
|
323
348
|
else return {}
|
|
324
349
|
|
|
350
|
+
// At this point, `country` could be `undefined` but `countryCallingCode` is always defined.
|
|
351
|
+
//
|
|
352
|
+
// Also, if `country` is defined, its numbering plan is selected in `metadata`.
|
|
353
|
+
// Otherwise, the numering plan for the `countryCallingCode` is selected in `metadata`.
|
|
354
|
+
|
|
325
355
|
if (!number) {
|
|
326
356
|
return {
|
|
327
357
|
countryCallingCodeSource,
|
|
@@ -334,7 +364,7 @@ function parsePhoneNumber(
|
|
|
334
364
|
carrierCode
|
|
335
365
|
} = extractNationalNumber(
|
|
336
366
|
parseIncompletePhoneNumber(number),
|
|
337
|
-
|
|
367
|
+
undefined,
|
|
338
368
|
metadata
|
|
339
369
|
)
|
|
340
370
|
|
|
@@ -14,9 +14,9 @@ export default function validatePhoneNumberLength() {
|
|
|
14
14
|
// Parse phone number.
|
|
15
15
|
try {
|
|
16
16
|
const phoneNumber = parsePhoneNumberWithError(text, options, metadata)
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const result = checkNumberLength(phoneNumber.nationalNumber,
|
|
17
|
+
const metadataInstance = new Metadata(metadata)
|
|
18
|
+
metadataInstance.selectNumberingPlan(phoneNumber.country || phoneNumber.countryCallingCode)
|
|
19
|
+
const result = checkNumberLength(phoneNumber.nationalNumber, undefined, metadataInstance)
|
|
20
20
|
if (result !== 'IS_POSSIBLE') {
|
|
21
21
|
return result
|
|
22
22
|
}
|
package/types.d.cts
CHANGED
|
@@ -87,40 +87,11 @@ export type PhoneNumberType = 'PREMIUM_RATE' | 'TOLL_FREE' | 'SHARED_COST' | 'VO
|
|
|
87
87
|
*/
|
|
88
88
|
export type NumberType = PhoneNumberType | undefined;
|
|
89
89
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
// a `NationalNumber` or an `Extension` or a `CarrierCode` or a `CountryCallingCode`.
|
|
96
|
-
//
|
|
97
|
-
// Example:
|
|
98
|
-
//
|
|
99
|
-
// import type { E164Number, CarrierCode } from 'libphonenumber-js'
|
|
100
|
-
// const number: E164Number = '+78005553535'
|
|
101
|
-
// const carrierCode: CarrierCode = number
|
|
102
|
-
//
|
|
103
|
-
// Shows an error:
|
|
104
|
-
// Type 'E164Number' is not assignable to type 'CarrierCode'.
|
|
105
|
-
//
|
|
106
|
-
// Originally, the `__tag` property was declared optional in order to allow passing
|
|
107
|
-
// a generic `string` in place of a `Tagged` type argument to functions that receive such arguments:
|
|
108
|
-
// that would allow ingesting generic `string` values from an outside source such as a database.
|
|
109
|
-
// Without that, those externally-obtained `string` values would have to be forcefully converted
|
|
110
|
-
// to the `Tagged` type via TypeScript `as` operator which is an anti-pattern.
|
|
111
|
-
//
|
|
112
|
-
// Later, it has been decided to make the `__tag` property non-optional and instead
|
|
113
|
-
// declare the arguments of those functions as `string | Tagged` rather than just `Tagged`:
|
|
114
|
-
// that would still allow ingesting generic `string` values from an outside source
|
|
115
|
-
// and would also make the `Tagged` type more "type safe".
|
|
116
|
-
//
|
|
117
|
-
type Tagged<A, T> = A & { __tag: T };
|
|
118
|
-
|
|
119
|
-
export type E164Number = Tagged<string, "E164Number">;
|
|
120
|
-
export type NationalNumber = Tagged<string, "NationalNumber">;
|
|
121
|
-
export type Extension = Tagged<string, "Extension">;
|
|
122
|
-
export type CarrierCode = Tagged<string, "CarrierCode">;
|
|
123
|
-
export type CountryCallingCode = Tagged<string, "CountryCallingCode">;
|
|
90
|
+
export type E164Number = string;
|
|
91
|
+
export type NationalNumber = string;
|
|
92
|
+
export type Extension = string;
|
|
93
|
+
export type CarrierCode = string;
|
|
94
|
+
export type CountryCallingCode = string;
|
|
124
95
|
|
|
125
96
|
type FormatExtension = (formattedNumber: string, extension: Extension, metadata: MetadataJson) => string
|
|
126
97
|
|
package/types.d.ts
CHANGED
|
@@ -87,40 +87,11 @@ export type PhoneNumberType = 'PREMIUM_RATE' | 'TOLL_FREE' | 'SHARED_COST' | 'VO
|
|
|
87
87
|
*/
|
|
88
88
|
export type NumberType = PhoneNumberType | undefined;
|
|
89
89
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
// a `NationalNumber` or an `Extension` or a `CarrierCode` or a `CountryCallingCode`.
|
|
96
|
-
//
|
|
97
|
-
// Example:
|
|
98
|
-
//
|
|
99
|
-
// import type { E164Number, CarrierCode } from 'libphonenumber-js'
|
|
100
|
-
// const number: E164Number = '+78005553535'
|
|
101
|
-
// const carrierCode: CarrierCode = number
|
|
102
|
-
//
|
|
103
|
-
// Shows an error:
|
|
104
|
-
// Type 'E164Number' is not assignable to type 'CarrierCode'.
|
|
105
|
-
//
|
|
106
|
-
// Originally, the `__tag` property was declared optional in order to allow passing
|
|
107
|
-
// a generic `string` in place of a `Tagged` type argument to functions that receive such arguments:
|
|
108
|
-
// that would allow ingesting generic `string` values from an outside source such as a database.
|
|
109
|
-
// Without that, those externally-obtained `string` values would have to be forcefully converted
|
|
110
|
-
// to the `Tagged` type via TypeScript `as` operator which is an anti-pattern.
|
|
111
|
-
//
|
|
112
|
-
// Later, it has been decided to make the `__tag` property non-optional and instead
|
|
113
|
-
// declare the arguments of those functions as `string | Tagged` rather than just `Tagged`:
|
|
114
|
-
// that would still allow ingesting generic `string` values from an outside source
|
|
115
|
-
// and would also make the `Tagged` type more "type safe".
|
|
116
|
-
//
|
|
117
|
-
type Tagged<A, T> = A & { __tag: T };
|
|
118
|
-
|
|
119
|
-
export type E164Number = Tagged<string, "E164Number">;
|
|
120
|
-
export type NationalNumber = Tagged<string, "NationalNumber">;
|
|
121
|
-
export type Extension = Tagged<string, "Extension">;
|
|
122
|
-
export type CarrierCode = Tagged<string, "CarrierCode">;
|
|
123
|
-
export type CountryCallingCode = Tagged<string, "CountryCallingCode">;
|
|
90
|
+
export type E164Number = string;
|
|
91
|
+
export type NationalNumber = string;
|
|
92
|
+
export type Extension = string;
|
|
93
|
+
export type CarrierCode = string;
|
|
94
|
+
export type CountryCallingCode = string;
|
|
124
95
|
|
|
125
96
|
type FormatExtension = (formattedNumber: string, extension: Extension, metadata: MetadataJson) => string
|
|
126
97
|
|