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,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':
@@ -0,0 +1,31 @@
1
+ import Metadata from '../metadata.js'
2
+ import isCountryCode from './isCountryCode.js'
3
+
4
+ const USE_NON_GEOGRAPHIC_COUNTRY_CODE = false
5
+
6
+ export default function getCountryAndCallingCodeFromOneOfThem(countryOrCallingCode, metadataJson) {
7
+ let country
8
+ let callingCode
9
+
10
+ const metadata = new Metadata(metadataJson)
11
+ // If country code is passed then derive `countryCallingCode` from it.
12
+ // Also store the country code as `.country`.
13
+ if (isCountryCode(countryOrCallingCode)) {
14
+ country = countryOrCallingCode
15
+ metadata.selectNumberingPlan(country)
16
+ callingCode = metadata.countryCallingCode()
17
+ } else {
18
+ callingCode = countryOrCallingCode
19
+ /* istanbul ignore if */
20
+ if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
21
+ if (metadata.isNonGeographicCallingCode(callingCode)) {
22
+ country = '001'
23
+ }
24
+ }
25
+ }
26
+
27
+ return {
28
+ country,
29
+ callingCode
30
+ }
31
+ }
@@ -2,10 +2,16 @@ 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
+ /**
6
+ * Returns the exact country that a given national (significant) number belongs to
7
+ * in case of ambiguity, i.e. when multiple countries share the same "country calling code".
8
+ * @param {string} [callingCode]
9
+ * @param {string} [options.nationalNumber] — National (significant) number.
10
+ * @param {Metadata} options.metadata — Metadata instance.
11
+ * @returns {string?} Returns the most suitable country for this calling code and national (significant) number.
12
+ */
7
13
  export default function getCountryByCallingCode(callingCode, {
8
- nationalNumber: nationalPhoneNumber,
14
+ nationalNumber,
9
15
  metadata
10
16
  }) {
11
17
  /* istanbul ignore if */
@@ -23,8 +29,5 @@ export default function getCountryByCallingCode(callingCode, {
23
29
  if (possibleCountries.length === 1) {
24
30
  return possibleCountries[0]
25
31
  }
26
- return getCountryByNationalNumber(nationalPhoneNumber, {
27
- countries: possibleCountries,
28
- metadata: metadata.metadata
29
- })
32
+ return getCountryByNationalNumber(nationalNumber, possibleCountries, metadata.metadata)
30
33
  }
@@ -1,14 +1,17 @@
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".
6
- export default function getCountryByNationalNumber(nationalPhoneNumber, {
7
- countries,
8
- metadata
9
- }) {
10
- // Re-create `metadata` because it will be selecting a `country`.
11
- metadata = new Metadata(metadata)
4
+ /**
5
+ * Returns the exact country that a given national (significant) number belongs to
6
+ * in case of ambiguity, i.e. when multiple countries share the same "country calling code".
7
+ * @param {string} [nationalNumber]
8
+ * @param {string[]} countries — A list of countries that share the same calling code.
9
+ * @param {object} metadata — Metadata JSON.
10
+ * @returns {string?} Returns the exact country this number belongs to, or `undefined` if the exact country couldn't be determined.
11
+ */
12
+ export default function getCountryByNationalNumber(nationalNumber, countries, metadataJson) {
13
+ // Create a new `metadata` instance because it will be selecting a `country`.
14
+ const metadata = new Metadata(metadataJson)
12
15
 
13
16
  // const matchingCountries = []
14
17
 
@@ -22,14 +25,14 @@ export default function getCountryByNationalNumber(nationalPhoneNumber, {
22
25
  // I'd suppose that "leading digits" patterns are mutually exclusive for different countries
23
26
  // because of the intended use of that feature.
24
27
  if (metadata.leadingDigits()) {
25
- if (nationalPhoneNumber &&
26
- nationalPhoneNumber.search(metadata.leadingDigits()) === 0) {
28
+ if (nationalNumber &&
29
+ nationalNumber.search(metadata.leadingDigits()) === 0) {
27
30
  return country
28
31
  }
29
32
  }
30
33
  // Else perform full validation with all of those
31
34
  // fixed-line/mobile/etc regular expressions.
32
- else if (getNumberType({ phone: nationalPhoneNumber, country }, undefined, metadata.metadata)) {
35
+ else if (getNumberType({ phone: nationalNumber, country }, undefined, metadata.metadata)) {
33
36
  // When multiple countries share the same "country calling code",
34
37
  // type patterns aren't guaranteed to be unique among them.
35
38
  // For example, both `US` and `CA` have the same pattern for `toll_free` numbers.
@@ -15,7 +15,7 @@ const SINGLE_IDD_PREFIX_REG_EXP = /^[\d]+(?:[~\u2053\u223C\uFF5E][\d]+)?$/
15
15
  // If the `country` doesn't support IDD calling, it returns `undefined`.
16
16
  export default function getIddPrefix(country, callingCode, metadata) {
17
17
  const countryMetadata = new Metadata(metadata)
18
- countryMetadata.selectNumberingPlan(country, callingCode)
18
+ countryMetadata.selectNumberingPlan(country || callingCode)
19
19
  if (countryMetadata.defaultIDDPrefix()) {
20
20
  return countryMetadata.defaultIDDPrefix()
21
21
  }
@@ -14,7 +14,7 @@ const NON_FIXED_LINE_PHONE_TYPES = [
14
14
  ]
15
15
 
16
16
  // Finds out national phone number type (fixed line, mobile, etc)
17
- export default function getNumberType(input, options, metadata)
17
+ export default function getNumberType(input, options, metadataJson)
18
18
  {
19
19
  // If assigning the `{}` default value is moved to the arguments above,
20
20
  // code coverage would decrease for some weird reason.
@@ -27,9 +27,9 @@ export default function getNumberType(input, options, metadata)
27
27
  return
28
28
  }
29
29
 
30
- metadata = new Metadata(metadata)
30
+ const metadata = new Metadata(metadataJson)
31
31
 
32
- metadata.selectNumberingPlan(input.country, input.countryCallingCode)
32
+ metadata.selectNumberingPlan(input.country || input.countryCallingCode)
33
33
 
34
34
  const nationalNumber = options.v2 ? input.nationalNumber : input.phone
35
35
 
@@ -18,11 +18,8 @@ export default function getPossibleCountriesForNumber(callingCode, nationalNumbe
18
18
  })
19
19
  }
20
20
 
21
- function couldNationalNumberBelongToCountry(nationalNumber, country, metadata) {
22
- const _metadata = new Metadata(metadata)
23
- _metadata.selectNumberingPlan(country)
24
- if (_metadata.numberingPlan.possibleLengths().indexOf(nationalNumber.length) >= 0) {
25
- return true
26
- }
27
- return false
21
+ function couldNationalNumberBelongToCountry(nationalNumber, country, metadataJson) {
22
+ const metadata = new Metadata(metadataJson)
23
+ metadata.selectNumberingPlan(country)
24
+ return metadata.numberingPlan.possibleLengths().indexOf(nationalNumber.length) >= 0
28
25
  }
@@ -0,0 +1,5 @@
1
+ const CALLING_CODE_REG_EXP = /^\d+$/
2
+
3
+ export default function isCallingCode(string) {
4
+ return CALLING_CODE_REG_EXP.test(string)
5
+ }
@@ -0,0 +1,5 @@
1
+ const COUNTRY_CODE_REG_EXP = /^[A-Z]{2}$/
2
+
3
+ export default function isCountryCode(string) {
4
+ return COUNTRY_CODE_REG_EXP.test(string)
5
+ }
@@ -3,19 +3,24 @@ import { VALID_DIGITS } from '../constants.js'
3
3
 
4
4
  const CAPTURING_DIGIT_PATTERN = new RegExp('([' + VALID_DIGITS + '])')
5
5
 
6
- export default function stripIddPrefix(number, country, callingCode, metadata) {
6
+ export default function stripIddPrefix(number, country, callingCode, metadataJson) {
7
7
  if (!country) {
8
8
  return
9
9
  }
10
+
11
+ // Get the IDD prefix pattern.
12
+ const metadata = new Metadata(metadataJson)
13
+ metadata.selectNumberingPlan(country || callingCode)
14
+ const IDDPrefixPattern = new RegExp(metadata.IDDPrefix())
15
+
10
16
  // Check if the number is IDD-prefixed.
11
- const countryMetadata = new Metadata(metadata)
12
- countryMetadata.selectNumberingPlan(country, callingCode)
13
- const IDDPrefixPattern = new RegExp(countryMetadata.IDDPrefix())
14
17
  if (number.search(IDDPrefixPattern) !== 0) {
15
18
  return
16
19
  }
20
+
17
21
  // Strip IDD prefix.
18
22
  number = number.slice(number.match(IDDPrefixPattern)[0].length)
23
+
19
24
  // If there're any digits after an IDD prefix,
20
25
  // then those digits are a country calling code.
21
26
  // Since no country code starts with a `0`,
@@ -26,5 +31,6 @@ export default function stripIddPrefix(number, country, callingCode, metadata) {
26
31
  return
27
32
  }
28
33
  }
34
+
29
35
  return number
30
36
  }
@@ -11,22 +11,22 @@ import checkNumberLength from './helpers/checkNumberLength.js'
11
11
  *
12
12
  * @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: '...' }`.
13
13
  * @param {object} [options]
14
- * @param {object} metadata
14
+ * @param {object} metadataJson
15
15
  * @return {string}
16
16
  */
17
- export default function isPossiblePhoneNumber(input, options, metadata) {
17
+ export default function isPossiblePhoneNumber(input, options, metadataJson) {
18
18
  /* istanbul ignore if */
19
19
  if (options === undefined) {
20
20
  options = {}
21
21
  }
22
22
 
23
- metadata = new Metadata(metadata)
23
+ const metadata = new Metadata(metadataJson)
24
24
 
25
25
  if (options.v2) {
26
26
  if (!input.countryCallingCode) {
27
27
  throw new Error('Invalid phone number object passed')
28
28
  }
29
- metadata.selectNumberingPlan(input.countryCallingCode)
29
+ metadata.selectNumberingPlan(input.country || input.countryCallingCode)
30
30
  } else {
31
31
  if (!input.phone) {
32
32
  return false
@@ -47,7 +47,7 @@ export default function isPossiblePhoneNumber(input, options, metadata) {
47
47
  // Old (legacy) metadata (< 1.0.18) had no "possible length" data.
48
48
  // So `isPossibleNumber()` function is supported only for non-legacy metadata.
49
49
  if (metadata.possibleLengths()) {
50
- return isPossibleNumber(input.phone || input.nationalNumber, input.country, metadata)
50
+ return isPossibleNumber(input.phone || input.nationalNumber, metadata)
51
51
  }
52
52
 
53
53
  // There was a bug in versions from `1.7.35` to `1.7.37` of `libphonenumber-js`
@@ -65,8 +65,14 @@ export default function isPossiblePhoneNumber(input, options, metadata) {
65
65
  throw new Error('Missing "possibleLengths" in metadata. Perhaps the metadata has been generated before v1.0.18.');
66
66
  }
67
67
 
68
- export function isPossibleNumber(nationalNumber, country, metadata) { //, isInternational) {
69
- switch (checkNumberLength(nationalNumber, country, metadata)) {
68
+ /**
69
+ * Tells if a given national (significant) number is possible
70
+ * @param {string} nationalNumber
71
+ * @param {Metadata} metadata = Metadata instance with a pre-selected telephone numbering plan.
72
+ * @returns {boolean}
73
+ */
74
+ export function isPossibleNumber(nationalNumber, metadata) {
75
+ switch (checkNumberLength(nationalNumber, undefined, metadata)) {
70
76
  case 'IS_POSSIBLE':
71
77
  return true
72
78
  // This library ignores "local-only" phone numbers (for simplicity).
package/source/isValid.js CHANGED
@@ -36,15 +36,15 @@ import getNumberType from './helpers/getNumberType.js'
36
36
  * isValidNumber({ phone: '8005553535', country: 'RU' }, metadata)
37
37
  * ```
38
38
  */
39
- export default function isValidNumber(input, options, metadata)
39
+ export default function isValidNumber(input, options, metadataJson)
40
40
  {
41
41
  // If assigning the `{}` default value is moved to the arguments above,
42
42
  // code coverage would decrease for some weird reason.
43
43
  options = options || {}
44
44
 
45
- metadata = new Metadata(metadata)
45
+ const metadata = new Metadata(metadataJson)
46
46
 
47
- metadata.selectNumberingPlan(input.country, input.countryCallingCode)
47
+ metadata.selectNumberingPlan(input.country || input.countryCallingCode)
48
48
 
49
49
  // By default, countries only have type regexps when it's required for
50
50
  // distinguishing different countries having the same `countryCallingCode`.