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.
Files changed (221) hide show
  1. package/.gitlab-ci.yml +24 -9
  2. package/CHANGELOG.md +20 -15
  3. package/README.md +27 -10
  4. package/build/AsYouType.js +100 -27
  5. package/build/AsYouType.js.map +1 -1
  6. package/build/AsYouType.test.js.map +1 -1
  7. package/build/AsYouTypeFormatter.complete.js +2 -2
  8. package/build/AsYouTypeFormatter.complete.js.map +1 -1
  9. package/build/AsYouTypeFormatter.js +1 -1
  10. package/build/AsYouTypeFormatter.js.map +1 -1
  11. package/build/AsYouTypeState.js +15 -1
  12. package/build/AsYouTypeState.js.map +1 -1
  13. package/build/PhoneNumber.js +26 -39
  14. package/build/PhoneNumber.js.map +1 -1
  15. package/build/format.js +3 -3
  16. package/build/format.js.map +1 -1
  17. package/build/formatPhoneNumberForMobileDialing.js +4 -3
  18. package/build/formatPhoneNumberForMobileDialing.js.map +1 -1
  19. package/build/helpers/checkNumberLength.js +21 -6
  20. package/build/helpers/checkNumberLength.js.map +1 -1
  21. package/build/helpers/checkNumberLength.test.js.map +1 -1
  22. package/build/helpers/extractCountryCallingCode.js +16 -11
  23. package/build/helpers/extractCountryCallingCode.js.map +1 -1
  24. package/build/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +21 -10
  25. package/build/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js.map +1 -1
  26. package/build/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.test.js.map +1 -0
  27. package/build/helpers/extractNationalNumber.js +12 -3
  28. package/build/helpers/extractNationalNumber.js.map +1 -1
  29. package/build/helpers/extractNationalNumber.test.js.map +1 -1
  30. package/build/helpers/getCountryAndCallingCodeFromOneOfThem.js +35 -0
  31. package/build/helpers/getCountryAndCallingCodeFromOneOfThem.js.map +1 -0
  32. package/build/helpers/getCountryByCallingCode.js +10 -7
  33. package/build/helpers/getCountryByCallingCode.js.map +1 -1
  34. package/build/helpers/getCountryByNationalNumber.js +13 -9
  35. package/build/helpers/getCountryByNationalNumber.js.map +1 -1
  36. package/build/helpers/getIddPrefix.js +1 -1
  37. package/build/helpers/getIddPrefix.js.map +1 -1
  38. package/build/helpers/getNumberType.js +3 -3
  39. package/build/helpers/getNumberType.js.map +1 -1
  40. package/build/helpers/getPossibleCountriesForNumber.js +4 -7
  41. package/build/helpers/getPossibleCountriesForNumber.js.map +1 -1
  42. package/build/helpers/isCallingCode.js +11 -0
  43. package/build/helpers/isCallingCode.js.map +1 -0
  44. package/build/helpers/isCallingCode.test.js.map +1 -0
  45. package/build/helpers/isCountryCode.js +11 -0
  46. package/build/helpers/isCountryCode.js.map +1 -0
  47. package/build/helpers/isCountryCode.test.js.map +1 -0
  48. package/build/helpers/stripIddPrefix.js +9 -4
  49. package/build/helpers/stripIddPrefix.js.map +1 -1
  50. package/build/isPossible.js +14 -8
  51. package/build/isPossible.js.map +1 -1
  52. package/build/isValid.js +3 -3
  53. package/build/isValid.js.map +1 -1
  54. package/build/metadata.js +80 -31
  55. package/build/metadata.js.map +1 -1
  56. package/build/metadata.test.js.map +1 -1
  57. package/build/parse.js +36 -12
  58. package/build/parse.js.map +1 -1
  59. package/build/validatePhoneNumberLength.js +3 -3
  60. package/build/validatePhoneNumberLength.js.map +1 -1
  61. package/bundle/libphonenumber-js.min.js +1 -1
  62. package/bundle/libphonenumber-js.min.js.map +1 -1
  63. package/bundle/libphonenumber-max.js +1 -1
  64. package/bundle/libphonenumber-max.js.map +1 -1
  65. package/bundle/libphonenumber-min.js +1 -1
  66. package/bundle/libphonenumber-min.js.map +1 -1
  67. package/bundle/libphonenumber-mobile.js +1 -1
  68. package/bundle/libphonenumber-mobile.js.map +1 -1
  69. package/core/index.d.cts +1 -0
  70. package/core/index.d.ts +1 -0
  71. package/custom.d.ts +3 -1
  72. package/es6/AsYouType.js +100 -27
  73. package/es6/AsYouType.js.map +1 -1
  74. package/es6/AsYouType.test.js.map +1 -1
  75. package/es6/AsYouTypeFormatter.complete.js +2 -2
  76. package/es6/AsYouTypeFormatter.complete.js.map +1 -1
  77. package/es6/AsYouTypeFormatter.js +1 -1
  78. package/es6/AsYouTypeFormatter.js.map +1 -1
  79. package/es6/AsYouTypeState.js +15 -1
  80. package/es6/AsYouTypeState.js.map +1 -1
  81. package/es6/PhoneNumber.js +26 -39
  82. package/es6/PhoneNumber.js.map +1 -1
  83. package/es6/format.js +3 -3
  84. package/es6/format.js.map +1 -1
  85. package/es6/formatPhoneNumberForMobileDialing.js +4 -3
  86. package/es6/formatPhoneNumberForMobileDialing.js.map +1 -1
  87. package/es6/helpers/checkNumberLength.js +22 -6
  88. package/es6/helpers/checkNumberLength.js.map +1 -1
  89. package/es6/helpers/checkNumberLength.test.js.map +1 -1
  90. package/es6/helpers/extractCountryCallingCode.js +16 -11
  91. package/es6/helpers/extractCountryCallingCode.js.map +1 -1
  92. package/es6/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +21 -10
  93. package/es6/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js.map +1 -1
  94. package/es6/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.test.js.map +1 -0
  95. package/es6/helpers/extractNationalNumber.js +12 -3
  96. package/es6/helpers/extractNationalNumber.js.map +1 -1
  97. package/es6/helpers/extractNationalNumber.test.js.map +1 -1
  98. package/es6/helpers/getCountryAndCallingCodeFromOneOfThem.js +28 -0
  99. package/es6/helpers/getCountryAndCallingCodeFromOneOfThem.js.map +1 -0
  100. package/es6/helpers/getCountryByCallingCode.js +10 -7
  101. package/es6/helpers/getCountryByCallingCode.js.map +1 -1
  102. package/es6/helpers/getCountryByNationalNumber.js +13 -9
  103. package/es6/helpers/getCountryByNationalNumber.js.map +1 -1
  104. package/es6/helpers/getIddPrefix.js +1 -1
  105. package/es6/helpers/getIddPrefix.js.map +1 -1
  106. package/es6/helpers/getNumberType.js +3 -3
  107. package/es6/helpers/getNumberType.js.map +1 -1
  108. package/es6/helpers/getPossibleCountriesForNumber.js +4 -7
  109. package/es6/helpers/getPossibleCountriesForNumber.js.map +1 -1
  110. package/es6/helpers/isCallingCode.js +5 -0
  111. package/es6/helpers/isCallingCode.js.map +1 -0
  112. package/es6/helpers/isCallingCode.test.js.map +1 -0
  113. package/es6/helpers/isCountryCode.js +5 -0
  114. package/es6/helpers/isCountryCode.js.map +1 -0
  115. package/es6/helpers/isCountryCode.test.js.map +1 -0
  116. package/es6/helpers/stripIddPrefix.js +9 -4
  117. package/es6/helpers/stripIddPrefix.js.map +1 -1
  118. package/es6/isPossible.js +14 -8
  119. package/es6/isPossible.js.map +1 -1
  120. package/es6/isValid.js +3 -3
  121. package/es6/isValid.js.map +1 -1
  122. package/es6/metadata.js +80 -31
  123. package/es6/metadata.js.map +1 -1
  124. package/es6/metadata.test.js.map +1 -1
  125. package/es6/parse.js +36 -12
  126. package/es6/parse.js.map +1 -1
  127. package/es6/validatePhoneNumberLength.js +3 -3
  128. package/es6/validatePhoneNumberLength.js.map +1 -1
  129. package/es6-modern/AsYouType.js +100 -26
  130. package/es6-modern/AsYouType.js.map +1 -1
  131. package/es6-modern/AsYouType.test.js.map +1 -1
  132. package/es6-modern/AsYouTypeFormatter.complete.js +2 -2
  133. package/es6-modern/AsYouTypeFormatter.complete.js.map +1 -1
  134. package/es6-modern/AsYouTypeFormatter.js +1 -1
  135. package/es6-modern/AsYouTypeFormatter.js.map +1 -1
  136. package/es6-modern/AsYouTypeState.js +15 -1
  137. package/es6-modern/AsYouTypeState.js.map +1 -1
  138. package/es6-modern/PhoneNumber.js +25 -36
  139. package/es6-modern/PhoneNumber.js.map +1 -1
  140. package/es6-modern/format.js +3 -3
  141. package/es6-modern/format.js.map +1 -1
  142. package/es6-modern/formatPhoneNumberForMobileDialing.js +4 -3
  143. package/es6-modern/formatPhoneNumberForMobileDialing.js.map +1 -1
  144. package/es6-modern/helpers/checkNumberLength.js +22 -6
  145. package/es6-modern/helpers/checkNumberLength.js.map +1 -1
  146. package/es6-modern/helpers/checkNumberLength.test.js.map +1 -1
  147. package/es6-modern/helpers/extractCountryCallingCode.js +16 -11
  148. package/es6-modern/helpers/extractCountryCallingCode.js.map +1 -1
  149. package/es6-modern/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +21 -10
  150. package/es6-modern/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js.map +1 -1
  151. package/es6-modern/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.test.js.map +1 -0
  152. package/es6-modern/helpers/extractNationalNumber.js +12 -3
  153. package/es6-modern/helpers/extractNationalNumber.js.map +1 -1
  154. package/es6-modern/helpers/extractNationalNumber.test.js.map +1 -1
  155. package/es6-modern/helpers/getCountryAndCallingCodeFromOneOfThem.js +28 -0
  156. package/es6-modern/helpers/getCountryAndCallingCodeFromOneOfThem.js.map +1 -0
  157. package/es6-modern/helpers/getCountryByCallingCode.js +10 -7
  158. package/es6-modern/helpers/getCountryByCallingCode.js.map +1 -1
  159. package/es6-modern/helpers/getCountryByNationalNumber.js +13 -10
  160. package/es6-modern/helpers/getCountryByNationalNumber.js.map +1 -1
  161. package/es6-modern/helpers/getIddPrefix.js +1 -1
  162. package/es6-modern/helpers/getIddPrefix.js.map +1 -1
  163. package/es6-modern/helpers/getNumberType.js +3 -3
  164. package/es6-modern/helpers/getNumberType.js.map +1 -1
  165. package/es6-modern/helpers/getPossibleCountriesForNumber.js +4 -7
  166. package/es6-modern/helpers/getPossibleCountriesForNumber.js.map +1 -1
  167. package/es6-modern/helpers/isCallingCode.js +5 -0
  168. package/es6-modern/helpers/isCallingCode.js.map +1 -0
  169. package/es6-modern/helpers/isCallingCode.test.js.map +1 -0
  170. package/es6-modern/helpers/isCountryCode.js +5 -0
  171. package/es6-modern/helpers/isCountryCode.js.map +1 -0
  172. package/es6-modern/helpers/isCountryCode.test.js.map +1 -0
  173. package/es6-modern/helpers/stripIddPrefix.js +9 -4
  174. package/es6-modern/helpers/stripIddPrefix.js.map +1 -1
  175. package/es6-modern/isPossible.js +14 -8
  176. package/es6-modern/isPossible.js.map +1 -1
  177. package/es6-modern/isValid.js +3 -3
  178. package/es6-modern/isValid.js.map +1 -1
  179. package/es6-modern/metadata.js +78 -31
  180. package/es6-modern/metadata.js.map +1 -1
  181. package/es6-modern/metadata.test.js.map +1 -1
  182. package/es6-modern/parse.js +41 -12
  183. package/es6-modern/parse.js.map +1 -1
  184. package/es6-modern/validatePhoneNumberLength.js +3 -3
  185. package/es6-modern/validatePhoneNumberLength.js.map +1 -1
  186. package/index.d.cts +1 -0
  187. package/index.d.ts +1 -0
  188. package/max/index.d.cts +1 -0
  189. package/max/index.d.ts +1 -0
  190. package/min/index.d.cts +1 -0
  191. package/min/index.d.ts +1 -0
  192. package/mobile/index.d.cts +1 -0
  193. package/mobile/index.d.ts +1 -0
  194. package/package.json +1 -1
  195. package/source/AsYouType.js +108 -27
  196. package/source/AsYouTypeFormatter.complete.js +2 -2
  197. package/source/AsYouTypeFormatter.js +1 -1
  198. package/source/AsYouTypeState.js +15 -1
  199. package/source/PhoneNumber.js +24 -39
  200. package/source/format.js +3 -3
  201. package/source/formatPhoneNumberForMobileDialing.js +4 -3
  202. package/source/helpers/checkNumberLength.js +21 -6
  203. package/source/helpers/extractCountryCallingCode.js +16 -11
  204. package/source/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +20 -10
  205. package/source/helpers/extractNationalNumber.js +11 -3
  206. package/source/helpers/getCountryAndCallingCodeFromOneOfThem.js +31 -0
  207. package/source/helpers/getCountryByCallingCode.js +10 -7
  208. package/source/helpers/getCountryByNationalNumber.js +14 -11
  209. package/source/helpers/getIddPrefix.js +1 -1
  210. package/source/helpers/getNumberType.js +3 -3
  211. package/source/helpers/getPossibleCountriesForNumber.js +4 -7
  212. package/source/helpers/isCallingCode.js +5 -0
  213. package/source/helpers/isCountryCode.js +5 -0
  214. package/source/helpers/stripIddPrefix.js +10 -4
  215. package/source/isPossible.js +13 -7
  216. package/source/isValid.js +3 -3
  217. package/source/metadata.js +77 -33
  218. package/source/parse.js +41 -11
  219. package/source/validatePhoneNumberLength.js +3 -3
  220. package/types.d.cts +5 -34
  221. package/types.d.ts +5 -34
@@ -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
- selectNumberingPlan(countryCode, callingCode) {
79
- // Supports just passing `callingCode` as the first argument.
80
- if (countryCode && CALLING_CODE_REG_EXP.test(countryCode)) {
81
- callingCode = countryCode
82
- countryCode = null
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
- if (!this.hasCountry(countryCode)) {
104
+ const countryMetadata = this.getCountryMetadata(countryCode)
105
+ if (!countryMetadata) {
86
106
  throw new Error(`Unknown country: ${countryCode}`)
87
107
  }
88
- this.numberingPlan = new NumberingPlan(this.getCountryMetadata(countryCode), this)
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
- // Formatting information for regions which share
236
- // a country calling code is contained by only one region
237
- // for performance reasons. For example, for NANPA region
238
- // ("North American Numbering Plan Administration",
239
- // which includes USA, Canada, Cayman Islands, Bahamas, etc)
240
- // it will be contained in the metadata for `US`.
241
- getDefaultCountryMetadataForRegion() {
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.getDefaultCountryMetadataForRegion()) || []
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.getDefaultCountryMetadataForRegion())
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.getDefaultCountryMetadataForRegion())
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, metadata) {
483
- metadata = new Metadata(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} metadata
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, metadata) {
501
- metadata = new Metadata(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
- export function isSupportedCountry(country, metadata) {
509
- // metadata = new Metadata(metadata)
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 metadata.countries.hasOwnProperty(country)
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} metadata
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, metadata) {
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(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, country, metadata) ? true : false
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
- // Extract calling code from phone number.
291
- let { countryCallingCodeSource, countryCallingCode, number } = extractCountryCallingCode(
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(defaultCallingCode)) {
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
- country,
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
- metadata = new Metadata(metadata)
18
- metadata.selectNumberingPlan(phoneNumber.countryCallingCode)
19
- const result = checkNumberLength(phoneNumber.nationalNumber, phoneNumber.country, metadata)
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
- // "Tagged" types are used to introduce some degree of type safety when passing in arguments to the functions.
91
- // https://medium.com/@ethanresnick/advanced-typescript-tagged-types-for-fewer-bugs-and-better-security-24db681d5721
92
- //
93
- // For example, if some function returns an `E164Number`, it can only be interpreted as
94
- // either a `string` or an `E164Number` and it can't mistakenly be interpreted as
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
- // "Tagged" types are used to introduce some degree of type safety when passing in arguments to the functions.
91
- // https://medium.com/@ethanresnick/advanced-typescript-tagged-types-for-fewer-bugs-and-better-security-24db681d5721
92
- //
93
- // For example, if some function returns an `E164Number`, it can only be interpreted as
94
- // either a `string` or an `E164Number` and it can't mistakenly be interpreted as
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