libphonenumber-js 1.13.2 → 1.13.4

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 (233) hide show
  1. package/.gitlab-ci.yml +24 -9
  2. package/CHANGELOG.md +15 -2
  3. package/README.md +29 -21
  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/max/metadata/metadata.max.json +1 -1
  191. package/max/metadata/metadata.max.json.js +1 -1
  192. package/metadata.full.json +1 -1
  193. package/metadata.full.json.js +1 -1
  194. package/metadata.max.json +1 -1
  195. package/metadata.max.json.js +1 -1
  196. package/metadata.min.json +1 -1
  197. package/metadata.min.json.js +1 -1
  198. package/metadata.mobile.json +1 -1
  199. package/metadata.mobile.json.js +1 -1
  200. package/min/index.d.cts +1 -0
  201. package/min/index.d.ts +1 -0
  202. package/min/metadata/metadata.min.json +1 -1
  203. package/min/metadata/metadata.min.json.js +1 -1
  204. package/mobile/index.d.cts +1 -0
  205. package/mobile/index.d.ts +1 -0
  206. package/mobile/metadata/metadata.mobile.json +1 -1
  207. package/mobile/metadata/metadata.mobile.json.js +1 -1
  208. package/package.json +1 -1
  209. package/source/AsYouType.js +108 -27
  210. package/source/AsYouTypeFormatter.complete.js +2 -2
  211. package/source/AsYouTypeFormatter.js +1 -1
  212. package/source/AsYouTypeState.js +15 -1
  213. package/source/PhoneNumber.js +24 -39
  214. package/source/format.js +3 -3
  215. package/source/formatPhoneNumberForMobileDialing.js +4 -3
  216. package/source/helpers/checkNumberLength.js +21 -6
  217. package/source/helpers/extractCountryCallingCode.js +16 -11
  218. package/source/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +20 -10
  219. package/source/helpers/extractNationalNumber.js +11 -3
  220. package/source/helpers/getCountryAndCallingCodeFromOneOfThem.js +31 -0
  221. package/source/helpers/getCountryByCallingCode.js +10 -7
  222. package/source/helpers/getCountryByNationalNumber.js +14 -11
  223. package/source/helpers/getIddPrefix.js +1 -1
  224. package/source/helpers/getNumberType.js +3 -3
  225. package/source/helpers/getPossibleCountriesForNumber.js +4 -7
  226. package/source/helpers/isCallingCode.js +5 -0
  227. package/source/helpers/isCountryCode.js +5 -0
  228. package/source/helpers/stripIddPrefix.js +10 -4
  229. package/source/isPossible.js +13 -7
  230. package/source/isValid.js +3 -3
  231. package/source/metadata.js +77 -33
  232. package/source/parse.js +41 -11
  233. package/source/validatePhoneNumberLength.js +3 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libphonenumber-js",
3
- "version": "1.13.2",
3
+ "version": "1.13.4",
4
4
  "description": "A simpler (and smaller) rewrite of Google Android's libphonenumber library in javascript",
5
5
  "main": "index.cjs",
6
6
  "module": "index.js",
@@ -1,5 +1,6 @@
1
1
  import Metadata from './metadata.js'
2
2
  import PhoneNumber from './PhoneNumber.js'
3
+ import checkNumberLength from './helpers/checkNumberLength.js'
3
4
  import AsYouTypeState from './AsYouTypeState.js'
4
5
  import AsYouTypeFormatter, { DIGIT_PLACEHOLDER } from './AsYouTypeFormatter.js'
5
6
  import AsYouTypeParser, { extractFormattedDigitsAndPlus } from './AsYouTypeParser.js'
@@ -15,6 +16,9 @@ export default class AsYouType {
15
16
  * @param {Object} metadata
16
17
  */
17
18
  constructor(optionsOrDefaultCountry, metadata) {
19
+ // Metadata instance is created once and stays constant.
20
+ // This is required because other utility classes such as `AsYouTypeFormatter`
21
+ // will use this metadata instance "reference" and will assume that it stays constant.
18
22
  this.metadata = new Metadata(metadata)
19
23
  const [defaultCountry, defaultCallingCode] = this.getCountryAndCallingCode(optionsOrDefaultCountry)
20
24
  // `this.defaultCountry` and `this.defaultCallingCode` aren't required to be in sync.
@@ -103,7 +107,7 @@ export default class AsYouType {
103
107
  this.country = country
104
108
  },
105
109
  onCallingCodeChange: (callingCode, country) => {
106
- this.metadata.selectNumberingPlan(country, callingCode)
110
+ this.metadata.selectNumberingPlan(country || callingCode)
107
111
  this.formatter.reset(this.metadata.numberingPlan, this.state)
108
112
  this.parser.reset(this.metadata.numberingPlan)
109
113
  }
@@ -276,13 +280,35 @@ export default class AsYouType {
276
280
  // entered so far based on the country phone code
277
281
  // and the national phone number.
278
282
  determineTheCountry() {
279
- this.state.setCountry(getCountryByCallingCode(
283
+ // Get a more precise country code for this national number.
284
+ // It is possible that the precise country couldn't be determined
285
+ // in cases when there're multiple countries sharing the same calling code.
286
+ // In that case, `country` will be `undefined`.
287
+ const country = getCountryByCallingCode(
280
288
  this.isInternational() ? this.state.callingCode : this.defaultCallingCode,
281
289
  {
282
290
  nationalNumber: this.state.nationalSignificantNumber,
283
291
  metadata: this.metadata
284
292
  }
285
- ))
293
+ )
294
+ // If the derived country is indeed more precise,
295
+ // or if no country could be derived for the updated phone number.
296
+ if (country !== this.state.country) {
297
+ // Set (or reset) the more precise country.
298
+ this.state.setCountry(country)
299
+ // If a more precise country could be derived.
300
+ if (country) {
301
+ // Select a more precise telephone numbering plan.
302
+ // For example, consider that `AsYouType` instance was created with `defaultCountry: "US"` argument.
303
+ // In that case, the metadata instance will be using "US" metadata.
304
+ // But then the user could input a Canadian phone number, in which case
305
+ // the metadata instance should be switched to Canadian telephone numbering plan.
306
+ // It is required to do that because later `canFormatCompleteNumber()` function will be called,
307
+ // and that function relies on the precise country being pre-selected in the metadata instance.
308
+ // Or, for example, `checkNumberLength()` function also relies on same thing.
309
+ this.metadata.selectNumberingPlan(country)
310
+ }
311
+ }
286
312
  }
287
313
 
288
314
  /**
@@ -343,18 +369,19 @@ export default class AsYouType {
343
369
  callingCode
344
370
  } = this.state
345
371
 
346
- // `this._getCountry()` is basically same as `this.state.country`
347
- // with the only change that it return `undefined` in case of a
348
- // "non-geographic" numbering plan instead of `"001"` "internal use" value.
349
- let country = this._getCountry()
350
-
351
372
  if (!nationalSignificantNumber) {
352
373
  return
353
374
  }
354
375
 
355
- // `state.country` and `state.callingCode` aren't required to be in sync.
356
- // For example, `country` could be `"AR"` and `callingCode` could be `undefined`.
357
- // So `country` and `callingCode` are totally independent.
376
+ // `this._getCountry()` is basically same as `this.state.country`
377
+ // with the only change that it returns `undefined` in case of a
378
+ // "non-geographic" numbering plan instead of `"001"` "internal use" value.
379
+ let country = this._getCountry()
380
+
381
+ // Sidenote:
382
+ // `state.callingCode` and `state.country` are somewhat independent from one another
383
+ // and there could be situations when one is defined but the other is not.
384
+ // See the comment in `AsYouTypeState.js` for more info on when that is possible.
358
385
 
359
386
  if (!country && !callingCode) {
360
387
  return
@@ -370,26 +397,36 @@ export default class AsYouType {
370
397
  //
371
398
  // Because `AsYouType.getNumber()` method is supposed to be a 1:1
372
399
  // equivalent for `parsePhoneNumber(AsYouType.getNumberValue())`,
373
- // then it should also behave accordingly in cases of `country` ambiguity.
400
+ // it means that it should also behave accordingly in such cases of `country` ambiguity.
374
401
  // That's how users of this library would expect it to behave anyway.
375
402
  //
403
+ // So here it checks if `state.country` is same as the `defaultCountry`,
404
+ // and if it is, it attempts to get rid of any potential ambiguity.
405
+ //
376
406
  if (country) {
377
407
  if (country === this.defaultCountry) {
378
- // `state.country` and `state.callingCode` aren't required to be in sync.
379
- // For example, `state.country` could be `"AR"` and `state.callingCode` could be `undefined`.
380
- // So `state.country` and `state.callingCode` are totally independent.
381
- const metadata = new Metadata(this.metadata.metadata)
382
- metadata.selectNumberingPlan(country)
383
- const callingCode = metadata.numberingPlan.callingCode()
384
- const ambiguousCountries = this.metadata.getCountryCodesForCallingCode(callingCode)
385
- if (ambiguousCountries.length > 1) {
386
- const exactCountry = getCountryByNationalNumber(nationalSignificantNumber, {
387
- countries: ambiguousCountries,
388
- metadata: this.metadata.metadata
389
- })
390
- if (exactCountry) {
391
- country = exactCountry
392
- }
408
+ // // Create a `Metadata` instance just to get the calling code of the `country`.
409
+ // const metadata = new Metadata(this.metadata.metadata)
410
+ // // `state.callingCode` and `state.country` are somewhat independent from one another
411
+ // // and there could be situations when one is defined but the other is not.
412
+ // // See the comment in `AsYouTypeState.js` for more info on when that is possible.
413
+ // // Here, it only passes `state.country` because there's no need to pass `state.callingCode`
414
+ // // because `state.country` is known to be defined.
415
+ // metadata.selectNumberingPlan(country)
416
+ // const countryCallingCode = metadata.numberingPlan.callingCode()
417
+
418
+ // Instead of getting `countryCallingCode` value above,
419
+ // it could simply read it from the existing metadata instance.
420
+ const countryCallingCode = this.metadata.numberingPlan.callingCode()
421
+
422
+ // Attempt to rule out any potential country code ambiguity, if present.
423
+ const exactCountry = getExactCountryForMultiCountryCallingCode(
424
+ countryCallingCode,
425
+ nationalSignificantNumber,
426
+ this.metadata
427
+ )
428
+ if (exactCountry) {
429
+ country = exactCountry
393
430
  }
394
431
  }
395
432
  }
@@ -432,6 +469,41 @@ export default class AsYouType {
432
469
  return phoneNumber.isValid()
433
470
  }
434
471
 
472
+ /**
473
+ * Checks if the phone number length is valid.
474
+ * If it is, nothing is returned.
475
+ * Otherwise, a rejection reason is returned.
476
+ * @return {string?}
477
+ */
478
+ validateLength() {
479
+ const {
480
+ digits,
481
+ nationalSignificantNumber
482
+ } = this.state
483
+
484
+ if (!digits) {
485
+ return 'NOT_A_NUMBER'
486
+ }
487
+
488
+ if (!this.metadata.numberingPlan) {
489
+ return 'INVALID_COUNTRY'
490
+ }
491
+
492
+ if (!nationalSignificantNumber) {
493
+ return 'TOO_SHORT'
494
+ }
495
+
496
+ // `state.callingCode` and `state.country` are somewhat independent from one another
497
+ // and there could be situations when one is defined but the other is not.
498
+ // See the comment in `AsYouTypeState.js` for more info on when that is possible.
499
+ // Here, it uses both: if `country` is defined then it will use that,
500
+ // otherwise it will use `callingCode`.
501
+ const result = checkNumberLength(nationalSignificantNumber, undefined, this.metadata)
502
+ if (result !== 'IS_POSSIBLE') {
503
+ return result
504
+ }
505
+ }
506
+
435
507
  /**
436
508
  * @deprecated
437
509
  * This method is used in `react-phone-number-input/source/input-control.js`
@@ -456,4 +528,13 @@ export default class AsYouType {
456
528
  getTemplate() {
457
529
  return this.formatter.getTemplate(this.state) || this.getNonFormattedTemplate() || ''
458
530
  }
531
+ }
532
+
533
+ // In situations when multiple countries use the same calling code,
534
+ // this function tells what exact country does a given national (significant) number belong to.
535
+ function getExactCountryForMultiCountryCallingCode(callingCode, nationalSignificantNumber, metadata) {
536
+ const ambiguousCountries = metadata.getCountryCodesForCallingCode(callingCode)
537
+ if (ambiguousCountries.length > 1) {
538
+ return getCountryByNationalNumber(nationalSignificantNumber, ambiguousCountries, metadata.metadata)
539
+ }
459
540
  }
@@ -21,8 +21,8 @@ export default function formatCompleteNumber(state, format, {
21
21
  }
22
22
  }
23
23
 
24
- export function canFormatCompleteNumber(nationalSignificantNumber, country, metadata) {
25
- return checkNumberLength(nationalSignificantNumber, country, metadata) === 'IS_POSSIBLE'
24
+ export function canFormatCompleteNumber(nationalSignificantNumber, metadata) {
25
+ return checkNumberLength(nationalSignificantNumber, undefined, metadata) === 'IS_POSSIBLE'
26
26
  }
27
27
 
28
28
  function formatNationalNumberWithAndWithoutNationalPrefixFormattingRule(state, format, {
@@ -154,7 +154,7 @@ export default class AsYouTypeFormatter {
154
154
  // If so, we should use this instead of any other formatting template
155
155
  // whose leadingDigitsPattern also matches the input."
156
156
  //
157
- if (canFormatCompleteNumber(state.nationalSignificantNumber, state.country, this.metadata)) {
157
+ if (canFormatCompleteNumber(state.nationalSignificantNumber, this.metadata)) {
158
158
  for (const format of this.matchingFormats) {
159
159
  const formattedCompleteNumber = formatCompleteNumber(
160
160
  state,
@@ -1,5 +1,20 @@
1
1
  // This "state" object simply holds the state of the "AsYouType" parser:
2
2
  //
3
+ // Sidenote:
4
+ // `state.callingCode` and `state.country` are somewhat independent from one another
5
+ // and there could be situations when one is defined but the other is not.
6
+ // * Situations when `state.country` would be defined but `state.callingCode` would be `undefined`:
7
+ // * When `defaultCountry` is specified and inputting a phone number not in "international" format.
8
+ // * When `defaultCountry` is specified and inputting a phone number in "international" format,
9
+ // but before the `+` character has been input.
10
+ // * Situations when `state.country` would be `undefined` but `state.callingCode` would be defined:
11
+ // * When `defaultCountry` is not specified and inputting a phone number in "international" format,
12
+ // and the "calling code" part is already complete, but multiple countries share this "calling code"
13
+ // and there's not enough national (significant) number digits yet to determine the exact country.
14
+ // * In any other sigutations, `state.country` and `state.callingCode` are either both defined or both `undefined`.
15
+ // * When they're both defined, `state.callingCode` always corresponds to `state.country`.
16
+ // So both are always consistent in this case.
17
+ //
3
18
  // * `country?: string` — The exact country of the phone number, if it could be determined.
4
19
  // When inputting a phone number in "international" format, it will derive the `country` from "country calling code" and the phone number digits.
5
20
  // When inputting a phone number in "national" format, it will derive the `country` from `defaultCountry` that was specified when creating the `AsYouType` formatter.
@@ -7,7 +22,6 @@
7
22
  // * `callingCode?: string` — "Country calling code" that has been extracted from the input phone number.
8
23
  // When inputting a phone number in "international" format, it will extract the "country calling code" from the digits that follow the "+" character.
9
24
  // When inputting a phone number in "national" format, `callingCode` will be `undefined`.
10
- // Sidenote: `state.callingCode` is therefore independent from `state.country` and there could be situations when `state.country` is defined by `state.callingCode` is not — that would be when inputting a phone number not in "international" format.
11
25
  // * `digits: string` — Phone number digits that have been input so far, including the "+" character, if present. In case of inputting non-arabic digits, those will be converted to arabic ones.
12
26
  // * `international: boolean` — Whether the phone number is being input in "international" format, i.e. with a "+" character.
13
27
  // * `missingPlus: boolean` — Whether it's a phone number in "international" format that is missing the leading "+" character for some reason — apparently, Google thinks that it's a common mistake when inputting a phone number.
@@ -2,6 +2,7 @@ import Metadata, { validateMetadata } from './metadata.js'
2
2
  import isPossibleNumber from './isPossible.js'
3
3
  import isValidNumber from './isValid.js'
4
4
  import getNumberType from './helpers/getNumberType.js'
5
+ import getCountryAndCallingCodeFromOneOfThem from './helpers/getCountryAndCallingCodeFromOneOfThem.js'
5
6
  import getPossibleCountriesForNumber from './helpers/getPossibleCountriesForNumber.js'
6
7
  import extractCountryCallingCode from './helpers/extractCountryCallingCode.js'
7
8
  import isObject from './helpers/isObject.js'
@@ -57,7 +58,7 @@ export default class PhoneNumber {
57
58
  validateMetadata(metadata)
58
59
 
59
60
  // Initialize properties.
60
- const { country, countryCallingCode } = getCountryAndCountryCallingCode(
61
+ const { country, callingCode: countryCallingCode } = getCountryAndCallingCodeFromOneOfThem(
61
62
  countryOrCountryCallingCode,
62
63
  metadata
63
64
  )
@@ -104,16 +105,29 @@ export default class PhoneNumber {
104
105
  return this.number === phoneNumber.number && this.ext === phoneNumber.ext
105
106
  }
106
107
 
107
- // This function was originally meant to be an equivalent for `validatePhoneNumberLength()`,
108
- // but later it was found out that it doesn't include the possible `TOO_SHORT` result
109
- // returned from `parsePhoneNumberWithError()` in the original `validatePhoneNumberLength()`,
110
- // so eventually I simply commented out this method from the `PhoneNumber` class
111
- // and just left the `validatePhoneNumberLength()` function, even though that one would require
112
- // and additional step to also validate the actual country / calling code of the phone number.
108
+ // `validateLength()` method was originally meant to be an equivalent for `validatePhoneNumberLength()`.
109
+ //
110
+ // Later, it became apparent that it's not really a true equivalent.
111
+ // The reason is that a `PhoneNumber` instance is not created
112
+ // when the phone number string is too short for it to be considered a valid phone number:
113
+ // * When there must be at least 2 national (significant) number digits: `"1"`.
114
+ // * When the country calling code part of an international number is incomplete: `"+12"`.
115
+ //
116
+ // So leaving this `validateLength()` method here would suggest a hidden anti-pattern
117
+ // of using it instead of `validatePhoneNumberLength()` while ignoring
118
+ // the "too short to be even possible" case from phone number length validation.
119
+ // And ignoring that case wouldn't make any sense in a real-world application
120
+ // because it would still be a valid case that should be handled.
121
+ //
122
+ // Because of that, this method was eventually commented out in order to not introduce
123
+ // that kind of an anti-pattern.
124
+ //
113
125
  // validateLength() {
114
- // const metadata = new Metadata(this.getMetadata())
115
- // metadata.selectNumberingPlan(this.countryCallingCode)
116
- // const result = checkNumberLength(this.nationalNumber, metadata)
126
+ // const result = checkNumberLength(
127
+ // this.nationalNumber,
128
+ // undefined,
129
+ // this.getMetadata()
130
+ // )
117
131
  // if (result !== 'IS_POSSIBLE') {
118
132
  // return result
119
133
  // }
@@ -145,33 +159,4 @@ export default class PhoneNumber {
145
159
  }
146
160
  }
147
161
 
148
- const isCountryCode = (value) => /^[A-Z]{2}$/.test(value)
149
-
150
- function getCountryAndCountryCallingCode(countryOrCountryCallingCode, metadataJson) {
151
- let country
152
- let countryCallingCode
153
-
154
- const metadata = new Metadata(metadataJson)
155
- // If country code is passed then derive `countryCallingCode` from it.
156
- // Also store the country code as `.country`.
157
- if (isCountryCode(countryOrCountryCallingCode)) {
158
- country = countryOrCountryCallingCode
159
- metadata.selectNumberingPlan(country)
160
- countryCallingCode = metadata.countryCallingCode()
161
- } else {
162
- countryCallingCode = countryOrCountryCallingCode
163
- /* istanbul ignore if */
164
- if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
165
- if (metadata.isNonGeographicCallingCode(countryCallingCode)) {
166
- country = '001'
167
- }
168
- }
169
- }
170
-
171
- return {
172
- country,
173
- countryCallingCode
174
- }
175
- }
176
-
177
162
  const E164_NUMBER_REGEXP = /^\+\d+$/
package/source/format.js CHANGED
@@ -25,10 +25,10 @@ const DEFAULT_OPTIONS = {
25
25
  * @param {object|PhoneNumber} input — If `options.v2: true` flag is passed, the `input` should be a `PhoneNumber` instance. Otherwise, it should be an object of shape `{ phone: '...', country: '...' }`.
26
26
  * @param {string} format
27
27
  * @param {object} [options]
28
- * @param {object} metadata
28
+ * @param {object} metadataJson
29
29
  * @return {string}
30
30
  */
31
- export default function formatNumber(input, format, options, metadata) {
31
+ export default function formatNumber(input, format, options, metadataJson) {
32
32
  // Apply default options.
33
33
  if (options) {
34
34
  // Using ES6 "rest spread" syntax here didn't work with `babel`/`istanbul`
@@ -43,7 +43,7 @@ export default function formatNumber(input, format, options, metadata) {
43
43
  options = DEFAULT_OPTIONS
44
44
  }
45
45
 
46
- metadata = new Metadata(metadata)
46
+ const metadata = new Metadata(metadataJson)
47
47
 
48
48
  // Normally, the `input` object is supposed to be a `PhoneNumber` class instance.
49
49
  // Also, according to the `PhoneNumber` class source code, `country` can't be "001".
@@ -22,10 +22,11 @@ const REGION_CODE_FOR_NON_GEO_ENTITY = '001'
22
22
  * @param {string} from_country - the region where the call is being placed.
23
23
  * @param {boolean} with_formatting - whether the number should be returned with
24
24
  * formatting symbols, such as spaces and dashes.
25
+ * @param {object} metadataJson
25
26
  * @return {string}
26
27
  */
27
- export default function(number, from_country, with_formatting, metadata) {
28
- metadata = new Metadata(metadata)
28
+ export default function(number, from_country, with_formatting, metadataJson) {
29
+ const metadata = new Metadata(metadataJson)
29
30
 
30
31
  // Validate `from_country`.
31
32
  if (!metadata.hasCountry(from_country)) {
@@ -70,7 +71,7 @@ export default function(number, from_country, with_formatting, metadata) {
70
71
  metadata.selectNumberingPlan(country)
71
72
 
72
73
  if (can_be_internationally_dialled(number) &&
73
- checkNumberLength(number.phone, country, metadata) !== 'TOO_SHORT') {
74
+ checkNumberLength(number.phone, undefined, metadata) !== 'TOO_SHORT') {
74
75
  formatted_number = format(number, 'INTERNATIONAL', metadata.metadata)
75
76
  }
76
77
  else {
@@ -1,10 +1,27 @@
1
1
  import Metadata from '../metadata.js'
2
2
  import mergeArrays from './mergeArrays.js'
3
3
 
4
+ /**
5
+ * Checks phone number length against the phone numbering plan selected in metadata.
6
+ * Optionally, it could be restrained by a specific country.
7
+ * @param {string} nationalNumber
8
+ * @param {string?} country — Specific country to use rather than the one that is pre-selected in the metadata instance.
9
+ * @param {Metadata} metadata — Metadata instance with a selected numbering plan.
10
+ * @returns {string}
11
+ */
4
12
  export default function checkNumberLength(nationalNumber, country, metadata) {
5
- return checkNumberLengthForType(nationalNumber, country, undefined, metadata)
13
+ return checkNumberLengthForType(nationalNumber, undefined, country, metadata)
6
14
  }
7
15
 
16
+ /**
17
+ * Checks phone number length against the phone numbering plan selected in metadata.
18
+ * Optionally, it could be restrained by a specific phone number type or a specific country.
19
+ * @param {string} nationalNumber
20
+ * @param {string?} type — Specific phone number type.
21
+ * @param {string?} country — Specific country to use instead of the one that is pre-selected in the metadata instance.
22
+ * @param {Metadata} metadata — Metadata instance with a selected numbering plan.
23
+ * @returns {string} See the README on `validatePhoneNumberLength()` function, with an addition of a special case: when phone numbers of type `type` aren't possible in the telephone numbering plan, it will return "INVALID_LENGTH".
24
+ */
8
25
  // Checks whether a number is possible for a certain `country` based on the number length.
9
26
  //
10
27
  // This function is not supported by metadata generated with ancient versions of
@@ -31,11 +48,9 @@ export default function checkNumberLength(nationalNumber, country, metadata) {
31
48
  // to check phone number length for that specific `country` rather than the "main" country
32
49
  // for the shared "country calling code".
33
50
  //
34
- export function checkNumberLengthForType(nationalNumber, country, type, metadata) {
35
- // If the exact `country` is specified, it's no necessarily already selected in `metadata`.
36
- // Most likely, in cases when there're multiple countries corresponding to the same
37
- // "country calling code", the "main" country for that "country calling code" will be selected.
51
+ export function checkNumberLengthForType(nationalNumber, type, country, metadata) {
38
52
  if (country) {
53
+ // If a specific `country` is specified, re-create a metadata instance with it.
39
54
  metadata = new Metadata(metadata.metadata)
40
55
  metadata.selectNumberingPlan(country)
41
56
  }
@@ -63,7 +78,7 @@ export function checkNumberLengthForType(nationalNumber, country, type, metadata
63
78
  if (!metadata.type('FIXED_LINE')) {
64
79
  // The rare case has been encountered where no fixedLine data is available
65
80
  // (true for some non-geographic entities), so we just check mobile.
66
- return checkNumberLengthForType(nationalNumber, country, 'MOBILE', metadata)
81
+ return checkNumberLengthForType(nationalNumber, 'MOBILE', country, metadata)
67
82
  }
68
83
 
69
84
  const mobile_type = metadata.type('MOBILE')
@@ -4,19 +4,24 @@ import Metadata from '../metadata.js'
4
4
  import { MAX_LENGTH_COUNTRY_CODE } from '../constants.js'
5
5
 
6
6
  /**
7
- * Converts a phone number digits (possibly with a `+`)
8
- * into a calling code and the rest phone number digits.
9
- * The "rest phone number digits" could include
10
- * a national prefix, carrier code, and national
11
- * (significant) number.
7
+ * Attempts to extract a calling code from a phone number.
8
+ * * If the phone number is found to be "international":
9
+ * * It will attempt to identify the calling code part of it and whether that part is complete and valid.
10
+ * * If the calling code part is complete and valid, it will return two properties:
11
+ * that calling code part (without `+`) and the rest of the digits.
12
+ * * Otherwise, i.e. if the calling code part is incomplete or invalid,
13
+ * it will return an empty object.
14
+ * * Otherwise, i.e. if the phone number is national, there's no callind code to extract
15
+ * so it will just return the originally-passed `number` string as the only property.
12
16
  * @param {string} number — Phone number digits (possibly with a `+`).
13
17
  * @param {string} [country] — Country.
14
18
  * @param {string} [defaultCountry] — Default country.
15
19
  * @param {string} [defaultCallingCode] — Default calling code (some phone numbering plans are non-geographic).
16
- * @param {object} metadata
17
- * @return {object} `{ countryCallingCodeSource: string?, countryCallingCode: string?, number: string }`
20
+ * @param {object} metadataJson
21
+ * @return {object} `{ countryCallingCodeSource: string?, countryCallingCode: string?, number: string }`, where `countryCallingCodeSource` tells how the returned calling code was extracted (if it was extracted), `countryCallingCode` is the calling code that was extracted from the input `number` string, and `number` is the originally passed `number` without the extracted calling code (and without a `+`). If the calling code is present but incomplete or invalid, it will return an empty object.
18
22
  * @example
19
23
  * // Returns `{ countryCallingCode: "1", number: "2133734253" }`.
24
+ * extractCountryCallingCode('2133734253', 'US', null, null, metadata)
20
25
  * extractCountryCallingCode('2133734253', null, 'US', null, metadata)
21
26
  * extractCountryCallingCode('2133734253', null, null, '1', metadata)
22
27
  * extractCountryCallingCode('+12133734253', null, null, null, metadata)
@@ -27,7 +32,7 @@ export default function extractCountryCallingCode(
27
32
  country,
28
33
  defaultCountry,
29
34
  defaultCallingCode,
30
- metadata
35
+ metadataJson
31
36
  ) {
32
37
  if (!number) {
33
38
  return {}
@@ -45,7 +50,7 @@ export default function extractCountryCallingCode(
45
50
  if (number[0] !== '+') {
46
51
  // Convert an "out-of-country" dialing phone number
47
52
  // to a proper international phone number.
48
- const numberWithoutIDD = stripIddPrefix(number, country || defaultCountry, defaultCallingCode, metadata)
53
+ const numberWithoutIDD = stripIddPrefix(number, country || defaultCountry, defaultCallingCode, metadataJson)
49
54
  // If an IDD prefix was stripped then
50
55
  // convert the number to international one
51
56
  // for subsequent parsing.
@@ -66,7 +71,7 @@ export default function extractCountryCallingCode(
66
71
  country,
67
72
  defaultCountry,
68
73
  defaultCallingCode,
69
- metadata
74
+ metadataJson
70
75
  )
71
76
  if (countryCallingCode) {
72
77
  return {
@@ -91,7 +96,7 @@ export default function extractCountryCallingCode(
91
96
  return {}
92
97
  }
93
98
 
94
- metadata = new Metadata(metadata)
99
+ const metadata = new Metadata(metadataJson)
95
100
 
96
101
  // The thing with country phone codes
97
102
  // is that they are orthogonal to each other
@@ -7,27 +7,37 @@ import getCountryCallingCode from '../getCountryCallingCode.js'
7
7
  /**
8
8
  * Sometimes some people incorrectly input international phone numbers
9
9
  * without the leading `+`. This function corrects such input.
10
- * @param {string} number — Phone number digits.
10
+ * @param {string} number — Phone number digits (only digits, no `+`).
11
11
  * @param {string} [country] — Exact country of the phone number.
12
12
  * @param {string} [defaultCountry]
13
13
  * @param {string} [defaultCallingCode]
14
- * @param {object} metadata
15
- * @return {object} `{ countryCallingCode: string?, number: string }`.
14
+ * @param {object} metadataJson
15
+ * @return {object} `{ countryCallingCode: string?, number: string }`, where `countryCallingCode` is the calling code that was extracted from the input `number` string, and `number` is the originally passed `number` without the extracted calling code.
16
16
  */
17
17
  export default function extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(
18
18
  number,
19
19
  country,
20
20
  defaultCountry,
21
21
  defaultCallingCode,
22
- metadata
22
+ metadataJson
23
23
  ) {
24
+ // Validate arguments.
25
+ // The `number` is known to be in a non-international form
26
+ // because there's no leading "+" character.
27
+ // Therefore, there must be either `country` or `defaultCountry` or `defaultCallingCode`.
28
+ // Otherwise, there'd be no source for the calling code to search for in the `number`.
29
+ if (!(country || defaultCountry || defaultCallingCode)) {
30
+ // There's no source for the calling code to search for in the `number`.
31
+ return { number }
32
+ }
33
+
24
34
  const countryCallingCode = country || defaultCountry
25
- ? getCountryCallingCode(country || defaultCountry, metadata)
35
+ ? getCountryCallingCode(country || defaultCountry, metadataJson)
26
36
  : defaultCallingCode
27
37
 
28
38
  if (number.indexOf(countryCallingCode) === 0) {
29
- metadata = new Metadata(metadata)
30
- metadata.selectNumberingPlan(country || defaultCountry, countryCallingCode)
39
+ const metadata = new Metadata(metadataJson)
40
+ metadata.selectNumberingPlan(country || defaultCountry || defaultCallingCode)
31
41
 
32
42
  const possibleShorterNumber = number.slice(countryCallingCode.length)
33
43
 
@@ -35,7 +45,7 @@ export default function extractCountryCallingCodeFromInternationalNumberWithoutP
35
45
  nationalNumber: possibleShorterNationalNumber,
36
46
  } = extractNationalNumber(
37
47
  possibleShorterNumber,
38
- country,
48
+ undefined,
39
49
  metadata
40
50
  )
41
51
 
@@ -43,7 +53,7 @@ export default function extractCountryCallingCodeFromInternationalNumberWithoutP
43
53
  nationalNumber
44
54
  } = extractNationalNumber(
45
55
  number,
46
- country,
56
+ undefined,
47
57
  metadata
48
58
  )
49
59
 
@@ -61,7 +71,7 @@ export default function extractCountryCallingCodeFromInternationalNumberWithoutP
61
71
  matchesEntirely(possibleShorterNationalNumber, metadata.nationalNumberPattern())
62
72
  )
63
73
  ||
64
- checkNumberLength(nationalNumber, country, metadata) === 'TOO_LONG'
74
+ checkNumberLength(nationalNumber, undefined, metadata) === 'TOO_LONG'
65
75
  ) {
66
76
  return {
67
77
  countryCallingCode,
@@ -9,8 +9,8 @@ import getCountryByCallingCode from './getCountryByCallingCode.js'
9
9
  * it won't extract national prefix if the resultant number is too short
10
10
  * to be a complete number for the selected phone numbering plan.
11
11
  * @param {string} number — Complete phone number digits.
12
- * @param {string?} country — Country, if known.
13
- * @param {Metadata} metadata — Metadata with a phone numbering plan selected.
12
+ * @param {string?} country — Specific country to use instead of the one that is pre-selected in the metadata instance.
13
+ * @param {Metadata} metadata — Metadata instance with a selected numbering plan.
14
14
  * @return {object} `{ nationalNumber: string, carrierCode: string? }`.
15
15
  */
16
16
  export default function extractNationalNumber(number, country, metadata) {
@@ -37,7 +37,8 @@ export default function extractNationalNumber(number, country, metadata) {
37
37
  return { nationalNumber: number }
38
38
  }
39
39
  // Check the national (significant) number length after extracting national prefix and carrier code.
40
- // Legacy generated metadata (before `1.0.18`) didn't support the "possible lengths" feature.
40
+ // Legacy generated metadata (before `1.0.18`) didn't support the "possible lengths" feature,
41
+ // so this `if` will only be executed with newer metadata.
41
42
  if (metadata.numberingPlan.possibleLengths()) {
42
43
  // If an exact `country` is not specified, attempt to detect it from the assumed national number.
43
44
  if (!country) {
@@ -102,6 +103,13 @@ function shouldHaveExtractedNationalPrefix(nationalNumberBefore, nationalNumberA
102
103
  return true
103
104
  }
104
105
 
106
+ /**
107
+ * Tells if a given incomplete national number is possible or not.
108
+ * @param {string} nationalNumber
109
+ * @param {string?} country — Specific country rather than the one that is pre-selected in the metadata instance.
110
+ * @param {Metadata} metadata — Metadata instance with a selected numbering plan.
111
+ * @returns {boolean}
112
+ */
105
113
  function isPossibleIncompleteNationalNumber(nationalNumber, country, metadata) {
106
114
  switch (checkNumberLength(nationalNumber, country, metadata)) {
107
115
  case 'TOO_SHORT':