libphonenumber-js 1.12.17 → 1.12.21

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 (179) hide show
  1. package/CHANGELOG.md +38 -2
  2. package/METADATA.md +2 -0
  3. package/README.md +255 -165
  4. package/build/AsYouType.js +6 -8
  5. package/build/AsYouType.js.map +1 -1
  6. package/build/AsYouType.test.js +13 -14
  7. package/build/AsYouType.test.js.map +1 -1
  8. package/build/AsYouTypeFormatter.complete.js +4 -4
  9. package/build/AsYouTypeFormatter.complete.js.map +1 -1
  10. package/build/AsYouTypeFormatter.js +4 -4
  11. package/build/AsYouTypeFormatter.js.map +1 -1
  12. package/build/AsYouTypeParser.js +42 -29
  13. package/build/AsYouTypeParser.js.map +1 -1
  14. package/build/AsYouTypeState.js +21 -17
  15. package/build/AsYouTypeState.js.map +1 -1
  16. package/build/PhoneNumber.js +2 -2
  17. package/build/PhoneNumber.js.map +1 -1
  18. package/build/findNumbers/Leniency.js +0 -1
  19. package/build/findNumbers/Leniency.js.map +1 -1
  20. package/build/format.js +4 -4
  21. package/build/format.js.map +1 -1
  22. package/build/formatPhoneNumberForMobileDialing.js +2 -2
  23. package/build/formatPhoneNumberForMobileDialing.js.map +1 -1
  24. package/build/helpers/checkNumberLength.js +39 -7
  25. package/build/helpers/checkNumberLength.js.map +1 -1
  26. package/build/helpers/checkNumberLength.test.js +51 -12
  27. package/build/helpers/checkNumberLength.test.js.map +1 -1
  28. package/build/helpers/extractCountryCallingCode.js +13 -10
  29. package/build/helpers/extractCountryCallingCode.js.map +1 -1
  30. package/build/helpers/extractCountryCallingCode.test.js +3 -3
  31. package/build/helpers/extractCountryCallingCode.test.js.map +1 -1
  32. package/build/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +10 -8
  33. package/build/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js.map +1 -1
  34. package/build/helpers/extractNationalNumber.js +15 -5
  35. package/build/helpers/extractNationalNumber.js.map +1 -1
  36. package/build/helpers/extractNationalNumber.test.js +10 -2
  37. package/build/helpers/extractNationalNumber.test.js.map +1 -1
  38. package/build/helpers/getCountryByCallingCode.js +3 -2
  39. package/build/helpers/getCountryByCallingCode.js.map +1 -1
  40. package/build/helpers/getCountryByNationalNumber.js +27 -6
  41. package/build/helpers/getCountryByNationalNumber.js.map +1 -1
  42. package/build/helpers/matchesEntirely.js +4 -4
  43. package/build/helpers/matchesEntirely.js.map +1 -1
  44. package/build/isPossible.js +4 -4
  45. package/build/isPossible.js.map +1 -1
  46. package/build/isPossible.test.js +9 -0
  47. package/build/isPossible.test.js.map +1 -1
  48. package/build/isPossiblePhoneNumber.test.js +6 -0
  49. package/build/isPossiblePhoneNumber.test.js.map +1 -1
  50. package/build/metadata.js +2 -2
  51. package/build/metadata.js.map +1 -1
  52. package/build/parse.js +9 -7
  53. package/build/parse.js.map +1 -1
  54. package/build/parseIncompletePhoneNumber.js +7 -7
  55. package/build/parseIncompletePhoneNumber.js.map +1 -1
  56. package/build/parsePhoneNumber.test.js +15 -0
  57. package/build/parsePhoneNumber.test.js.map +1 -1
  58. package/build/validatePhoneNumberLength.js +1 -1
  59. package/build/validatePhoneNumberLength.js.map +1 -1
  60. package/build-scripts/check-for-metadata-changes-and-create-pull-request.js +87 -0
  61. package/build-scripts/check-for-metadata-changes-and-release.js +92 -0
  62. package/build-scripts/check-for-uncommitted-files.js +11 -0
  63. package/{runnable/json-to-js.js → build-scripts/create-js-file-for-json-file.js} +2 -2
  64. package/{runnable/metadata-branch.js → build-scripts/create-metadata-update-branch.js} +6 -1
  65. package/{runnable/generate-country-codes.js → build-scripts/generate-country-codes-list-in-typescript.js} +1 -0
  66. package/{runnable/generate.js → build-scripts/generate-metadata-from-google-metadata.js} +8 -14
  67. package/{runnable/modules → build-scripts/helpers}/commit.js +1 -0
  68. package/{runnable/modules → build-scripts/helpers}/exec.js +1 -0
  69. package/build-scripts/helpers/getUncommittedChanges.js +33 -0
  70. package/{runnable/modules/update-metadata.js → build-scripts/helpers/update-metadata-from-google-metadata.js} +9 -7
  71. package/{runnable/download.js → build-scripts/pull-google-metadata.js} +12 -4
  72. package/bundle/libphonenumber-js.min.js +1 -1
  73. package/bundle/libphonenumber-js.min.js.map +1 -1
  74. package/bundle/libphonenumber-max.js +1 -1
  75. package/bundle/libphonenumber-max.js.map +1 -1
  76. package/bundle/libphonenumber-min.js +1 -1
  77. package/bundle/libphonenumber-min.js.map +1 -1
  78. package/bundle/libphonenumber-mobile.js +1 -1
  79. package/bundle/libphonenumber-mobile.js.map +1 -1
  80. package/core/index.d.cts +1 -1
  81. package/core/index.d.ts +1 -1
  82. package/custom.d.ts +1 -1
  83. package/es6/AsYouType.js +6 -8
  84. package/es6/AsYouType.js.map +1 -1
  85. package/es6/AsYouType.test.js +13 -14
  86. package/es6/AsYouType.test.js.map +1 -1
  87. package/es6/AsYouTypeFormatter.complete.js +4 -4
  88. package/es6/AsYouTypeFormatter.complete.js.map +1 -1
  89. package/es6/AsYouTypeFormatter.js +4 -4
  90. package/es6/AsYouTypeFormatter.js.map +1 -1
  91. package/es6/AsYouTypeParser.js +42 -29
  92. package/es6/AsYouTypeParser.js.map +1 -1
  93. package/es6/AsYouTypeState.js +21 -17
  94. package/es6/AsYouTypeState.js.map +1 -1
  95. package/es6/PhoneNumber.js +1 -2
  96. package/es6/PhoneNumber.js.map +1 -1
  97. package/es6/findNumbers/Leniency.js +0 -1
  98. package/es6/findNumbers/Leniency.js.map +1 -1
  99. package/es6/format.js +4 -4
  100. package/es6/format.js.map +1 -1
  101. package/es6/formatPhoneNumberForMobileDialing.js +2 -2
  102. package/es6/formatPhoneNumberForMobileDialing.js.map +1 -1
  103. package/es6/helpers/checkNumberLength.js +39 -7
  104. package/es6/helpers/checkNumberLength.js.map +1 -1
  105. package/es6/helpers/checkNumberLength.test.js +49 -12
  106. package/es6/helpers/checkNumberLength.test.js.map +1 -1
  107. package/es6/helpers/extractCountryCallingCode.js +13 -10
  108. package/es6/helpers/extractCountryCallingCode.js.map +1 -1
  109. package/es6/helpers/extractCountryCallingCode.test.js +3 -3
  110. package/es6/helpers/extractCountryCallingCode.test.js.map +1 -1
  111. package/es6/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +10 -8
  112. package/es6/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js.map +1 -1
  113. package/es6/helpers/extractNationalNumber.js +15 -5
  114. package/es6/helpers/extractNationalNumber.js.map +1 -1
  115. package/es6/helpers/extractNationalNumber.test.js +10 -2
  116. package/es6/helpers/extractNationalNumber.test.js.map +1 -1
  117. package/es6/helpers/getCountryByCallingCode.js +3 -2
  118. package/es6/helpers/getCountryByCallingCode.js.map +1 -1
  119. package/es6/helpers/getCountryByNationalNumber.js +28 -6
  120. package/es6/helpers/getCountryByNationalNumber.js.map +1 -1
  121. package/es6/helpers/matchesEntirely.js +4 -4
  122. package/es6/helpers/matchesEntirely.js.map +1 -1
  123. package/es6/isPossible.js +4 -4
  124. package/es6/isPossible.js.map +1 -1
  125. package/es6/isPossible.test.js +9 -0
  126. package/es6/isPossible.test.js.map +1 -1
  127. package/es6/isPossiblePhoneNumber.test.js +6 -0
  128. package/es6/isPossiblePhoneNumber.test.js.map +1 -1
  129. package/es6/metadata.js +2 -2
  130. package/es6/metadata.js.map +1 -1
  131. package/es6/parse.js +9 -7
  132. package/es6/parse.js.map +1 -1
  133. package/es6/parseIncompletePhoneNumber.js +7 -7
  134. package/es6/parseIncompletePhoneNumber.js.map +1 -1
  135. package/es6/parsePhoneNumber.test.js +15 -0
  136. package/es6/parsePhoneNumber.test.js.map +1 -1
  137. package/es6/validatePhoneNumberLength.js +1 -1
  138. package/es6/validatePhoneNumberLength.js.map +1 -1
  139. package/index.d.cts +1 -1
  140. package/index.d.ts +1 -1
  141. package/max/index.d.cts +1 -1
  142. package/max/index.d.ts +1 -1
  143. package/min/index.d.cts +1 -1
  144. package/min/index.d.ts +1 -1
  145. package/mobile/index.d.cts +1 -1
  146. package/mobile/index.d.ts +1 -1
  147. package/package.json +17 -23
  148. package/source/AsYouType.js +8 -10
  149. package/source/AsYouType.test.js +13 -14
  150. package/source/AsYouTypeFormatter.complete.js +4 -4
  151. package/source/AsYouTypeFormatter.js +4 -4
  152. package/source/AsYouTypeParser.js +45 -28
  153. package/source/AsYouTypeState.js +21 -17
  154. package/source/PhoneNumber.js +1 -2
  155. package/source/findNumbers/Leniency.js +0 -1
  156. package/source/format.js +4 -4
  157. package/source/formatPhoneNumberForMobileDialing.js +2 -2
  158. package/source/helpers/checkNumberLength.js +40 -7
  159. package/source/helpers/checkNumberLength.test.js +54 -12
  160. package/source/helpers/extractCountryCallingCode.js +15 -10
  161. package/source/helpers/extractCountryCallingCode.test.js +3 -3
  162. package/source/helpers/extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js +19 -8
  163. package/source/helpers/extractNationalNumber.js +15 -5
  164. package/source/helpers/extractNationalNumber.test.js +11 -2
  165. package/source/helpers/getCountryByCallingCode.js +2 -2
  166. package/source/helpers/getCountryByNationalNumber.js +27 -6
  167. package/source/helpers/matchesEntirely.js +4 -4
  168. package/source/isPossible.js +4 -4
  169. package/source/isPossible.test.js +11 -0
  170. package/source/isPossiblePhoneNumber.test.js +7 -0
  171. package/source/metadata.js +2 -2
  172. package/source/parse.js +9 -5
  173. package/source/parseIncompletePhoneNumber.js +7 -7
  174. package/source/parsePhoneNumber.test.js +17 -0
  175. package/source/validatePhoneNumberLength.js +1 -1
  176. package/runnable/metadata-update-and-push-and-pull-request.js +0 -47
  177. package/runnable/metadata-update-and-push.js +0 -30
  178. package/runnable/metadata-update-and-release.js +0 -36
  179. /package/{runnable/create-commonjs-package-json.js → build-scripts/create-commonjs-package-json-file.js} +0 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "libphonenumber-js",
3
- "version": "1.12.17",
3
+ "version": "1.12.21",
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",
@@ -99,10 +99,11 @@
99
99
  "cpy-cli": "^5.0.0",
100
100
  "crlf": "^1.1.1",
101
101
  "cross-env": "^10.0.0",
102
+ "gmail-send": "^1.8.14",
102
103
  "istanbul": "^1.1.0-alpha.1",
103
104
  "jest": "^30.0.5",
104
105
  "jest-codemods": "^0.34.2",
105
- "libphonenumber-metadata-generator": "^2.0.1",
106
+ "libphonenumber-metadata-generator": "^2.0.3",
106
107
  "minimist": "^1.2.8",
107
108
  "mocha": "^10.0.0",
108
109
  "npm-run-all": "^4.1.5",
@@ -114,26 +115,19 @@
114
115
  "tslib": "^2.8.1"
115
116
  },
116
117
  "scripts": {
117
- "metadata:update:job": "git reset --hard && git pull && npm install && npm run metadata:update:release",
118
- "metadata:pull-request": "node runnable/metadata-pull-request",
119
- "metadata:branch": "node runnable/metadata-branch",
120
- "metadata:unbranch": "node runnable/metadata-unbranch",
121
- "metadata:publish": "npm version patch && npm publish && git push",
122
- "metadata:update:release": "npm run metadata:download && node runnable/metadata-update-and-release",
123
- "metadata:update:branch": "npm run metadata:branch && npm run metadata:download && node runnable/metadata-update-and-push",
124
- "metadata:update:pull-request": "npm run metadata:branch && npm run metadata:download && node runnable/metadata-update-and-push-and-pull-request",
118
+ "metadata:update:job": "node build-scripts/check-for-uncommitted-files && git reset --hard && git pull && npm install && npm run metadata:update-and-release",
119
+ "metadata:update-and-release": "node build-scripts/pull-google-metadata ./PhoneNumberMetadata.xml ./metadata-info.json && node build-scripts/check-for-metadata-changes-and-release ./PhoneNumberMetadata.xml ./metadata-info.json",
125
120
  "metadata:generate": "npm-run-all metadata:generate:min metadata:generate:full metadata:generate:max metadata:generate:mobile metadata:generate:min:js metadata:generate:full:js metadata:generate:max:js metadata:generate:mobile:js metadata:generate:mobile:examples:js",
126
- "metadata:generate:min": "node runnable/generate ../PhoneNumberMetadata.xml ../metadata.min.json --examples mobile",
127
- "metadata:generate:full": "node runnable/generate ../PhoneNumberMetadata.xml ../metadata.full.json --extended --debug",
128
- "metadata:generate:max": "node runnable/generate ../PhoneNumberMetadata.xml ../metadata.max.json --extended --debug",
129
- "metadata:generate:mobile": "node runnable/generate ../PhoneNumberMetadata.xml ../metadata.mobile.json --types mobile",
130
- "metadata:generate:min:js": "node runnable/json-to-js ./metadata.min.json",
131
- "metadata:generate:full:js": "node runnable/json-to-js ./metadata.full.json",
132
- "metadata:generate:max:js": "node runnable/json-to-js ./metadata.max.json",
133
- "metadata:generate:mobile:js": "node runnable/json-to-js ./metadata.mobile.json",
134
- "metadata:generate:mobile:examples:js": "node runnable/json-to-js ./examples.mobile.json",
135
- "metadata:download": "node runnable/download PhoneNumberMetadata.xml",
136
- "generate-country-codes": "node --experimental-json-modules runnable/generate-country-codes",
121
+ "metadata:generate:min": "node build-scripts/generate-metadata-from-google-metadata ./PhoneNumberMetadata.xml ./metadata.min.json --examples mobile",
122
+ "metadata:generate:full": "node build-scripts/generate-metadata-from-google-metadata ./PhoneNumberMetadata.xml ./metadata.full.json --extended --debug",
123
+ "metadata:generate:max": "node build-scripts/generate-metadata-from-google-metadata ./PhoneNumberMetadata.xml ./metadata.max.json --extended --debug",
124
+ "metadata:generate:mobile": "node build-scripts/generate-metadata-from-google-metadata ./PhoneNumberMetadata.xml ./metadata.mobile.json --types mobile",
125
+ "metadata:generate:min:js": "node build-scripts/create-js-file-for-json-file ./metadata.min.json",
126
+ "metadata:generate:full:js": "node build-scripts/create-js-file-for-json-file ./metadata.full.json",
127
+ "metadata:generate:max:js": "node build-scripts/create-js-file-for-json-file ./metadata.max.json",
128
+ "metadata:generate:mobile:js": "node build-scripts/create-js-file-for-json-file ./metadata.mobile.json",
129
+ "metadata:generate:mobile:examples:js": "node build-scripts/create-js-file-for-json-file ./examples.mobile.json",
130
+ "typescript:generate-country-codes-list": "node --experimental-json-modules build-scripts/generate-country-codes-list-in-typescript",
137
131
  "transform-mocha-tests-into-jest-tests": "npx jest-codemods",
138
132
  "test": "npm run test--mocha",
139
133
  "test--jest": "npm-run-all test:except-exports--jest test:exports--mocha",
@@ -152,7 +146,7 @@
152
146
  "build:commonjs": "npm-run-all build:commonjs:with-tests build:commonjs:package.json build:commonjs:create-typescript-definitions build:commonjs:patch-typescript-definitions",
153
147
  "build:commonjs:before-es-modules": "cross-env BABEL_ENV=commonjs babel ./source --out-dir ./build --source-maps --ignore test.js",
154
148
  "build:commonjs:with-tests": "cross-env BABEL_ENV=commonjs babel ./source --out-dir ./build --source-maps",
155
- "build:commonjs:package.json": "node runnable/create-commonjs-package-json.js",
149
+ "build:commonjs:package.json": "node build-scripts/create-commonjs-package-json-file.js",
156
150
  "build:commonjs:create-typescript-definitions": "rimraf --verbose --glob ./*.d.cts \"./!(node_modules)/**/*.d.cts\" && cpy **/{index,metadata*,examples*,types}.d.ts . --rename={{basename}}.cts && renamer --find d.cts.ts --replace d.cts ./*.d.cts.ts \"./!(node_modules)/**/*.d.cts.ts\"",
157
151
  "build:commonjs:patch-typescript-definitions": "replace-in-file \".d.js';\" \".d.cjs';\" **/*.d.cts",
158
152
  "build:modules:copy-typescript-definitions": "cpy --flat min/index.d.ts max && cpy --flat min/index.d.ts mobile",
@@ -165,7 +159,7 @@
165
159
  "build:modules": "cross-env BABEL_ENV=es6 babel ./source --out-dir ./es6 --source-maps --ignore test.js",
166
160
  "build:bundle": "rollup --config rollup.config.mjs",
167
161
  "build": "npm-run-all clean build:modules:copy-typescript-definitions build:modules build:commonjs build:bundle build:copy-metadata-files-to-subpackage-folders build:copy-examples-files-to-subpackage-folders",
168
- "prepublishOnly": "npm run metadata:generate && npm run generate-country-codes && crlf --set=LF metadata.*.json && npm-run-all build test"
162
+ "prepublishOnly": "npm run metadata:generate && npm run typescript:generate-country-codes-list && crlf --set=LF metadata.*.json && npm-run-all build test"
169
163
  },
170
164
  "repository": {
171
165
  "type": "git",
@@ -96,7 +96,7 @@ export default class AsYouType {
96
96
  this.state = new AsYouTypeState({
97
97
  onCountryChange: (country) => {
98
98
  // Before version `1.6.0`, the official `AsYouType` formatter API
99
- // included the `.country` property of an `AsYouType` instance.
99
+ // included a `.country` property on an `AsYouType` instance.
100
100
  // Since that property (along with the others) have been moved to
101
101
  // `this.state`, `this.country` property is emulated for compatibility
102
102
  // with the old versions.
@@ -239,11 +239,11 @@ export default class AsYouType {
239
239
  getNonFormattedNationalNumberWithPrefix() {
240
240
  const {
241
241
  nationalSignificantNumber,
242
- complexPrefixBeforeNationalSignificantNumber,
242
+ prefixBeforeNationalSignificantNumberThatIsNotNationalPrefix,
243
243
  nationalPrefix
244
244
  } = this.state
245
245
  let number = nationalSignificantNumber
246
- const prefix = complexPrefixBeforeNationalSignificantNumber || nationalPrefix
246
+ const prefix = prefixBeforeNationalSignificantNumberThatIsNotNationalPrefix || nationalPrefix
247
247
  if (prefix) {
248
248
  number = prefix + number
249
249
  }
@@ -251,11 +251,11 @@ export default class AsYouType {
251
251
  }
252
252
 
253
253
  getNonFormattedNumber() {
254
- const { nationalSignificantNumberMatchesInput } = this.state
254
+ const { nationalSignificantNumberIsModified } = this.state
255
255
  return this.getFullNumber(
256
- nationalSignificantNumberMatchesInput
257
- ? this.getNonFormattedNationalNumberWithPrefix()
258
- : this.state.getNationalDigits()
256
+ nationalSignificantNumberIsModified
257
+ ? this.state.getNationalDigits()
258
+ : this.getNonFormattedNationalNumberWithPrefix()
259
259
  )
260
260
  }
261
261
 
@@ -272,7 +272,7 @@ export default class AsYouType {
272
272
  return countryCodes && countryCodes.length > 1
273
273
  }
274
274
 
275
- // Determines the country of the phone number
275
+ // Determines the exact country of the phone number
276
276
  // entered so far based on the country phone code
277
277
  // and the national phone number.
278
278
  determineTheCountry() {
@@ -280,7 +280,6 @@ export default class AsYouType {
280
280
  this.isInternational() ? this.state.callingCode : this.defaultCallingCode,
281
281
  {
282
282
  nationalNumber: this.state.nationalSignificantNumber,
283
- defaultCountry: this.defaultCountry,
284
283
  metadata: this.metadata
285
284
  }
286
285
  ))
@@ -386,7 +385,6 @@ export default class AsYouType {
386
385
  if (ambiguousCountries.length > 1) {
387
386
  const exactCountry = getCountryByNationalNumber(nationalSignificantNumber, {
388
387
  countries: ambiguousCountries,
389
- defaultCountry: this.defaultCountry,
390
388
  metadata: this.metadata.metadata
391
389
  })
392
390
  if (exactCountry) {
@@ -630,17 +630,16 @@ describe('AsYouType', () => {
630
630
  })
631
631
 
632
632
  it('should return non-formatted phone number when no format matches and national (significant) number has digits added', () => {
633
- // When formatting Argentinian mobile numbers in international format,
634
- // a `9` is prepended, when compared to national format.
633
+ // When inputting "0343515551212999" Argentinian mobile number,
634
+ // the parsed national (significant) number is "93435551212999".
635
+ // There, one can see how it stripped "0" national prefix and prepended a "9",
636
+ // because that's how it is instructed to do in Argentina's metadata.
637
+ //
638
+ // So in this case, national (significant) number doesn't match the input
639
+ // and, therefore, `state.nationalSignificantNumberIsModified` flag is `true`.
640
+ //
635
641
  const asYouType = new AsYouType('AR')
636
- // Digits shouldn't be changed when formatting in national format.
637
- // (no `9` is prepended).
638
- // First parses national (significant) number by prepending `9` to it
639
- // and stripping `15` from it.
640
- // Then uses `$2 15-$3-$4` format that strips the leading `9`
641
- // and adds `15`.
642
- // `this.nationalSignificantNumberMatchesInput` is `false` in this case,
643
- // so `getNonFormattedNumber()` returns `getFullNumber(getNationalDigits())`.
642
+ // This line checks that the formatter didn't alter the output digits compared to the input digits.
644
643
  expect(asYouType.input('0343515551212999')).to.equal('0343515551212999')
645
644
  })
646
645
 
@@ -718,7 +717,7 @@ describe('AsYouType', () => {
718
717
  expect(formatter.input('081')).to.equal('081')
719
718
  })
720
719
 
721
- it('should prepend `complexPrefixBeforeNationalSignificantNumber` (not a complete number)', () => {
720
+ it('should prepend `prefixBeforeNationalSignificantNumberThatIsNotNationalPrefix` (not a complete number)', () => {
722
721
  // A country having `national_prefix_for_parsing` with a "capturing group".
723
722
  // National prefix is either not used in a format or is optional.
724
723
  // Input phone number without a national prefix.
@@ -730,10 +729,10 @@ describe('AsYouType', () => {
730
729
  // `formatter.digits` is not always `formatter.nationalPrefix`
731
730
  // plus `formatter.nationalNumberDigits`.
732
731
  expect(formatter.state.nationalPrefix).to.be.undefined
733
- expect(formatter.state.complexPrefixBeforeNationalSignificantNumber).to.equal('1831')
732
+ expect(formatter.state.prefixBeforeNationalSignificantNumberThatIsNotNationalPrefix).to.equal('1831')
734
733
  })
735
734
 
736
- it('should prepend `complexPrefixBeforeNationalSignificantNumber` (complete number)', () => {
735
+ it('should prepend `prefixBeforeNationalSignificantNumberThatIsNotNationalPrefix` (complete number)', () => {
737
736
  // A country having `national_prefix_for_parsing` with a "capturing group".
738
737
  // National prefix is either not used in a format or is optional.
739
738
  // Input phone number without a national prefix.
@@ -745,7 +744,7 @@ describe('AsYouType', () => {
745
744
  // `formatter.digits` is not always `formatter.nationalPrefix`
746
745
  // plus `formatter.nationalNumberDigits`.
747
746
  expect(formatter.state.nationalPrefix).to.be.undefined
748
- expect(formatter.state.complexPrefixBeforeNationalSignificantNumber).to.equal('1831')
747
+ expect(formatter.state.prefixBeforeNationalSignificantNumberThatIsNotNationalPrefix).to.equal('1831')
749
748
  })
750
749
 
751
750
  it('should work with Mexico numbers', () => {
@@ -21,8 +21,8 @@ export default function formatCompleteNumber(state, format, {
21
21
  }
22
22
  }
23
23
 
24
- export function canFormatCompleteNumber(nationalSignificantNumber, metadata) {
25
- return checkNumberLength(nationalSignificantNumber, metadata) === 'IS_POSSIBLE'
24
+ export function canFormatCompleteNumber(nationalSignificantNumber, country, metadata) {
25
+ return checkNumberLength(nationalSignificantNumber, country, metadata) === 'IS_POSSIBLE'
26
26
  }
27
27
 
28
28
  function formatNationalNumberWithAndWithoutNationalPrefixFormattingRule(state, format, {
@@ -90,8 +90,8 @@ function formatNationalNumber(state, format, {
90
90
  formattedNationalNumber = state.nationalPrefix +
91
91
  getSeparatorAfterNationalPrefix(format) +
92
92
  formattedNationalNumber
93
- } else if (state.complexPrefixBeforeNationalSignificantNumber) {
94
- formattedNationalNumber = state.complexPrefixBeforeNationalSignificantNumber +
93
+ } else if (state.prefixBeforeNationalSignificantNumberThatIsNotNationalPrefix) {
94
+ formattedNationalNumber = state.prefixBeforeNationalSignificantNumberThatIsNotNationalPrefix +
95
95
  ' ' +
96
96
  formattedNationalNumber
97
97
  }
@@ -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, this.metadata)) {
157
+ if (canFormatCompleteNumber(state.nationalSignificantNumber, state.country, this.metadata)) {
158
158
  for (const format of this.matchingFormats) {
159
159
  const formattedCompleteNumber = formatCompleteNumber(
160
160
  state,
@@ -534,7 +534,7 @@ export default class AsYouTypeFormatter {
534
534
  nationalSignificantNumber,
535
535
  international,
536
536
  nationalPrefix,
537
- complexPrefixBeforeNationalSignificantNumber
537
+ prefixBeforeNationalSignificantNumberThatIsNotNationalPrefix
538
538
  }) {
539
539
  let pattern = format.pattern()
540
540
 
@@ -654,9 +654,9 @@ export default class AsYouTypeFormatter {
654
654
  // before the national (significant) number, optionally spacing
655
655
  // the two with a whitespace.
656
656
  if (!nationalPrefixIncludedInTemplate) {
657
- if (complexPrefixBeforeNationalSignificantNumber) {
657
+ if (prefixBeforeNationalSignificantNumberThatIsNotNationalPrefix) {
658
658
  // Prepend the prefix to the template manually.
659
- template = repeat(DIGIT_PLACEHOLDER, complexPrefixBeforeNationalSignificantNumber.length) +
659
+ template = repeat(DIGIT_PLACEHOLDER, prefixBeforeNationalSignificantNumberThatIsNotNationalPrefix.length) +
660
660
  ' ' +
661
661
  template
662
662
  } else if (nationalPrefix) {
@@ -66,7 +66,7 @@ export default class AsYouTypeParser {
66
66
  let justLeadingPlus
67
67
  if (hasPlus) {
68
68
  if (!state.digits) {
69
- state.startInternationalNumber()
69
+ state.startInternationalNumber(undefined, undefined)
70
70
  if (!digits) {
71
71
  justLeadingPlus = true
72
72
  }
@@ -143,6 +143,7 @@ export default class AsYouTypeParser {
143
143
  extractCountryCallingCode(state) {
144
144
  const { countryCallingCode, number } = extractCountryCallingCode(
145
145
  '+' + state.getDigitsWithoutInternationalPrefix(),
146
+ state.country,
146
147
  this.defaultCountry,
147
148
  this.defaultCallingCode,
148
149
  this.metadata.metadata
@@ -245,39 +246,55 @@ export default class AsYouTypeParser {
245
246
  nationalDigits,
246
247
  setState
247
248
  ) {
248
- let complexPrefixBeforeNationalSignificantNumber
249
- let nationalSignificantNumberMatchesInput
250
- // This check also works with empty `this.nationalSignificantNumber`.
251
- const nationalSignificantNumberIndex = nationalDigits.lastIndexOf(nationalSignificantNumber)
252
- // If the extracted national (significant) number is the
253
- // last substring of the `digits`, then it means that it hasn't been altered:
254
- // no digits have been removed from the national (significant) number
255
- // while applying `national_prefix_transform_rule`.
249
+ // Tells if the parsed national (significant) number is present as-is in the input string.
250
+ // For example, when inputting "0343515551212999" Argentinian mobile number,
251
+ // the parsed national (significant) number is "93435551212999".
252
+ // There, one can see how it stripped "0" national prefix and prepended a "9",
253
+ // because that's how it is instructed to do in Argentina's metadata.
254
+ // So in the described example, the parsed national (significant) number is not present
255
+ // as-is in the input string. Instead, it's "modified" in the input string.
256
+ // https://gitlab.com/catamphetamine/libphonenumber-js/-/blob/master/METADATA.md#national_prefix_for_parsing--national_prefix_transform_rule
257
+ let nationalSignificantNumberIsModified = false
258
+
259
+ // In some countries, a phone number could have a prefix that is not a "national prefix"
260
+ // but rather some other type of "utility" prefix.
261
+ // For example, when calling within Australia, one could prepend `1831` prefix to hide
262
+ // caller's phone number.
256
263
  // https://gitlab.com/catamphetamine/libphonenumber-js/-/blob/master/METADATA.md#national_prefix_for_parsing--national_prefix_transform_rule
257
- if (nationalSignificantNumberIndex >= 0 &&
258
- nationalSignificantNumberIndex === nationalDigits.length - nationalSignificantNumber.length) {
259
- nationalSignificantNumberMatchesInput = true
260
- // If a prefix of a national (significant) number is not as simple
261
- // as just a basic national prefix, then such prefix is stored in
262
- // `this.complexPrefixBeforeNationalSignificantNumber` property and will be
263
- // prepended "as is" to the national (significant) number to produce
264
- // a formatted result.
264
+ let prefixBeforeNationalSignificantNumberThatIsNotNationalPrefix
265
+
266
+ // `nationalSignificantNumber` could be empty. In that case, `.lastIndexOf()` still works correctly.
267
+ const nationalSignificantNumberIndex = nationalDigits.lastIndexOf(nationalSignificantNumber)
268
+
269
+ // If the parsed national (significant) number is the last substring of the `nationalDigits`,
270
+ // then it means that it's present as-is in the input string.
271
+ // In any other case, the parsed national (significant) number is "modified" in the input string.
272
+ if (
273
+ nationalSignificantNumberIndex < 0 ||
274
+ nationalSignificantNumberIndex !== nationalDigits.length - nationalSignificantNumber.length
275
+ ) {
276
+ nationalSignificantNumberIsModified = true
277
+ } else {
265
278
  const prefixBeforeNationalNumber = nationalDigits.slice(0, nationalSignificantNumberIndex)
266
- // `prefixBeforeNationalNumber` is always non-empty,
267
- // because `onExtractedNationalNumber()` isn't called
268
- // when a national (significant) number hasn't been actually "extracted":
269
- // when a national (significant) number is equal to the national part of `digits`,
270
- // then `onExtractedNationalNumber()` doesn't get called.
271
- if (prefixBeforeNationalNumber !== nationalPrefix) {
272
- complexPrefixBeforeNationalSignificantNumber = prefixBeforeNationalNumber
279
+ // When national (significant) number is equal to the `nationalDigits`,
280
+ // this `onExtractedNationalNumber()` function simply doesn't get called.
281
+ // This means that at this point, `prefixBeforeNationalNumber` is always non-empty.
282
+ // Still, added this `if` check just to prevent potential silly bugs.
283
+ // The `!prefixBeforeNationalNumber` case is not really testable
284
+ // so this line is exluded from the code coverage.
285
+ /* istanbul ignore if */
286
+ if (prefixBeforeNationalNumber) {
287
+ if (prefixBeforeNationalNumber !== nationalPrefix) {
288
+ prefixBeforeNationalSignificantNumberThatIsNotNationalPrefix = prefixBeforeNationalNumber
289
+ }
273
290
  }
274
291
  }
275
292
  setState({
276
293
  nationalPrefix,
277
294
  carrierCode,
278
295
  nationalSignificantNumber,
279
- nationalSignificantNumberMatchesInput,
280
- complexPrefixBeforeNationalSignificantNumber
296
+ nationalSignificantNumberIsModified,
297
+ prefixBeforeNationalSignificantNumberThatIsNotNationalPrefix
281
298
  })
282
299
  // `onExtractedNationalNumber()` is only called when
283
300
  // the national (significant) number actually did change.
@@ -396,10 +413,10 @@ export default class AsYouTypeParser {
396
413
  fixMissingPlus(state) {
397
414
  if (!state.international) {
398
415
  const {
399
- countryCallingCode: newCallingCode,
400
- number
416
+ countryCallingCode: newCallingCode
401
417
  } = extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(
402
418
  state.digits,
419
+ state.country,
403
420
  this.defaultCountry,
404
421
  this.defaultCallingCode,
405
422
  this.metadata.metadata
@@ -1,20 +1,24 @@
1
1
  // This "state" object simply holds the state of the "AsYouType" parser:
2
2
  //
3
- // * `country?: string`
4
- // * `callingCode?: string`
5
- // * `digits: string`
6
- // * `international: boolean`
7
- // * `missingPlus: boolean`
8
- // * `IDDPrefix?: string`
9
- // * `carrierCode?: string`
10
- // * `nationalPrefix?: string`
11
- // * `nationalSignificantNumber?: string`
12
- // * `nationalSignificantNumberMatchesInput: boolean`
13
- // * `complexPrefixBeforeNationalSignificantNumber?: string`
14
- //
15
- // `state.country` and `state.callingCode` aren't required to be in sync.
16
- // For example, `state.country` could be `"AR"` and `state.callingCode` could be `undefined`.
17
- // So `state.country` and `state.callingCode` are totally independent.
3
+ // * `country?: string` — The exact country of the phone number, if it could be determined.
4
+ // When inputting a phone number in "international" format, it will derive the `country` from "country calling code" and the phone number digits.
5
+ // When inputting a phone number in "national" format, it will derive the `country` from `defaultCountry` that was specified when creating the `AsYouType` formatter.
6
+ // Sidenote: If `USE_NON_GEOGRAPHIC_COUNTRY_CODE` flag was `true`, then for "non-geographic phone numbers" `state.country` would've been "001".
7
+ // * `callingCode?: string` — "Country calling code" that has been extracted from the input phone number.
8
+ // When inputting a phone number in "international" format, it will extract the "country calling code" from the digits that follow the "+" character.
9
+ // 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
+ // * `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
+ // * `international: boolean` — Whether the phone number is being input in "international" format, i.e. with a "+" character.
13
+ // * `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.
14
+ // * `IDDPrefix?: string` — An "IDD prefix", when the phone number is being input in an "out-of-country dialing" format. https://wikitravel.org/en/International_dialling_prefix
15
+ // * `carrierCode?: string` — A "carrier code", if the phone number contains it. Normally, those can only be present in Colombia or Brazil, and only when calling from mobile phones to fixed-line numbers.
16
+ // * `nationalPrefix?: string` — "National prefix", if present in the phone number input.
17
+ // * `nationalSignificantNumber?: string` — National (significant) number digits that have been input so far.
18
+ // * `nationalSignificantNumberIsModified: boolean` — Tells if the parsed national (significant) number is present as-is in the input string. For example, when inputting "0343515551212999" Argentinian mobile number, the parsed national (significant) number is "93435551212999". There, one can see how it stripped "0" national prefix and prepended a "9", because that's how it is instructed to do in Argentina's metadata. So in the described example, the parsed national (significant) number is not present as-is in the input string. Instead, it's "modified" in the input string. https://gitlab.com/caamphetamine/libphonenumber-js/-/blob/master/METADATA.md#national_prefix_for_parsing--national_prefix_transform_rule
19
+ // * `prefixBeforeNationalSignificantNumberThatIsNotNationalPrefix?: string` — In some countries, a phone number could have a prefix that is not a "national prefix" but rather some other type of "utility" prefix.
20
+ // For example, when calling within Australia, one could prepend `1831` prefix to hide caller's phone number.
21
+ // https://gitlab.com/catamphetamine/libphonenumber-js/-/blob/master/METADATA.md#national_prefix_for_parsing--national_prefix_transform_rule
18
22
  //
19
23
  export default class AsYouTypeState {
20
24
  constructor({ onCountryChange, onCallingCodeChange }) {
@@ -34,10 +38,10 @@ export default class AsYouTypeState {
34
38
 
35
39
  resetNationalSignificantNumber() {
36
40
  this.nationalSignificantNumber = this.getNationalDigits()
37
- this.nationalSignificantNumberMatchesInput = true
41
+ this.nationalSignificantNumberIsModified = false
38
42
  this.nationalPrefix = undefined
39
43
  this.carrierCode = undefined
40
- this.complexPrefixBeforeNationalSignificantNumber = undefined
44
+ this.prefixBeforeNationalSignificantNumberThatIsNotNationalPrefix = undefined
41
45
  }
42
46
 
43
47
  update(properties) {
@@ -1,7 +1,6 @@
1
1
  import Metadata, { validateMetadata } from './metadata.js'
2
2
  import isPossibleNumber from './isPossible.js'
3
3
  import isValidNumber from './isValid.js'
4
- // import checkNumberLength from './helpers/checkNumberLength.js'
5
4
  import getNumberType from './helpers/getNumberType.js'
6
5
  import getPossibleCountriesForNumber from './helpers/getPossibleCountriesForNumber.js'
7
6
  import extractCountryCallingCode from './helpers/extractCountryCallingCode.js'
@@ -38,7 +37,7 @@ export default class PhoneNumber {
38
37
  if (!E164_NUMBER_REGEXP.test(e164Number)) {
39
38
  throw new Error('Invalid `number` argument passed: must consist of a "+" followed by digits')
40
39
  }
41
- const { countryCallingCode, number } = extractCountryCallingCode(e164Number, undefined, undefined, metadata)
40
+ const { countryCallingCode, number } = extractCountryCallingCode(e164Number, undefined, undefined, undefined, metadata)
42
41
  nationalNumber = number
43
42
  countryOrCountryCallingCode = countryCallingCode
44
43
  if (!nationalNumber) {
@@ -170,7 +170,6 @@ function isNationalPrefixPresentIfRequired(phoneNumber, { defaultCountry, metada
170
170
 
171
171
  const phoneNumberRegion = phoneNumber.country || getCountryByCallingCode(phoneNumber.countryCallingCode, {
172
172
  nationalNumber: phoneNumber.nationalNumber,
173
- defaultCountry,
174
173
  metadata
175
174
  })
176
175
 
package/source/format.js CHANGED
@@ -43,7 +43,7 @@ export default function formatNumber(input, format, options, metadata) {
43
43
  if (!metadata.hasCountry(input.country)) {
44
44
  throw new Error(`Unknown country: ${input.country}`)
45
45
  }
46
- metadata.country(input.country)
46
+ metadata.selectNumberingPlan(input.country)
47
47
  }
48
48
  else if (input.countryCallingCode) {
49
49
  metadata.selectNumberingPlan(input.countryCallingCode)
@@ -128,7 +128,7 @@ function formatNationalNumber(number, carrierCode, formatAs, metadata, options)
128
128
  )
129
129
  }
130
130
 
131
- export function chooseFormatForNumber(availableFormats, nationalNnumber) {
131
+ export function chooseFormatForNumber(availableFormats, nationalNumber) {
132
132
  for (const format of availableFormats) {
133
133
  // Validate leading digits.
134
134
  // The test case for "else path" could be found by searching for
@@ -137,12 +137,12 @@ export function chooseFormatForNumber(availableFormats, nationalNnumber) {
137
137
  // The last leading_digits_pattern is used here, as it is the most detailed
138
138
  const lastLeadingDigitsPattern = format.leadingDigitsPatterns()[format.leadingDigitsPatterns().length - 1]
139
139
  // If leading digits don't match then move on to the next phone number format
140
- if (nationalNnumber.search(lastLeadingDigitsPattern) !== 0) {
140
+ if (nationalNumber.search(lastLeadingDigitsPattern) !== 0) {
141
141
  continue
142
142
  }
143
143
  }
144
144
  // Check that the national number matches the phone number format regular expression
145
- if (matchesEntirely(nationalNnumber, format.pattern())) {
145
+ if (matchesEntirely(nationalNumber, format.pattern())) {
146
146
  return format
147
147
  }
148
148
  }
@@ -67,10 +67,10 @@ export default function(number, from_country, with_formatting, metadata) {
67
67
  // dialled in national format.
68
68
 
69
69
  // Select country for `checkNumberLength()`.
70
- metadata.country(country)
70
+ metadata.selectNumberingPlan(country)
71
71
 
72
72
  if (can_be_internationally_dialled(number) &&
73
- checkNumberLength(number.phone, metadata) !== 'TOO_SHORT') {
73
+ checkNumberLength(number.phone, country, metadata) !== 'TOO_SHORT') {
74
74
  formatted_number = format(number, 'INTERNATIONAL', metadata.metadata)
75
75
  }
76
76
  else {
@@ -1,12 +1,45 @@
1
+ import Metadata from '../metadata.js'
1
2
  import mergeArrays from './mergeArrays.js'
2
3
 
3
- export default function checkNumberLength(nationalNumber, metadata) {
4
- return checkNumberLengthForType(nationalNumber, undefined, metadata)
4
+ export default function checkNumberLength(nationalNumber, country, metadata) {
5
+ return checkNumberLengthForType(nationalNumber, country, undefined, metadata)
5
6
  }
6
7
 
7
- // Checks whether a number is possible for the country based on its length.
8
- // Should only be called for the "new" metadata which has "possible lengths".
9
- export function checkNumberLengthForType(nationalNumber, type, metadata) {
8
+ // Checks whether a number is possible for a certain `country` based on the number length.
9
+ //
10
+ // This function is not supported by metadata generated with ancient versions of
11
+ // `libphonenumber-js` (before version `1.0.18`) which didn't include "possible lengths".
12
+ //
13
+ // There was also a known issue with `checkNumberLength()` function:
14
+ // if a number is possible only in a certain `country` among several `countries`
15
+ // that share the same "country calling code", that function would check
16
+ // the possibility of the phone number only in the "main" `country` for the "country calling code"
17
+ // and would not check if it's actually be possible in the speciifc `country`.
18
+ //
19
+ // For example, "+1310xxxx" numbers are valid in Canada.
20
+ // However, they are not possible in the US due to being too short.
21
+ // Since Canada and the US share the same country calling code — "+1" —
22
+ // `checkNumberLength()` function used to return not "IS_POSSIBLE" for "+1310xxxx" numbers.
23
+ //
24
+ // In such cases, when using "/max" metadata, `isValid()` could output `true`
25
+ // but at the same time `isPossible()` could output `false`, which was contradictory.
26
+ //
27
+ // See https://issuetracker.google.com/issues/335892662 for the discusson in Google's issues.
28
+ //
29
+ // The solution suggested by Google was implemented: an optional `country` argument
30
+ // was added to `checkNumberLength()` function. If present, that `country` will be used
31
+ // to check phone number length for that specific `country` rather than the "main" country
32
+ // for the shared "country calling code".
33
+ //
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.
38
+ if (country) {
39
+ metadata = new Metadata(metadata.metadata)
40
+ metadata.selectNumberingPlan(country)
41
+ }
42
+
10
43
  const type_info = metadata.type(type)
11
44
 
12
45
  // There should always be "<possiblePengths/>" set for every type element.
@@ -17,7 +50,7 @@ export function checkNumberLengthForType(nationalNumber, type, metadata) {
17
50
  // exist at all, there is one possible length (-1) which is guaranteed
18
51
  // not to match the length of any real phone number.
19
52
  let possible_lengths = type_info && type_info.possibleLengths() || metadata.possibleLengths()
20
- // let local_lengths = type_info && type.possibleLengthsLocal() || metadata.possibleLengthsLocal()
53
+ // let local_lengths = type_info && type.possibleLengthsLocal() || metadata.possibleLengthsLocal()
21
54
 
22
55
  // Metadata before version `1.0.18` didn't contain `possible_lengths`.
23
56
  if (!possible_lengths) {
@@ -30,7 +63,7 @@ export function checkNumberLengthForType(nationalNumber, type, metadata) {
30
63
  if (!metadata.type('FIXED_LINE')) {
31
64
  // The rare case has been encountered where no fixedLine data is available
32
65
  // (true for some non-geographic entities), so we just check mobile.
33
- return checkNumberLengthForType(nationalNumber, 'MOBILE', metadata)
66
+ return checkNumberLengthForType(nationalNumber, country, 'MOBILE', metadata)
34
67
  }
35
68
 
36
69
  const mobile_type = metadata.type('MOBILE')