libphonenumber-js 1.12.17 → 1.12.18

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 (175) hide show
  1. package/METADATA.md +2 -0
  2. package/README.md +209 -158
  3. package/build/AsYouType.js +6 -8
  4. package/build/AsYouType.js.map +1 -1
  5. package/build/AsYouType.test.js +13 -14
  6. package/build/AsYouType.test.js.map +1 -1
  7. package/build/AsYouTypeFormatter.complete.js +4 -4
  8. package/build/AsYouTypeFormatter.complete.js.map +1 -1
  9. package/build/AsYouTypeFormatter.js +4 -4
  10. package/build/AsYouTypeFormatter.js.map +1 -1
  11. package/build/AsYouTypeParser.js +42 -29
  12. package/build/AsYouTypeParser.js.map +1 -1
  13. package/build/AsYouTypeState.js +21 -17
  14. package/build/AsYouTypeState.js.map +1 -1
  15. package/build/PhoneNumber.js +2 -2
  16. package/build/PhoneNumber.js.map +1 -1
  17. package/build/findNumbers/Leniency.js +0 -1
  18. package/build/findNumbers/Leniency.js.map +1 -1
  19. package/build/format.js +4 -4
  20. package/build/format.js.map +1 -1
  21. package/build/formatPhoneNumberForMobileDialing.js +2 -2
  22. package/build/formatPhoneNumberForMobileDialing.js.map +1 -1
  23. package/build/helpers/checkNumberLength.js +39 -7
  24. package/build/helpers/checkNumberLength.js.map +1 -1
  25. package/build/helpers/checkNumberLength.test.js +51 -12
  26. package/build/helpers/checkNumberLength.test.js.map +1 -1
  27. package/build/helpers/extractCountryCallingCode.js +13 -10
  28. package/build/helpers/extractCountryCallingCode.js.map +1 -1
  29. package/build/helpers/extractCountryCallingCode.test.js +3 -3
  30. package/build/helpers/extractCountryCallingCode.test.js.map +1 -1
  31. package/build/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +10 -8
  32. package/build/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js.map +1 -1
  33. package/build/helpers/extractNationalNumber.js +15 -5
  34. package/build/helpers/extractNationalNumber.js.map +1 -1
  35. package/build/helpers/extractNationalNumber.test.js +10 -2
  36. package/build/helpers/extractNationalNumber.test.js.map +1 -1
  37. package/build/helpers/getCountryByCallingCode.js +3 -2
  38. package/build/helpers/getCountryByCallingCode.js.map +1 -1
  39. package/build/helpers/getCountryByNationalNumber.js +27 -6
  40. package/build/helpers/getCountryByNationalNumber.js.map +1 -1
  41. package/build/helpers/matchesEntirely.js +4 -4
  42. package/build/helpers/matchesEntirely.js.map +1 -1
  43. package/build/isPossible.js +4 -4
  44. package/build/isPossible.js.map +1 -1
  45. package/build/isPossible.test.js +7 -0
  46. package/build/isPossible.test.js.map +1 -1
  47. package/build/isPossiblePhoneNumber.test.js +4 -0
  48. package/build/isPossiblePhoneNumber.test.js.map +1 -1
  49. package/build/metadata.js +2 -2
  50. package/build/metadata.js.map +1 -1
  51. package/build/parse.js +9 -7
  52. package/build/parse.js.map +1 -1
  53. package/build/parseIncompletePhoneNumber.js +7 -7
  54. package/build/parseIncompletePhoneNumber.js.map +1 -1
  55. package/build/parsePhoneNumber.test.js +12 -0
  56. package/build/parsePhoneNumber.test.js.map +1 -1
  57. package/build/validatePhoneNumberLength.js +1 -1
  58. package/build/validatePhoneNumberLength.js.map +1 -1
  59. package/build-scripts/check-for-metadata-changes-and-create-pull-request.js +87 -0
  60. package/{runnable/metadata-update-and-release.js → build-scripts/check-for-metadata-changes-and-release.js} +10 -4
  61. package/{runnable/json-to-js.js → build-scripts/create-js-file-for-json-file.js} +2 -2
  62. package/{runnable/metadata-branch.js → build-scripts/create-metadata-update-branch.js} +6 -1
  63. package/{runnable/generate-country-codes.js → build-scripts/generate-country-codes-list-in-typescript.js} +1 -0
  64. package/{runnable/generate.js → build-scripts/generate-metadata-from-google-metadata.js} +8 -14
  65. package/{runnable/modules → build-scripts/helpers}/commit.js +1 -0
  66. package/{runnable/modules → build-scripts/helpers}/exec.js +1 -0
  67. package/{runnable/modules/update-metadata.js → build-scripts/helpers/update-metadata-from-google-metadata.js} +9 -7
  68. package/{runnable/download.js → build-scripts/pull-google-metadata.js} +12 -4
  69. package/bundle/libphonenumber-js.min.js +1 -1
  70. package/bundle/libphonenumber-js.min.js.map +1 -1
  71. package/bundle/libphonenumber-max.js +1 -1
  72. package/bundle/libphonenumber-max.js.map +1 -1
  73. package/bundle/libphonenumber-min.js +1 -1
  74. package/bundle/libphonenumber-min.js.map +1 -1
  75. package/bundle/libphonenumber-mobile.js +1 -1
  76. package/bundle/libphonenumber-mobile.js.map +1 -1
  77. package/core/index.d.cts +1 -1
  78. package/core/index.d.ts +1 -1
  79. package/custom.d.ts +1 -1
  80. package/es6/AsYouType.js +6 -8
  81. package/es6/AsYouType.js.map +1 -1
  82. package/es6/AsYouType.test.js +13 -14
  83. package/es6/AsYouType.test.js.map +1 -1
  84. package/es6/AsYouTypeFormatter.complete.js +4 -4
  85. package/es6/AsYouTypeFormatter.complete.js.map +1 -1
  86. package/es6/AsYouTypeFormatter.js +4 -4
  87. package/es6/AsYouTypeFormatter.js.map +1 -1
  88. package/es6/AsYouTypeParser.js +42 -29
  89. package/es6/AsYouTypeParser.js.map +1 -1
  90. package/es6/AsYouTypeState.js +21 -17
  91. package/es6/AsYouTypeState.js.map +1 -1
  92. package/es6/PhoneNumber.js +1 -2
  93. package/es6/PhoneNumber.js.map +1 -1
  94. package/es6/findNumbers/Leniency.js +0 -1
  95. package/es6/findNumbers/Leniency.js.map +1 -1
  96. package/es6/format.js +4 -4
  97. package/es6/format.js.map +1 -1
  98. package/es6/formatPhoneNumberForMobileDialing.js +2 -2
  99. package/es6/formatPhoneNumberForMobileDialing.js.map +1 -1
  100. package/es6/helpers/checkNumberLength.js +39 -7
  101. package/es6/helpers/checkNumberLength.js.map +1 -1
  102. package/es6/helpers/checkNumberLength.test.js +49 -12
  103. package/es6/helpers/checkNumberLength.test.js.map +1 -1
  104. package/es6/helpers/extractCountryCallingCode.js +13 -10
  105. package/es6/helpers/extractCountryCallingCode.js.map +1 -1
  106. package/es6/helpers/extractCountryCallingCode.test.js +3 -3
  107. package/es6/helpers/extractCountryCallingCode.test.js.map +1 -1
  108. package/es6/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +10 -8
  109. package/es6/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js.map +1 -1
  110. package/es6/helpers/extractNationalNumber.js +15 -5
  111. package/es6/helpers/extractNationalNumber.js.map +1 -1
  112. package/es6/helpers/extractNationalNumber.test.js +10 -2
  113. package/es6/helpers/extractNationalNumber.test.js.map +1 -1
  114. package/es6/helpers/getCountryByCallingCode.js +3 -2
  115. package/es6/helpers/getCountryByCallingCode.js.map +1 -1
  116. package/es6/helpers/getCountryByNationalNumber.js +28 -6
  117. package/es6/helpers/getCountryByNationalNumber.js.map +1 -1
  118. package/es6/helpers/matchesEntirely.js +4 -4
  119. package/es6/helpers/matchesEntirely.js.map +1 -1
  120. package/es6/isPossible.js +4 -4
  121. package/es6/isPossible.js.map +1 -1
  122. package/es6/isPossible.test.js +7 -0
  123. package/es6/isPossible.test.js.map +1 -1
  124. package/es6/isPossiblePhoneNumber.test.js +4 -0
  125. package/es6/isPossiblePhoneNumber.test.js.map +1 -1
  126. package/es6/metadata.js +2 -2
  127. package/es6/metadata.js.map +1 -1
  128. package/es6/parse.js +9 -7
  129. package/es6/parse.js.map +1 -1
  130. package/es6/parseIncompletePhoneNumber.js +7 -7
  131. package/es6/parseIncompletePhoneNumber.js.map +1 -1
  132. package/es6/parsePhoneNumber.test.js +12 -0
  133. package/es6/parsePhoneNumber.test.js.map +1 -1
  134. package/es6/validatePhoneNumberLength.js +1 -1
  135. package/es6/validatePhoneNumberLength.js.map +1 -1
  136. package/index.d.cts +1 -1
  137. package/index.d.ts +1 -1
  138. package/max/index.d.cts +1 -1
  139. package/max/index.d.ts +1 -1
  140. package/min/index.d.cts +1 -1
  141. package/min/index.d.ts +1 -1
  142. package/mobile/index.d.cts +1 -1
  143. package/mobile/index.d.ts +1 -1
  144. package/package.json +16 -23
  145. package/source/AsYouType.js +8 -10
  146. package/source/AsYouType.test.js +13 -14
  147. package/source/AsYouTypeFormatter.complete.js +4 -4
  148. package/source/AsYouTypeFormatter.js +4 -4
  149. package/source/AsYouTypeParser.js +45 -28
  150. package/source/AsYouTypeState.js +21 -17
  151. package/source/PhoneNumber.js +1 -2
  152. package/source/findNumbers/Leniency.js +0 -1
  153. package/source/format.js +4 -4
  154. package/source/formatPhoneNumberForMobileDialing.js +2 -2
  155. package/source/helpers/checkNumberLength.js +40 -7
  156. package/source/helpers/checkNumberLength.test.js +54 -12
  157. package/source/helpers/extractCountryCallingCode.js +15 -10
  158. package/source/helpers/extractCountryCallingCode.test.js +3 -3
  159. package/source/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +19 -8
  160. package/source/helpers/extractNationalNumber.js +15 -5
  161. package/source/helpers/extractNationalNumber.test.js +11 -2
  162. package/source/helpers/getCountryByCallingCode.js +2 -2
  163. package/source/helpers/getCountryByNationalNumber.js +27 -6
  164. package/source/helpers/matchesEntirely.js +4 -4
  165. package/source/isPossible.js +4 -4
  166. package/source/isPossible.test.js +9 -0
  167. package/source/isPossiblePhoneNumber.test.js +5 -0
  168. package/source/metadata.js +2 -2
  169. package/source/parse.js +9 -5
  170. package/source/parseIncompletePhoneNumber.js +7 -7
  171. package/source/parsePhoneNumber.test.js +14 -0
  172. package/source/validatePhoneNumberLength.js +1 -1
  173. package/runnable/metadata-update-and-push-and-pull-request.js +0 -47
  174. package/runnable/metadata-update-and-push.js +0 -30
  175. /package/{runnable/create-commonjs-package-json.js → build-scripts/create-commonjs-package-json-file.js} +0 -0
@@ -2,39 +2,81 @@ import Metadata from '../metadata.js'
2
2
  import metadata from '../../metadata.max.json' with { type: 'json' }
3
3
  import oldMetadata from '../../test/metadata/1.0.0/metadata.min.json' with { type: 'json' }
4
4
 
5
- import { checkNumberLengthForType } from './checkNumberLength.js'
5
+ import checkNumberLength, { checkNumberLengthForType } from './checkNumberLength.js'
6
6
 
7
7
  describe('checkNumberLength', () => {
8
8
  it('should check phone number length', () => {
9
9
  // Too short.
10
- expect(checkNumberLength('800555353', 'FIXED_LINE', 'RU')).to.equal('TOO_SHORT')
10
+ expect(checkNumberLengthWithCountry('800555353', 'RU', 'FIXED_LINE')).to.equal('TOO_SHORT')
11
11
  // Normal.
12
- expect(checkNumberLength('8005553535', 'FIXED_LINE', 'RU')).to.equal('IS_POSSIBLE')
12
+ expect(checkNumberLengthWithCountry('8005553535', 'RU', 'FIXED_LINE')).to.equal('IS_POSSIBLE')
13
13
  // Too long.
14
- expect(checkNumberLength('80055535355', 'FIXED_LINE', 'RU')).to.equal('TOO_LONG')
14
+ expect(checkNumberLengthWithCountry('80055535355', 'RU', 'FIXED_LINE')).to.equal('TOO_LONG')
15
15
 
16
16
  // No such type.
17
- expect(checkNumberLength('169454850', 'VOIP', 'AC')).to.equal('INVALID_LENGTH')
17
+ expect(checkNumberLengthWithCountry('169454850', 'AC', 'VOIP')).to.equal('INVALID_LENGTH')
18
18
  // No such possible length.
19
- expect(checkNumberLength('1694548', undefined, 'AD')).to.equal('INVALID_LENGTH')
19
+ expect(checkNumberLengthWithCountry('1694548', 'AD', undefined)).to.equal('INVALID_LENGTH')
20
20
 
21
21
  // FIXED_LINE_OR_MOBILE
22
- expect(checkNumberLength('1694548', 'FIXED_LINE_OR_MOBILE', 'AD')).to.equal('INVALID_LENGTH')
22
+ expect(checkNumberLengthWithCountry('1694548', 'AD', 'FIXED_LINE_OR_MOBILE')).to.equal('INVALID_LENGTH')
23
23
  // No mobile phones.
24
- expect(checkNumberLength('8123', 'FIXED_LINE_OR_MOBILE', 'TA')).to.equal('IS_POSSIBLE')
24
+ expect(checkNumberLengthWithCountry('8123', 'TA', 'FIXED_LINE_OR_MOBILE')).to.equal('IS_POSSIBLE')
25
25
  // No "possible lengths" for "mobile".
26
- expect(checkNumberLength('81234567', 'FIXED_LINE_OR_MOBILE', 'SZ')).to.equal('IS_POSSIBLE')
26
+ expect(checkNumberLengthWithCountry('81234567', 'SZ', 'FIXED_LINE_OR_MOBILE')).to.equal('IS_POSSIBLE')
27
+ })
28
+
29
+ it('should check phone number length (no `country` specified)', () => {
30
+ // Too short.
31
+ expect(checkNumberLengthWithoutCountry('800555353', 'RU', 'FIXED_LINE')).to.equal('TOO_SHORT')
32
+ // Normal.
33
+ expect(checkNumberLengthWithoutCountry('8005553535', 'RU', 'FIXED_LINE')).to.equal('IS_POSSIBLE')
34
+ // Too long.
35
+ expect(checkNumberLengthWithoutCountry('80055535355', 'RU', 'FIXED_LINE')).to.equal('TOO_LONG')
36
+
37
+ // No such type.
38
+ expect(checkNumberLengthWithoutCountry('169454850', 'AC', 'VOIP')).to.equal('INVALID_LENGTH')
39
+ // No such possible length.
40
+ expect(checkNumberLengthWithoutCountry('1694548', 'AD', undefined)).to.equal('INVALID_LENGTH')
41
+
42
+ // FIXED_LINE_OR_MOBILE
43
+ expect(checkNumberLengthWithoutCountry('1694548', 'AD', 'FIXED_LINE_OR_MOBILE')).to.equal('INVALID_LENGTH')
44
+ // No mobile phones.
45
+ expect(checkNumberLengthWithoutCountry('8123', 'TA', 'FIXED_LINE_OR_MOBILE')).to.equal('IS_POSSIBLE')
46
+ // No "possible lengths" for "mobile".
47
+ expect(checkNumberLengthWithoutCountry('81234567', 'SZ', 'FIXED_LINE_OR_MOBILE')).to.equal('IS_POSSIBLE')
27
48
  })
28
49
 
29
50
  it('should work for old metadata', function() {
30
51
  const _oldMetadata = new Metadata(oldMetadata)
31
52
  _oldMetadata.country('RU')
32
- expect(checkNumberLengthForType('8005553535', 'FIXED_LINE', _oldMetadata)).to.equal('IS_POSSIBLE')
53
+ expect(checkNumberLengthForType('8005553535', 'RU', 'FIXED_LINE', _oldMetadata)).to.equal('IS_POSSIBLE')
54
+ })
55
+
56
+ it('should work for old metadata (no `country` specified)', function() {
57
+ const _oldMetadata = new Metadata(oldMetadata)
58
+ _oldMetadata.country('RU')
59
+ expect(checkNumberLengthForType('8005553535', undefined, 'FIXED_LINE', _oldMetadata)).to.equal('IS_POSSIBLE')
60
+ })
61
+
62
+ it('should handle the cases when multiple countries share the same country calling code and a phone number is possible in non-"main" country and is not possible in the "main" country', () => {
63
+ const _metadata = new Metadata(metadata)
64
+ _metadata.country('US')
65
+
66
+ expect(checkNumberLength('3100000', undefined, _metadata)).to.equal('TOO_SHORT');
67
+ expect(checkNumberLength('3100000', 'US', _metadata)).to.equal('TOO_SHORT');
68
+ expect(checkNumberLength('3100000', 'CA', _metadata)).to.equal('IS_POSSIBLE');
33
69
  })
34
70
  })
35
71
 
36
- function checkNumberLength(number, type, country) {
72
+ function checkNumberLengthWithCountry(number, country, type) {
73
+ const _metadata = new Metadata(metadata)
74
+ _metadata.country(country)
75
+ return checkNumberLengthForType(number, country, type, _metadata)
76
+ }
77
+
78
+ function checkNumberLengthWithoutCountry(number, country, type) {
37
79
  const _metadata = new Metadata(metadata)
38
80
  _metadata.country(country)
39
- return checkNumberLengthForType(number, type, _metadata)
81
+ return checkNumberLengthForType(number, undefined, type, _metadata)
40
82
  }
@@ -10,21 +10,23 @@ import { MAX_LENGTH_COUNTRY_CODE } from '../constants.js'
10
10
  * a national prefix, carrier code, and national
11
11
  * (significant) number.
12
12
  * @param {string} number — Phone number digits (possibly with a `+`).
13
- * @param {string} [country] — Default country.
14
- * @param {string} [callingCode] — Default calling code (some phone numbering plans are non-geographic).
13
+ * @param {string} [country] — Country.
14
+ * @param {string} [defaultCountry] — Default country.
15
+ * @param {string} [defaultCallingCode] — Default calling code (some phone numbering plans are non-geographic).
15
16
  * @param {object} metadata
16
17
  * @return {object} `{ countryCallingCodeSource: string?, countryCallingCode: string?, number: string }`
17
18
  * @example
18
19
  * // Returns `{ countryCallingCode: "1", number: "2133734253" }`.
19
- * extractCountryCallingCode('2133734253', 'US', null, metadata)
20
- * extractCountryCallingCode('2133734253', null, '1', metadata)
21
- * extractCountryCallingCode('+12133734253', null, null, metadata)
22
- * extractCountryCallingCode('+12133734253', 'RU', null, metadata)
20
+ * extractCountryCallingCode('2133734253', null, 'US', null, metadata)
21
+ * extractCountryCallingCode('2133734253', null, null, '1', metadata)
22
+ * extractCountryCallingCode('+12133734253', null, null, null, metadata)
23
+ * extractCountryCallingCode('+12133734253', null, 'RU', null, metadata)
23
24
  */
24
25
  export default function extractCountryCallingCode(
25
26
  number,
26
27
  country,
27
- callingCode,
28
+ defaultCountry,
29
+ defaultCallingCode,
28
30
  metadata
29
31
  ) {
30
32
  if (!number) {
@@ -43,7 +45,7 @@ export default function extractCountryCallingCode(
43
45
  if (number[0] !== '+') {
44
46
  // Convert an "out-of-country" dialing phone number
45
47
  // to a proper international phone number.
46
- const numberWithoutIDD = stripIddPrefix(number, country, callingCode, metadata)
48
+ const numberWithoutIDD = stripIddPrefix(number, country || defaultCountry, defaultCallingCode, metadata)
47
49
  // If an IDD prefix was stripped then
48
50
  // convert the number to international one
49
51
  // for subsequent parsing.
@@ -55,14 +57,15 @@ export default function extractCountryCallingCode(
55
57
  // for the default country. If so, we remove the country calling code,
56
58
  // and do some checks on the validity of the number before and after.
57
59
  // https://github.com/catamphetamine/libphonenumber-js/issues/376
58
- if (country || callingCode) {
60
+ if (country || defaultCountry || defaultCallingCode) {
59
61
  const {
60
62
  countryCallingCode,
61
63
  number: shorterNumber
62
64
  } = extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(
63
65
  number,
64
66
  country,
65
- callingCode,
67
+ defaultCountry,
68
+ defaultCallingCode,
66
69
  metadata
67
70
  )
68
71
  if (countryCallingCode) {
@@ -81,6 +84,8 @@ export default function extractCountryCallingCode(
81
84
  }
82
85
  }
83
86
 
87
+ // `number` can only be international at this point.
88
+
84
89
  // Fast abortion: country codes do not begin with a '0'
85
90
  if (number[1] === '0') {
86
91
  return {}
@@ -3,18 +3,18 @@ import metadata from '../../metadata.min.json' with { type: 'json' }
3
3
 
4
4
  describe('extractCountryCallingCode', () => {
5
5
  it('should extract country calling code from a number', () => {
6
- expect(extractCountryCallingCode('+78005553535', null, null, metadata)).to.deep.equal({
6
+ expect(extractCountryCallingCode('+78005553535', null, null, null, metadata)).to.deep.equal({
7
7
  countryCallingCodeSource: 'FROM_NUMBER_WITH_PLUS_SIGN',
8
8
  countryCallingCode: '7',
9
9
  number: '8005553535'
10
10
  })
11
11
 
12
- expect(extractCountryCallingCode('+7800', null, null, metadata)).to.deep.equal({
12
+ expect(extractCountryCallingCode('+7800', null, null, null, metadata)).to.deep.equal({
13
13
  countryCallingCodeSource: 'FROM_NUMBER_WITH_PLUS_SIGN',
14
14
  countryCallingCode: '7',
15
15
  number: '800'
16
16
  })
17
17
 
18
- expect(extractCountryCallingCode('', null, null, metadata)).to.deep.equal({})
18
+ expect(extractCountryCallingCode('', null, null, null, metadata)).to.deep.equal({})
19
19
  })
20
20
  })
@@ -8,34 +8,45 @@ import getCountryCallingCode from '../getCountryCallingCode.js'
8
8
  * Sometimes some people incorrectly input international phone numbers
9
9
  * without the leading `+`. This function corrects such input.
10
10
  * @param {string} number — Phone number digits.
11
- * @param {string?} country
12
- * @param {string?} callingCode
11
+ * @param {string} [country] — Exact country of the phone number.
12
+ * @param {string} [defaultCountry]
13
+ * @param {string} [defaultCallingCode]
13
14
  * @param {object} metadata
14
15
  * @return {object} `{ countryCallingCode: string?, number: string }`.
15
16
  */
16
17
  export default function extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(
17
18
  number,
18
19
  country,
19
- callingCode,
20
+ defaultCountry,
21
+ defaultCallingCode,
20
22
  metadata
21
23
  ) {
22
- const countryCallingCode = country ? getCountryCallingCode(country, metadata) : callingCode
24
+ const countryCallingCode = country || defaultCountry
25
+ ? getCountryCallingCode(country || defaultCountry, metadata)
26
+ : defaultCallingCode
27
+
23
28
  if (number.indexOf(countryCallingCode) === 0) {
24
29
  metadata = new Metadata(metadata)
25
- metadata.selectNumberingPlan(country, callingCode)
30
+ metadata.selectNumberingPlan(country || defaultCountry, countryCallingCode)
31
+
26
32
  const possibleShorterNumber = number.slice(countryCallingCode.length)
33
+
27
34
  const {
28
35
  nationalNumber: possibleShorterNationalNumber,
29
36
  } = extractNationalNumber(
30
37
  possibleShorterNumber,
38
+ country,
31
39
  metadata
32
40
  )
41
+
33
42
  const {
34
43
  nationalNumber
35
44
  } = extractNationalNumber(
36
45
  number,
46
+ country,
37
47
  metadata
38
48
  )
49
+
39
50
  // If the number was not valid before but is valid now,
40
51
  // or if it was too long before, we consider the number
41
52
  // with the country calling code stripped to be a better result
@@ -46,12 +57,11 @@ export default function extractCountryCallingCodeFromInternationalNumberWithoutP
46
57
  // a leading `+`.
47
58
  if (
48
59
  (
49
- !matchesEntirely(nationalNumber, metadata.nationalNumberPattern())
50
- &&
60
+ !matchesEntirely(nationalNumber, metadata.nationalNumberPattern()) &&
51
61
  matchesEntirely(possibleShorterNationalNumber, metadata.nationalNumberPattern())
52
62
  )
53
63
  ||
54
- checkNumberLength(nationalNumber, metadata) === 'TOO_LONG'
64
+ checkNumberLength(nationalNumber, country, metadata) === 'TOO_LONG'
55
65
  ) {
56
66
  return {
57
67
  countryCallingCode,
@@ -59,5 +69,6 @@ export default function extractCountryCallingCodeFromInternationalNumberWithoutP
59
69
  }
60
70
  }
61
71
  }
72
+
62
73
  return { number }
63
74
  }
@@ -1,6 +1,7 @@
1
1
  import extractNationalNumberFromPossiblyIncompleteNumber from './extractNationalNumberFromPossiblyIncompleteNumber.js'
2
2
  import matchesEntirely from './matchesEntirely.js'
3
3
  import checkNumberLength from './checkNumberLength.js'
4
+ import getCountryByCallingCode from './getCountryByCallingCode.js'
4
5
 
5
6
  /**
6
7
  * Strips national prefix and carrier code from a complete phone number.
@@ -8,10 +9,11 @@ import checkNumberLength from './checkNumberLength.js'
8
9
  * it won't extract national prefix if the resultant number is too short
9
10
  * to be a complete number for the selected phone numbering plan.
10
11
  * @param {string} number — Complete phone number digits.
12
+ * @param {string?} country — Country, if known.
11
13
  * @param {Metadata} metadata — Metadata with a phone numbering plan selected.
12
14
  * @return {object} `{ nationalNumber: string, carrierCode: string? }`.
13
15
  */
14
- export default function extractNationalNumber(number, metadata) {
16
+ export default function extractNationalNumber(number, country, metadata) {
15
17
  // Parsing national prefixes and carrier codes
16
18
  // is only required for local phone numbers
17
19
  // but some people don't understand that
@@ -36,7 +38,15 @@ export default function extractNationalNumber(number, metadata) {
36
38
  }
37
39
  // Check the national (significant) number length after extracting national prefix and carrier code.
38
40
  // Legacy generated metadata (before `1.0.18`) didn't support the "possible lengths" feature.
39
- if (metadata.possibleLengths()) {
41
+ if (metadata.numberingPlan.possibleLengths()) {
42
+ // If an exact `country` is not specified, attempt to detect it from the assumed national number.
43
+ if (!country) {
44
+ country = getCountryByCallingCode(metadata.numberingPlan.callingCode(), {
45
+ nationalNumber,
46
+ metadata
47
+ })
48
+ }
49
+
40
50
  // The number remaining after stripping the national prefix and carrier code
41
51
  // should be long enough to have a possible length for the country.
42
52
  // Otherwise, don't strip the national prefix and carrier code,
@@ -45,7 +55,7 @@ export default function extractNationalNumber(number, metadata) {
45
55
  // https://github.com/google/libphonenumber/blob/876268eb1ad6cdc1b7b5bef17fc5e43052702d57/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L3236-L3250
46
56
  // It doesn't check for the "possibility" of the original `number`.
47
57
  // I guess it's fine not checking that one. It works as is anyway.
48
- if (!isPossibleIncompleteNationalNumber(nationalNumber, metadata)) {
58
+ if (!isPossibleIncompleteNationalNumber(nationalNumber, country, metadata)) {
49
59
  // Don't strip the national prefix.
50
60
  return { nationalNumber: number }
51
61
  }
@@ -92,8 +102,8 @@ function shouldHaveExtractedNationalPrefix(nationalNumberBefore, nationalNumberA
92
102
  return true
93
103
  }
94
104
 
95
- function isPossibleIncompleteNationalNumber(nationalNumber, metadata) {
96
- switch (checkNumberLength(nationalNumber, metadata)) {
105
+ function isPossibleIncompleteNationalNumber(nationalNumber, country, metadata) {
106
+ switch (checkNumberLength(nationalNumber, country, metadata)) {
97
107
  case 'TOO_SHORT':
98
108
  case 'INVALID_LENGTH':
99
109
  // This library ignores "local-only" phone numbers (for simplicity).
@@ -4,10 +4,19 @@ import Metadata from '../metadata.js'
4
4
  import oldMetadata from '../../test/metadata/1.0.0/metadata.min.json' with { type: 'json' }
5
5
 
6
6
  describe('extractNationalNumber', function() {
7
- it('should extract a national number when using old metadata', function() {
7
+ it('should extract a national number when using old metadata (no `country` or `defaultCountry` specified)', function() {
8
8
  const _oldMetadata = new Metadata(oldMetadata)
9
9
  _oldMetadata.selectNumberingPlan('RU')
10
- expect(extractNationalNumber('88005553535', _oldMetadata)).to.deep.equal({
10
+ expect(extractNationalNumber('88005553535', undefined, _oldMetadata)).to.deep.equal({
11
+ nationalNumber: '8005553535',
12
+ carrierCode: undefined
13
+ })
14
+ })
15
+
16
+ it('should extract a national number when using old metadata (`country` is specified)', function() {
17
+ const _oldMetadata = new Metadata(oldMetadata)
18
+ _oldMetadata.selectNumberingPlan('RU')
19
+ expect(extractNationalNumber('88005553535', 'RU', _oldMetadata)).to.deep.equal({
11
20
  nationalNumber: '8005553535',
12
21
  carrierCode: undefined
13
22
  })
@@ -2,9 +2,10 @@ import getCountryByNationalNumber from './getCountryByNationalNumber.js'
2
2
 
3
3
  const USE_NON_GEOGRAPHIC_COUNTRY_CODE = false
4
4
 
5
+ // Returns the exact country for the `nationalNumber`
6
+ // that belongs to the specified "country calling code".
5
7
  export default function getCountryByCallingCode(callingCode, {
6
8
  nationalNumber: nationalPhoneNumber,
7
- defaultCountry,
8
9
  metadata
9
10
  }) {
10
11
  /* istanbul ignore if */
@@ -24,7 +25,6 @@ export default function getCountryByCallingCode(callingCode, {
24
25
  }
25
26
  return getCountryByNationalNumber(nationalPhoneNumber, {
26
27
  countries: possibleCountries,
27
- defaultCountry,
28
28
  metadata: metadata.metadata
29
29
  })
30
30
  }
@@ -1,9 +1,10 @@
1
1
  import Metadata from '../metadata.js'
2
2
  import getNumberType from './getNumberType.js'
3
3
 
4
+ // Returns the exact country that the `nationalPhoneNumber` belongs to
5
+ // in cases of ambiguity, i.e. when multiple countries share the same "country calling code".
4
6
  export default function getCountryByNationalNumber(nationalPhoneNumber, {
5
7
  countries,
6
- defaultCountry,
7
8
  metadata
8
9
  }) {
9
10
  // Re-create `metadata` because it will be selecting a `country`.
@@ -12,7 +13,7 @@ export default function getCountryByNationalNumber(nationalPhoneNumber, {
12
13
  // const matchingCountries = []
13
14
 
14
15
  for (const country of countries) {
15
- metadata.country(country)
16
+ metadata.selectNumberingPlan(country)
16
17
  // "Leading digits" patterns are only defined for about 20% of all countries.
17
18
  // By definition, matching "leading digits" is a sufficient but not a necessary
18
19
  // condition for a phone number to belong to a country.
@@ -29,16 +30,36 @@ export default function getCountryByNationalNumber(nationalPhoneNumber, {
29
30
  // Else perform full validation with all of those
30
31
  // fixed-line/mobile/etc regular expressions.
31
32
  else if (getNumberType({ phone: nationalPhoneNumber, country }, undefined, metadata.metadata)) {
32
- // If both the `defaultCountry` and the "main" one match the phone number,
33
- // don't prefer the `defaultCountry` over the "main" one.
33
+ // When multiple countries share the same "country calling code",
34
+ // type patterns aren't guaranteed to be unique among them.
35
+ // For example, both `US` and `CA` have the same pattern for `toll_free` numbers.
36
+ // https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/103#note_1417147572
37
+ //
38
+ // That means that this `if` condition could be `true` for multiple countries from the list.
39
+ // Currently, it just returns the first one, which is also the "main" country for the "country calling code".
40
+ // In an example with `toll_free` numbers above, `"US"` would be returned even though
41
+ // it could as well be `"CA"`.
42
+ //
43
+ // There was also a time when this attempted to be overly smart
44
+ // and kept track of all such multiple matching countries
45
+ // and then picked the one that matched the `defaultCountry`, if provided.
46
+ // For example, with `toll_free` numbers above, and with `defaultCountry: "CA"`,
47
+ // it would've returned `"CA"` instead of `"US"`.
48
+ // Later it turned out that such "overly smart" behavior turned out to be just confusing,
49
+ // so this "overly smart" country detection was reverted to returning the "main" country
50
+ // for the "country calling code".
34
51
  // https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/154
52
+ //
35
53
  return country
36
- // // If the `defaultCountry` is among the `matchingCountries` then return it.
54
+ //
55
+ // The "overly smart" behavior code:
56
+ //
37
57
  // if (defaultCountry) {
38
58
  // if (country === defaultCountry) {
39
59
  // return country
60
+ // } else {
61
+ // matchingCountries.push(country)
40
62
  // }
41
- // matchingCountries.push(country)
42
63
  // } else {
43
64
  // return country
44
65
  // }
@@ -3,9 +3,9 @@
3
3
  * against the regular expression.
4
4
  * @return {boolean}
5
5
  */
6
- export default function matchesEntirely(text, regular_expression) {
7
- // If assigning the `''` default value is moved to the arguments above,
8
- // code coverage would decrease for some weird reason.
6
+ export default function matchesEntirely(text, regularExpressionText) {
7
+ // If the assigning of the `''` default value is moved to the arguments above,
8
+ // the code coverage would decrease for some weird reason.
9
9
  text = text || ''
10
- return new RegExp('^(?:' + regular_expression + ')$').test(text)
10
+ return new RegExp('^(?:' + regularExpressionText + ')$').test(text)
11
11
  }
@@ -35,7 +35,7 @@ export default function isPossiblePhoneNumber(input, options, metadata) {
35
35
  if (!metadata.hasCountry(input.country)) {
36
36
  throw new Error(`Unknown country: ${input.country}`)
37
37
  }
38
- metadata.country(input.country)
38
+ metadata.selectNumberingPlan(input.country)
39
39
  } else {
40
40
  if (!input.countryCallingCode) {
41
41
  throw new Error('Invalid phone number object passed')
@@ -46,7 +46,7 @@ export default function isPossiblePhoneNumber(input, options, metadata) {
46
46
 
47
47
  // Old metadata (< 1.0.18) had no "possible length" data.
48
48
  if (metadata.possibleLengths()) {
49
- return isPossibleNumber(input.phone || input.nationalNumber, metadata)
49
+ return isPossibleNumber(input.phone || input.nationalNumber, input.country, metadata)
50
50
  } else {
51
51
  // There was a bug between `1.7.35` and `1.7.37` where "possible_lengths"
52
52
  // were missing for "non-geographical" numbering plans.
@@ -64,8 +64,8 @@ export default function isPossiblePhoneNumber(input, options, metadata) {
64
64
  }
65
65
  }
66
66
 
67
- export function isPossibleNumber(nationalNumber, metadata) { //, isInternational) {
68
- switch (checkNumberLength(nationalNumber, metadata)) {
67
+ export function isPossibleNumber(nationalNumber, country, metadata) { //, isInternational) {
68
+ switch (checkNumberLength(nationalNumber, country, metadata)) {
69
69
  case 'IS_POSSIBLE':
70
70
  return true
71
71
  // This library ignores "local-only" phone numbers (for simplicity).
@@ -69,4 +69,13 @@ describe('isPossible', () => {
69
69
  // Incorrect country.
70
70
  expect(() => isPossibleNumber({ phone: '1112223344', country: 'XX' })).to.throw('Unknown country')
71
71
  })
72
+
73
+
74
+ it('should handle the cases when multiple countries share the same country calling code and a phone number is possible in non-"main" country and is not possible in the "main" country', () => {
75
+ const phoneNumber = parsePhoneNumber('+13100000', undefined, metadata)
76
+ expect(phoneNumber.country).to.equal('CA')
77
+ expect(phoneNumber.isPossible()).to.equal(true)
78
+ expect(phoneNumber.isValid()).to.equal(true)
79
+ expect(phoneNumber.nationalNumber).to.equal('3100000')
80
+ })
72
81
  })
@@ -23,4 +23,9 @@ describe('isPossiblePhoneNumber', () => {
23
23
  expect(() => _isPossiblePhoneNumber('8 (800) 555 35 35', 'RU', oldMetadata)).to.throw('Missing "possibleLengths" in metadata.')
24
24
  expect(_isPossiblePhoneNumber('+888 123 456 78901', oldMetadata)).to.equal(true)
25
25
  })
26
+
27
+ it('should handle the cases when multiple countries share the same country calling code and a phone number is possible in non-"main" country and is not possible in the "main" country', () => {
28
+ expect(isPossiblePhoneNumber('+13100000', 'CA')).to.equal(true)
29
+ expect(isPossiblePhoneNumber('3100000', 'CA')).to.equal(true)
30
+ })
26
31
  })
@@ -482,7 +482,7 @@ const typeOf = _ => typeof _
482
482
  export function getExtPrefix(country, metadata) {
483
483
  metadata = new Metadata(metadata)
484
484
  if (metadata.hasCountry(country)) {
485
- return metadata.country(country).ext()
485
+ return metadata.selectNumberingPlan(country).ext()
486
486
  }
487
487
  return DEFAULT_EXT_PREFIX
488
488
  }
@@ -500,7 +500,7 @@ export function getExtPrefix(country, metadata) {
500
500
  export function getCountryCallingCode(country, metadata) {
501
501
  metadata = new Metadata(metadata)
502
502
  if (metadata.hasCountry(country)) {
503
- return metadata.country(country).countryCallingCode()
503
+ return metadata.selectNumberingPlan(country).countryCallingCode()
504
504
  }
505
505
  throw new Error(`Unknown country: ${country}`)
506
506
  }
package/source/parse.js CHANGED
@@ -181,7 +181,7 @@ export default function parse(text, options, metadata) {
181
181
  possible: valid ? true : (
182
182
  options.extended === true &&
183
183
  metadata.possibleLengths() &&
184
- isPossibleNumber(nationalNumber, metadata) ? true : false
184
+ isPossibleNumber(nationalNumber, country, metadata) ? true : false
185
185
  ),
186
186
  phone: nationalNumber,
187
187
  ext
@@ -290,17 +290,21 @@ function parsePhoneNumber(
290
290
  // Extract calling code from phone number.
291
291
  let { countryCallingCodeSource, countryCallingCode, number } = extractCountryCallingCode(
292
292
  parseIncompletePhoneNumber(formattedPhoneNumber),
293
+ undefined,
293
294
  defaultCountry,
294
295
  defaultCallingCode,
295
296
  metadata.metadata
296
297
  )
297
298
 
298
- // Choose a country by `countryCallingCode`.
299
+ // The exact country of the phone number
299
300
  let country
301
+
302
+ // If `formattedPhoneNumber` is passed in "international" format,
303
+ // choose a country by `countryCallingCode`.
300
304
  if (countryCallingCode) {
301
305
  metadata.selectNumberingPlan(countryCallingCode)
302
306
  }
303
- // If `formattedPhoneNumber` is passed in "national" format
307
+ // Else, if `formattedPhoneNumber` is passed in "national" format,
304
308
  // then `number` is defined and `countryCallingCode` is `undefined`.
305
309
  else if (number && (defaultCountry || defaultCallingCode)) {
306
310
  metadata.selectNumberingPlan(defaultCountry, defaultCallingCode)
@@ -330,6 +334,7 @@ function parsePhoneNumber(
330
334
  carrierCode
331
335
  } = extractNationalNumber(
332
336
  parseIncompletePhoneNumber(number),
337
+ country,
333
338
  metadata
334
339
  )
335
340
 
@@ -345,7 +350,6 @@ function parsePhoneNumber(
345
350
  //
346
351
  const exactCountry = getCountryByCallingCode(countryCallingCode, {
347
352
  nationalNumber,
348
- defaultCountry,
349
353
  metadata
350
354
  })
351
355
  if (exactCountry) {
@@ -356,7 +360,7 @@ function parsePhoneNumber(
356
360
  // If `USE_NON_GEOGRAPHIC_COUNTRY_CODE` is set to `true` for some reason,
357
361
  // then remove the "istanbul ignore if".
358
362
  } else {
359
- metadata.country(country)
363
+ metadata.selectNumberingPlan(country)
360
364
  }
361
365
  }
362
366
 
@@ -37,28 +37,28 @@ export default function parseIncompletePhoneNumber(string) {
37
37
  * [`input-format`](https://gitlab.com/catamphetamine/input-format).
38
38
  * @param {string} character - Yet another character from raw input string.
39
39
  * @param {string?} prevParsedCharacters - Previous parsed characters.
40
- * @param {function?} emitEvent - An optional "emit event" function.
40
+ * @param {function?} eventListener - An optional "on event" function.
41
41
  * @return {string?} The parsed character.
42
42
  */
43
- export function parsePhoneNumberCharacter(character, prevParsedCharacters, emitEvent) {
43
+ export function parsePhoneNumberCharacter(character, prevParsedCharacters, eventListener) {
44
44
  // Only allow a leading `+`.
45
45
  if (character === '+') {
46
46
  // If this `+` is not the first parsed character
47
47
  // then discard it.
48
48
  if (prevParsedCharacters) {
49
- // `emitEvent` argument was added to this `export`ed function on Dec 26th, 2023.
49
+ // `eventListener` argument was added to this `export`ed function on Dec 26th, 2023.
50
50
  // Any 3rd-party code that used to `import` and call this function before that
51
- // won't be passing any `emitEvent` argument.
51
+ // won't be passing any `eventListener` argument.
52
52
  //
53
- // The addition of the `emitEvent` argument was to fix the slightly-weird behavior
53
+ // The addition of the `eventListener` argument was to fix the slightly-weird behavior
54
54
  // of parsing an input string when the user inputs something like `"2+7"
55
55
  // https://github.com/catamphetamine/react-phone-number-input/issues/437
56
56
  //
57
57
  // If the parser encounters an unexpected `+` in a string being parsed
58
58
  // then it simply discards that out-of-place `+` and any following characters.
59
59
  //
60
- if (typeof emitEvent === 'function') {
61
- emitEvent('end')
60
+ if (typeof eventListener === 'function') {
61
+ eventListener('end')
62
62
  }
63
63
  return
64
64
  }
@@ -77,4 +77,18 @@ describe('parsePhoneNumber', () => {
77
77
  expect(phoneNumber.isValid()).to.equal(false)
78
78
  expect(phoneNumber.nationalNumber).to.equal('123456789')
79
79
  })
80
+
81
+ it('should handle the cases when multiple countries share the same country calling code and a phone number is possible in non-"main" country and is not possible in the "main" country', () => {
82
+ const phoneNumber = parsePhoneNumber('+13100000')
83
+ expect(phoneNumber.country).to.equal('CA')
84
+ expect(phoneNumber.isPossible()).to.equal(true)
85
+ expect(phoneNumber.isValid()).to.equal(true)
86
+ expect(phoneNumber.nationalNumber).to.equal('3100000')
87
+
88
+ const phoneNumberLocal = parsePhoneNumber('3100000', 'CA')
89
+ expect(phoneNumberLocal.country).to.equal('CA')
90
+ expect(phoneNumberLocal.isPossible()).to.equal(true)
91
+ expect(phoneNumberLocal.isValid()).to.equal(true)
92
+ expect(phoneNumberLocal.nationalNumber).to.equal('3100000')
93
+ })
80
94
  })
@@ -16,7 +16,7 @@ export default function validatePhoneNumberLength() {
16
16
  const phoneNumber = parsePhoneNumberWithError(text, options, metadata)
17
17
  metadata = new Metadata(metadata)
18
18
  metadata.selectNumberingPlan(phoneNumber.countryCallingCode)
19
- const result = checkNumberLength(phoneNumber.nationalNumber, metadata)
19
+ const result = checkNumberLength(phoneNumber.nationalNumber, phoneNumber.country, metadata)
20
20
  if (result !== 'IS_POSSIBLE') {
21
21
  return result
22
22
  }