libphonenumber-js 1.9.41 → 1.9.46

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 (77) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/README.md +18 -10
  3. package/build/AsYouType.js +89 -32
  4. package/build/AsYouType.js.map +1 -1
  5. package/build/AsYouType.test.js +220 -0
  6. package/build/AsYouType.test.js.map +1 -1
  7. package/build/PhoneNumber.test.js +9 -9
  8. package/build/PhoneNumber.test.js.map +1 -1
  9. package/build/findNumbers.test.js +23 -23
  10. package/build/findNumbers.test.js.map +1 -1
  11. package/build/getExampleNumber.test.js +4 -4
  12. package/build/getExampleNumber.test.js.map +1 -1
  13. package/build/getNumberType.test.js +3 -3
  14. package/build/getNumberType.test.js.map +1 -1
  15. package/build/helpers/checkNumberLength.test.js +3 -3
  16. package/build/helpers/checkNumberLength.test.js.map +1 -1
  17. package/build/helpers/extractCountryCallingCode.test.js +4 -4
  18. package/build/helpers/extractCountryCallingCode.test.js.map +1 -1
  19. package/build/parsePhoneNumber.test.js +2 -2
  20. package/build/parsePhoneNumber.test.js.map +1 -1
  21. package/bundle/libphonenumber-js.min.js +1 -1
  22. package/bundle/libphonenumber-js.min.js.map +1 -1
  23. package/bundle/libphonenumber-max.js +1 -1
  24. package/bundle/libphonenumber-max.js.map +1 -1
  25. package/bundle/libphonenumber-min.js +1 -1
  26. package/bundle/libphonenumber-min.js.map +1 -1
  27. package/bundle/libphonenumber-mobile.js +1 -1
  28. package/bundle/libphonenumber-mobile.js.map +1 -1
  29. package/core/index.d.ts +1 -0
  30. package/custom.d.ts +1 -0
  31. package/es6/AsYouType.js +89 -32
  32. package/es6/AsYouType.js.map +1 -1
  33. package/es6/AsYouType.test.js +220 -0
  34. package/es6/AsYouType.test.js.map +1 -1
  35. package/es6/PhoneNumber.test.js +1 -1
  36. package/es6/PhoneNumber.test.js.map +1 -1
  37. package/es6/findNumbers.test.js +1 -1
  38. package/es6/findNumbers.test.js.map +1 -1
  39. package/es6/getExampleNumber.test.js +2 -2
  40. package/es6/getExampleNumber.test.js.map +1 -1
  41. package/es6/getNumberType.test.js +1 -1
  42. package/es6/getNumberType.test.js.map +1 -1
  43. package/es6/helpers/checkNumberLength.test.js +1 -1
  44. package/es6/helpers/checkNumberLength.test.js.map +1 -1
  45. package/es6/helpers/extractCountryCallingCode.test.js +1 -1
  46. package/es6/helpers/extractCountryCallingCode.test.js.map +1 -1
  47. package/es6/parsePhoneNumber.test.js +1 -1
  48. package/es6/parsePhoneNumber.test.js.map +1 -1
  49. package/exports/README.md +98 -0
  50. package/exports/examples.mobile.json/index.js +1 -0
  51. package/exports/metadata.max.json/index.js +1 -0
  52. package/exports/metadata.min.json/index.js +1 -0
  53. package/exports/metadata.mobile.json/index.js +1 -0
  54. package/exports/package.exports.json +55 -0
  55. package/index.d.ts +1 -0
  56. package/max/index.commonjs.js +1 -1
  57. package/max/metadata.js +1 -1
  58. package/metadata.full.json +1 -1
  59. package/metadata.full.json.js +1 -1
  60. package/metadata.max.json +1 -0
  61. package/metadata.max.json.d.ts +4 -0
  62. package/metadata.max.json.js +4 -0
  63. package/metadata.min.json +1 -1
  64. package/metadata.min.json.js +1 -1
  65. package/metadata.mobile.json +1 -1
  66. package/metadata.mobile.json.js +1 -1
  67. package/min/index.d.ts +1 -0
  68. package/package.json +4 -2
  69. package/source/AsYouType.js +90 -28
  70. package/source/AsYouType.test.js +223 -0
  71. package/source/PhoneNumber.test.js +1 -1
  72. package/source/findNumbers.test.js +1 -1
  73. package/source/getExampleNumber.test.js +2 -2
  74. package/source/getNumberType.test.js +1 -1
  75. package/source/helpers/checkNumberLength.test.js +1 -1
  76. package/source/helpers/extractCountryCallingCode.test.js +1 -1
  77. package/source/parsePhoneNumber.test.js +1 -1
package/CHANGELOG.md CHANGED
@@ -67,6 +67,16 @@ And edit the README:
67
67
 
68
68
  This changelog [only](https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/16#note_594165443) mentions the changes in the code. See [Google's changelog](https://github.com/google/libphonenumber/blob/master/release_notes.txt) for metadata changes.
69
69
 
70
+ 1.9.45 / xx.01.2022
71
+ ==================
72
+
73
+ * Added `AsYouType.getNumberValue()` function. The function will be [used](https://gitlab.com/catamphetamine/react-phone-number-input/-/issues/113) in `react-phone-number-input` component. Returns the phone number in [`E.164`](https://en.wikipedia.org/wiki/E.164) format. For example, for country `"US"` and input `"(222) 333-4444"` it will return `"+12223334444"`. Will return `undefined` if no digits have been input, or when inputting a phone number in national format and no default country or default "country calling code" have been set.
74
+
75
+ 1.9.42 / 05.11.2021
76
+ ==================
77
+
78
+ * Added a better called alias for `metadata.full.json` — `metadata.max.json`.
79
+
70
80
  1.9.40 / 02.11.2021
71
81
  ==================
72
82
 
package/README.md CHANGED
@@ -283,7 +283,7 @@ findPhoneNumbersInText(`
283
283
 
284
284
  This library provides different "metadata" sets, "metadata" being a list of phone number parsing and formatting rules for all countries. The complete list of those rules is huge, so this library provides a way to optimize bundle size by choosing between `max`, `min`, `mobile` and "custom" metadata:
285
285
 
286
- * `max` — The complete metadata set, is about `145 kilobytes` in size (`libphonenumber-js/metadata.full.json`). Choose this when you need the most strict version of `isValid()`, or if you need to detect phone number type ("fixed line", "mobile", etc).
286
+ * `max` — The complete metadata set, is about `145 kilobytes` in size (`libphonenumber-js/metadata.max.json`). Choose this when you need the most strict version of `isValid()`, or if you need to detect phone number type ("fixed line", "mobile", etc).
287
287
 
288
288
  * `min` — (default) The smallest metadata set, is about `80 kilobytes` in size (`libphonenumber-js/metadata.min.json`). Choose this by default: when you don't need to detect phone number type ("fixed line", "mobile", etc), or when a basic version of `isValid()` is enough. The `min` metadata set doesn't contain the regular expressions for phone number digits validation (via [`.isValid()`](#isvalid)) and detecting phone number type (via [`.getType()`](#gettype)) for most countries. In this case, `.isValid()` still performs some basic phone number validation (for example, checks phone number length), but it doesn't validate phone number digits themselves the way `max` metadata validation does.
289
289
 
@@ -686,7 +686,9 @@ new AsYouType('US').input('2133734') === '(213) 373-4'
686
686
 
687
687
  The formatter instance also provides the following getters:
688
688
 
689
- * `getNumber(): PhoneNumber` — Returns the [`PhoneNumber`](#phonenumber). Will return `undefined` if no [national (significant) number](#national-significant-number) digits have been entered so far, or if no `defaultCountry`/`defaultCallingCode` has been set and the user enters a phone number not in international format.
689
+ * `getNumber(): PhoneNumber?` — Returns the [`PhoneNumber`](#phonenumber). Will return `undefined` if no [national (significant) number](#national-significant-number) digits have been entered so far, or if no `defaultCountry`/`defaultCallingCode` has been set and the user enters a phone number not in international format.
690
+
691
+ * `getNumberValue(): string?` — Returns the phone number in [`E.164`](https://en.wikipedia.org/wiki/E.164) format. For example, for country `"US"` and input `"(222) 333-4444"` it will return `"+12223334444"`. Will return `undefined` if no digits have been input, or when inputting a phone number in national format and no default country or default "country calling code" have been set.
690
692
 
691
693
  * `getChars(): string` — Returns the phone number characters entered by the user: digits and a `+` sign (if present). Returns an empty string if no phone number characters have been input.
692
694
 
@@ -729,7 +731,7 @@ asYouType.getTemplate() === 'xx xxx xxx xxxx'
729
731
 
730
732
  * `isInternational(): boolean` — Returns `true` if the phone number is being input in international format. In other words, returns `true` if and only if the parsed phone number starts with a `"+"`.
731
733
 
732
- * `getCallingCode(): string?` — Returns the ["country calling code"](#country-calling-code) part of the phone number. Returns `undefined` if the number is not being input in international format. Returns "country calling code" for ["non-geographic"](#non-geographic) phone numbering plans too.
734
+ * `getCallingCode(): string?` — Returns the ["country calling code"](#country-calling-code) part of the phone number. Returns `undefined` if the number is not being input in international format, or if no valid "country calling code" has been entered. Supports ["non-geographic"](#non-geographic) phone numbering plans: even though those aren't technically "countries", they have their own "country calling codes" too.
733
735
 
734
736
  * `getCountry(): string?` — Returns a two-letter [country code](#country-code) of the phone number. Returns `undefined` for ["non-geographic"](#non-geographic) phone numbering plans. Returns `undefined` if no phone number has been input yet.
735
737
 
@@ -1285,7 +1287,7 @@ The difference between using `parseNumber()` and `isValidNumber()` for phone num
1285
1287
 
1286
1288
  This is how it is implemented in the original Google's [`libphonenumber`](https://static.javadoc.io/com.googlecode.libphonenumber/libphonenumber/8.9.1/com/google/i18n/phonenumbers/PhoneNumberUtil.html#parse-java.lang.CharSequence-java.lang.String-): `parseNumber()` parses phone numbers and loosely validates them while `isValidNumber()` validates phone numbers precisely (provided the precise regular expressions are included in metadata).
1287
1289
 
1288
- The precise regular expressions aren't included in the default metadata because that would cause the default metadata to grow twice in its size: the complete ("full") metadata size is about 145 kilobytes while the reduced ("default") metadata size is about 77 kilobytes. Hence in the default configuration `isValidNumber()` performs absolutely the same "lite" validation as `parseNumber()`. For enabling extensive phone number validation the simplest way is to import functions from `libphonenumber-js/custom` module and supply them with `libphonenumber-js/metadata.full.json`. For generating custom metadata see the instructions provided in the [Customizing metadata](#customizing-metadata) section of this document.
1290
+ The precise regular expressions aren't included in the default metadata because that would cause the default metadata to grow twice in its size: the complete ("full") metadata size is about 145 kilobytes while the reduced ("default") metadata size is about 77 kilobytes. Hence in the default configuration `isValidNumber()` performs absolutely the same "lite" validation as `parseNumber()`. For enabling extensive phone number validation the simplest way is to import functions from `libphonenumber-js/custom` module and supply them with `libphonenumber-js/metadata.max.json`. For generating custom metadata see the instructions provided in the [Customizing metadata](#customizing-metadata) section of this document.
1289
1291
  </details>
1290
1292
 
1291
1293
  ####
@@ -1319,12 +1321,18 @@ Whenever there's a "business requirement" to validate a phone number that's bein
1319
1321
 
1320
1322
  If you’re trying to build a React component with this library, take a look at [`react-phone-number-input`](https://www.npmjs.com/package/react-phone-number-input).
1321
1323
 
1324
+ <!--
1325
+ ## Node ES Modules
1326
+
1327
+ This package currently [doesn't provide](https://gitlab.com/catamphetamine/libphonenumber-js/-/tree/master/exports) Node 14+ ES Modules exports, but that shouldn't be an issue because Node.js seems to [support](https://gitlab.com/catamphetamine/libphonenumber-js/-/issues/42) importing from "legacy" packages just fine.
1328
+ -->
1329
+
1322
1330
  ## Bug reporting
1323
1331
 
1324
1332
  When reporting an issue one must also provide a link to [Google's `libphonenumber` demo page](https://libphonenumber.appspot.com/) illustrating the expected behaviour. This includes validation, parsing, formatting and "as you type" formatting. For example, for an Australian number `438 331 999` Google's demo [outputs four sections](https://libphonenumber.appspot.com/phonenumberparser?number=438331999&country=AU) — "Parsing Result", "Validation Results", "Formatting Results" and "AsYouTypeFormatter Results". In a bug report, first describe the observed `libphonenumber-js` demo result and then Google's demo result (with a link to it) which must differ from the observed `libphonenumber-js` demo result. If the observed `libphonenumber-js` demo result is the same as Google's demo result and you don't agree with Google's demo result then create an issue in [Google's repo](https://github.com/googlei18n/libphonenumber).
1325
1333
 
1326
1334
  <!--
1327
- Phone number validation bugs should **only** be reported if they appear when using [custom metadata functions](#customizing-metadata) fed with `metadata.full.json` because by default all functions in this library use the reduced metadata set which results in looser validation than the original Google `libphonenumber`'s. The [demo page](https://catamphetamine.gitlab.io/libphonenumber-js/) also uses the reduced metadata set and therefore its validation is also looser than the original Google `libphonenumber`'s.
1335
+ Phone number validation bugs should **only** be reported if they appear when using [custom metadata functions](#customizing-metadata) fed with `metadata.max.json` because by default all functions in this library use the reduced metadata set which results in looser validation than the original Google `libphonenumber`'s. The [demo page](https://catamphetamine.gitlab.io/libphonenumber-js/) also uses the reduced metadata set and therefore its validation is also looser than the original Google `libphonenumber`'s.
1328
1336
 
1329
1337
  There is also a possibility of this library's demo metadata being outdated, or this library's metadata lagging behind Google's (I have to update it manually from time to time due to `ssh-agent` not working properly on Windows).
1330
1338
  -->
@@ -1423,7 +1431,7 @@ Using with custom metadata:
1423
1431
  import { Metadata } from 'libphonenumber-js/core'
1424
1432
 
1425
1433
  import min from 'libphonenumber-js/metadata.min.json'
1426
- // import max from 'libphonenumber-js/metadata.full.json'
1434
+ // import max from 'libphonenumber-js/metadata.max.json'
1427
1435
  // import mobile from 'libphonenumber-js/metadata.mobile.json'
1428
1436
 
1429
1437
  const metadata = new Metadata(min)
@@ -1471,7 +1479,7 @@ import _parsePhoneNumber, {
1471
1479
  AsYouType as _AsYouType
1472
1480
  } from 'libphonenumber-js/core'
1473
1481
 
1474
- import metadata from 'libphonenumber-js/metadata.full.json'
1482
+ import metadata from 'libphonenumber-js/metadata.max.json'
1475
1483
 
1476
1484
  function call(func, _arguments) {
1477
1485
  var args = Array.prototype.slice.call(_arguments)
@@ -1498,7 +1506,7 @@ And for [Common.js](https://auth0.com/blog/javascript-module-systems-showdown/)
1498
1506
 
1499
1507
  ```js
1500
1508
  var core = require('libphonenumber-js/core')
1501
- var metadata = require('libphonenumber-js/metadata.full.json')
1509
+ var metadata = require('libphonenumber-js/metadata.max.json')
1502
1510
 
1503
1511
  function call(func, _arguments) {
1504
1512
  var args = Array.prototype.slice.call(_arguments)
@@ -1545,7 +1553,7 @@ import {
1545
1553
  AsYouType
1546
1554
  } from 'libphonenumber-js/custom'
1547
1555
 
1548
- import metadata from 'libphonenumber-js/metadata.full.json'
1556
+ import metadata from 'libphonenumber-js/metadata.max.json'
1549
1557
 
1550
1558
  parseNumber('+78005553535', metadata)
1551
1559
  formatNumber({ phone: '8005553535', country: 'RU' }, metadata)
@@ -1558,7 +1566,7 @@ And for [Common.js](https://auth0.com/blog/javascript-module-systems-showdown/)
1558
1566
 
1559
1567
  ```js
1560
1568
  var custom = require('libphonenumber-js/custom')
1561
- var metadata = require('libphonenumber-js/metadata.full.json')
1569
+ var metadata = require('libphonenumber-js/metadata.max.json')
1562
1570
 
1563
1571
  exports.parseNumber = function parseNumber() {
1564
1572
  var parameters = Array.prototype.slice.call(arguments)
@@ -191,16 +191,26 @@ function () {
191
191
  return this.state.international;
192
192
  }
193
193
  /**
194
- * Returns the "country calling code" part of the phone number.
195
- * Returns `undefined` if the number is not being input in international format.
196
- * Returns "country calling code" for "non-geographic" phone numbering plans too.
194
+ * Returns the "calling code" part of the phone number when it's being input
195
+ * in an international format.
196
+ * If no valid calling code has been entered so far, returns `undefined`.
197
197
  * @return {string} [callingCode]
198
198
  */
199
199
 
200
200
  }, {
201
201
  key: "getCallingCode",
202
202
  value: function getCallingCode() {
203
- return this.state.callingCode;
203
+ // If the number is being input in national format and some "default calling code"
204
+ // has been passed to `AsYouType` constructor, then `this.state.callingCode`
205
+ // is equal to that "default calling code".
206
+ //
207
+ // If the number is being input in national format and no "default calling code"
208
+ // has been passed to `AsYouType` constructor, then returns `undefined`,
209
+ // even if a "default country" has been passed to `AsYouType` constructor.
210
+ //
211
+ if (this.isInternational()) {
212
+ return this.state.callingCode;
213
+ }
204
214
  } // A legacy alias.
205
215
 
206
216
  }, {
@@ -218,28 +228,33 @@ function () {
218
228
  }, {
219
229
  key: "getCountry",
220
230
  value: function getCountry() {
221
- var _this$state = this.state,
222
- digits = _this$state.digits,
223
- country = _this$state.country; // If no digits have been input yet,
224
- // then `this.country` is the `defaultCountry`.
225
- // Won't return the `defaultCountry` in such case.
231
+ var digits = this.state.digits; // Return `undefined` if no digits have been input yet.
226
232
 
227
- if (!digits) {
228
- return;
233
+ if (digits) {
234
+ return this._getCountry();
229
235
  }
236
+ }
237
+ /**
238
+ * Returns a two-letter country code of the phone number.
239
+ * Returns `undefined` for "non-geographic" phone numbering plans.
240
+ * @return {string} [country]
241
+ */
230
242
 
231
- var countryCode = country;
243
+ }, {
244
+ key: "_getCountry",
245
+ value: function _getCountry() {
246
+ var country = this.state.country;
232
247
  /* istanbul ignore if */
233
248
 
234
249
  if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
235
250
  // `AsYouType.getCountry()` returns `undefined`
236
251
  // for "non-geographic" phone numbering plans.
237
- if (countryCode === '001') {
238
- countryCode = undefined;
252
+ if (country === '001') {
253
+ return;
239
254
  }
240
255
  }
241
256
 
242
- return countryCode;
257
+ return country;
243
258
  }
244
259
  }, {
245
260
  key: "determineTheCountryIfNeeded",
@@ -295,10 +310,10 @@ function () {
295
310
  }, {
296
311
  key: "getNonFormattedNationalNumberWithPrefix",
297
312
  value: function getNonFormattedNationalNumberWithPrefix() {
298
- var _this$state2 = this.state,
299
- nationalSignificantNumber = _this$state2.nationalSignificantNumber,
300
- complexPrefixBeforeNationalSignificantNumber = _this$state2.complexPrefixBeforeNationalSignificantNumber,
301
- nationalPrefix = _this$state2.nationalPrefix;
313
+ var _this$state = this.state,
314
+ nationalSignificantNumber = _this$state.nationalSignificantNumber,
315
+ complexPrefixBeforeNationalSignificantNumber = _this$state.complexPrefixBeforeNationalSignificantNumber,
316
+ nationalPrefix = _this$state.nationalPrefix;
302
317
  var number = nationalSignificantNumber;
303
318
  var prefix = complexPrefixBeforeNationalSignificantNumber || nationalPrefix;
304
319
 
@@ -338,6 +353,51 @@ function () {
338
353
  value: function determineTheCountry() {
339
354
  this.state.setCountry((0, _getCountryByCallingCode["default"])(this.isInternational() ? this.state.callingCode : this.defaultCallingCode, this.state.nationalSignificantNumber, this.metadata));
340
355
  }
356
+ /**
357
+ * Returns a E.164 phone number value for the user's input.
358
+ *
359
+ * For example, for country `"US"` and input `"(222) 333-4444"`
360
+ * it will return `"+12223334444"`.
361
+ *
362
+ * For international phone number input, it will also auto-correct
363
+ * some minor errors such as using a national prefix when writing
364
+ * an international phone number. For example, if the user inputs
365
+ * `"+44 0 7400 000000"` then it will return an auto-corrected
366
+ * `"+447400000000"` phone number value.
367
+ *
368
+ * Will return `undefined` if no digits have been input,
369
+ * or when inputting a phone number in national format and no
370
+ * default country or default "country calling code" have been set.
371
+ *
372
+ * @return {string} [value]
373
+ */
374
+
375
+ }, {
376
+ key: "getNumberValue",
377
+ value: function getNumberValue() {
378
+ var _this$state2 = this.state,
379
+ digits = _this$state2.digits,
380
+ callingCode = _this$state2.callingCode,
381
+ country = _this$state2.country,
382
+ nationalSignificantNumber = _this$state2.nationalSignificantNumber; // Will return `undefined` if no digits have been input.
383
+
384
+ if (!digits) {
385
+ return;
386
+ }
387
+
388
+ if (this.isInternational()) {
389
+ if (callingCode) {
390
+ return '+' + callingCode + nationalSignificantNumber;
391
+ } else {
392
+ return '+' + digits;
393
+ }
394
+ } else {
395
+ if (country || callingCode) {
396
+ var callingCode_ = country ? this.metadata.countryCallingCode() : callingCode;
397
+ return '+' + callingCode_ + nationalSignificantNumber;
398
+ }
399
+ }
400
+ }
341
401
  /**
342
402
  * Returns an instance of `PhoneNumber` class.
343
403
  * Will return `undefined` if no national (significant) number
@@ -350,25 +410,22 @@ function () {
350
410
  value: function getNumber() {
351
411
  var _this$state3 = this.state,
352
412
  nationalSignificantNumber = _this$state3.nationalSignificantNumber,
353
- carrierCode = _this$state3.carrierCode;
413
+ carrierCode = _this$state3.carrierCode,
414
+ callingCode = _this$state3.callingCode; // `this._getCountry()` is basically same as `this.state.country`
415
+ // with the only change that it return `undefined` in case of a
416
+ // "non-geographic" numbering plan instead of `"001"` "internal use" value.
354
417
 
355
- if (this.isInternational()) {
356
- if (!this.state.callingCode) {
357
- return;
358
- }
359
- } else {
360
- if (!this.state.country && !this.defaultCallingCode) {
361
- return;
362
- }
363
- }
418
+ var country = this._getCountry();
364
419
 
365
420
  if (!nationalSignificantNumber) {
366
421
  return;
367
422
  }
368
423
 
369
- var countryCode = this.getCountry();
370
- var callingCode = this.getCountryCallingCode() || this.defaultCallingCode;
371
- var phoneNumber = new _PhoneNumber["default"](countryCode || callingCode, nationalSignificantNumber, this.metadata.metadata);
424
+ if (!country && !callingCode) {
425
+ return;
426
+ }
427
+
428
+ var phoneNumber = new _PhoneNumber["default"](country || callingCode, nationalSignificantNumber, this.metadata.metadata);
372
429
 
373
430
  if (carrierCode) {
374
431
  phoneNumber.carrierCode = carrierCode;
@@ -1 +1 @@
1
- {"version":3,"sources":["../source/AsYouType.js"],"names":["USE_NON_GEOGRAPHIC_COUNTRY_CODE","AsYouType","optionsOrDefaultCountry","metadata","Metadata","getCountryAndCallingCode","defaultCountry","defaultCallingCode","reset","hasCountry","undefined","isNonGeographicCallingCode","text","parser","input","state","digits","justLeadingPlus","formattedOutput","determineTheCountryIfNeeded","nationalSignificantNumber","formatter","narrowDownMatchingFormats","formattedNationalNumber","hasSelectedNumberingPlan","format","reExtractNationalSignificantNumber","nationalDigits","getNationalDigits","getFullNumber","getNonFormattedNumber","AsYouTypeState","onCountryChange","country","onCallingCodeChange","callingCode","selectNumberingPlan","numberingPlan","AsYouTypeFormatter","AsYouTypeParser","onNationalSignificantNumberChange","international","getCallingCode","countryCode","isCountryCallingCodeAmbiguous","determineTheCountry","isInternational","prefix","getInternationalPrefixBeforeCountryCallingCode","spacing","getDigitsWithoutInternationalPrefix","complexPrefixBeforeNationalSignificantNumber","nationalPrefix","number","nationalSignificantNumberMatchesInput","getNonFormattedNationalNumberWithPrefix","replace","DIGIT_PLACEHOLDER","countryCodes","getCountryCodesForCallingCode","length","setCountry","carrierCode","getCountry","getCountryCallingCode","phoneNumber","PhoneNumber","getNumber","isPossible","isValid","getTemplate","getNonFormattedTemplate"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AACA;;AACA;;AACA;;;;;;;;;;;;;;;;;;;;;;AAEA,IAAMA,+BAA+B,GAAG,KAAxC;;IAEqBC,S;;;AACpB;;;;AAIA,qBAAYC,uBAAZ,EAAqCC,QAArC,EAA+C;AAAA;;AAC9C,SAAKA,QAAL,GAAgB,IAAIC,oBAAJ,CAAaD,QAAb,CAAhB;;AAD8C,gCAED,KAAKE,wBAAL,CAA8BH,uBAA9B,CAFC;AAAA;AAAA,QAEvCI,cAFuC;AAAA,QAEvBC,kBAFuB;;AAG9C,SAAKD,cAAL,GAAsBA,cAAtB;AACA,SAAKC,kBAAL,GAA0BA,kBAA1B;AACA,SAAKC,KAAL;AACA;;;;6CAEwBN,uB,EAAyB;AACjD;AACA,UAAII,cAAJ;AACA,UAAIC,kBAAJ,CAHiD,CAIjD;;AACA,UAAIL,uBAAJ,EAA6B;AAC5B,YAAI,QAAOA,uBAAP,MAAmC,QAAvC,EAAiD;AAChDI,UAAAA,cAAc,GAAGJ,uBAAuB,CAACI,cAAzC;AACAC,UAAAA,kBAAkB,GAAGL,uBAAuB,CAACK,kBAA7C;AACA,SAHD,MAGO;AACND,UAAAA,cAAc,GAAGJ,uBAAjB;AACA;AACD;;AACD,UAAII,cAAc,IAAI,CAAC,KA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;;;8BAKU;AACT,UAAMH,WAAW,GAAG,KAAKE,SAAL,EAApB;;AACA,UAAI,CAACF,WAAL,EAAkB;AACjB,eAAO,KAAP;AACA;;AACD,aAAOA,WAAW,CAACI,OAAZ,EAAP;AACA;AAED;;;;;;;;wCAKoB;AACnB,aAAO,KAAKtD,KAAL,CAAWK,yBAAlB;AACA;AAED;;;;;;;+BAIW;AACV,aAAO,CAAC,KAAKL,KAAL,CAAW0B,aAAX,GAA2B,GAA3B,GAAiC,EAAlC,IAAwC,KAAK1B,KAAL,CAAWC,MAA1D;AACA;AAED;;;;;;;kCAIc;AACb,aAAO,KAAKK,SAAL,CAAeiD,WAAf,CAA2B,KAAKvD,KAAhC,KAA0C,KAAKwD,uBAAL,EAA1C,IAA4E,EAAnF;AACA","sourcesContent":["import Metadata from './metadata'\r\nimport PhoneNumber from './PhoneNumber'\r\nimport AsYouTypeState from './AsYouTypeState'\r\nimport AsYouTypeFormatter, { DIGIT_PLACEHOLDER } from './AsYouTypeFormatter'\r\nimport AsYouTypeParser, { extractFormattedDigitsAndPlus } from './AsYouTypeParser'\r\nimport getCountryByCallingCode from './helpers/getCountryByCallingCode'\r\n\r\nconst USE_NON_GEOGRAPHIC_COUNTRY_CODE = false\r\n\r\nexport default class AsYouType {\r\n\t/**\r\n\t * @param {(string|object)?} [optionsOrDefaultCountry] - The default country used for parsing non-international phone numbers. Can also be an `options` object.\r\n\t * @param {Object} metadata\r\n\t */\r\n\tconstructor(optionsOrDefaultCountry, metadata) {\r\n\t\tthis.metadata = new Metadata(metadata)\r\n\t\tconst [defaultCountry, defaultCallingCode] = this.getCountryAndCallingCode(optionsOrDefaultCountry)\r\n\t\tthis.defaultCountry = defaultCountry\r\n\t\tthis.defaultCallingCode = defaultCallingCode\r\n\t\tthis.reset()\r\n\t}\r\n\r\n\tgetCountryAndCallingCode(optionsOrDefaultCountry) {\r\n\t\t// Set `defaultCountry` and `defaultCallingCode` options.\r\n\t\tlet defaultCountry\r\n\t\tlet defaultCallingCode\r\n\t\t// Turns out `null` also has type \"object\". Weird.\r\n\t\tif (optionsOrDefaultCountry) {\r\n\t\t\tif (typeof optionsOrDefaultCountry === 'object') {\r\n\t\t\t\tdefaultCountry = optionsOrDefaultCountry.defaultCountry\r\n\t\t\t\tdefaultCallingCode = optionsOrDefaultCountry.defaultCallingCode\r\n\t\t\t} else {\r\n\t\t\t\tdefaultCountry = optionsOrDefaultCountry\r\n\t\t\t}\r\n\t\t}\r\n\t\tif (defaultCountry && !this.metadata.hasCountry(defaultCountry)) {\r\n\t\t\tdefaultCountry = undefined\r\n\t\t}\r\n\t\tif (defaultCallingCode) {\r\n\t\t\t/* istanbul ignore if */\r\n\t\t\tif (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {\r\n\t\t\t\tif (this.metadata.isNonGeographicCallingCode(defaultCallingCode)) {\r\n\t\t\t\t\tdefaultCountry = '001'\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn [defaultCountry, defaultCallingCode]\r\n\t}\r\n\r\n\t/**\r\n\t * Inputs \"next\" phone number characters.\r\n\t * @param {string} text\r\n\t * @return {string} Formatted phone number characters that have been input so far.\r\n\t */\r\n\tinput(text) {\r\n\t\tconst {\r\n\t\t\tdigits,\r\n\t\t\tjustLeadingPlus\r\n\t\t} = this.parser.input(text, this.state)\r\n\t\tif (justLeadingPlus) {\r\n\t\t\tthis.formattedOutput = '+'\r\n\t\t} else if (digits) {\r\n\t\t\tthis.determineTheCountryIfNeeded()\r\n\t\t\t// Match the available formats by the currently available leading digits.\r\n\t\t\tif (this.state.nationalSignificantNumber) {\r\n\t\t\t\tthis.formatter.narrowDownMatchingFormats(this.state)\r\n\t\t\t}\r\n\t\t\tlet formattedNationalNumber\r\n\t\t\tif (this.metadata.hasSelectedNumberingPlan()) {\r\n\t\t\t\tformattedNationalNumber = this.formatter.format(digits, this.state)\r\n\t\t\t}\r\n\t\t\tif (formattedNationalNumber === undefined) {\r\n\t\t\t\t// See if another national (significant) number could be re-extracted.\r\n\t\t\t\tif (this.parser.reExtractNationalSignificantNumber(this.state)) {\r\n\t\t\t\t\tthis.determineTheCountryIfNeeded()\r\n\t\t\t\t\t// If it could, then re-try formatting the new national (significant) number.\r\n\t\t\t\t\tconst nationalDigits = this.state.getNationalDigits()\r\n\t\t\t\t\tif (nationalDigits) {\r\n\t\t\t\t\t\tformattedNationalNumber = this.formatter.format(nationalDigits, this.state)\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tthis.formattedOutput = formattedNationalNumber\r\n\t\t\t\t? this.getFullNumber(formattedNationalNumber)\r\n\t\t\t\t: this.getNonFormattedNumber()\r\n\t\t}\r\n\t\treturn this.formattedOutput\r\n\t}\r\n\r\n\treset() {\r\n\t\tthis.state = new AsYouTypeState({\r\n\t\t\tonCountryChange: (country) => {\r\n\t\t\t\t// Before version `1.6.0`, the official `AsYouType` formatter API\r\n\t\t\t\t// included the `.country` property of an `AsYouType` instance.\r\n\t\t\t\t// Since that property (along with the others) have been moved to\r\n\t\t\t\t// `this.state`, `this.country` property is emulated for compatibility\r\n\t\t\t\t// with the old versions.\r\n\t\t\t\tthis.country = country\r\n\t\t\t},\r\n\t\t\tonCallingCodeChange: (country, callingCode) => {\r\n\t\t\t\tthis.metadata.selectNumberingPlan(country, callingCode)\r\n\t\t\t\tthis.formatter.reset(this.metadata.numberingPlan, this.state)\r\n\t\t\t\tthis.parser.reset(this.metadata.numberingPlan)\r\n\t\t\t}\r\n\t\t})\r\n\t\tthis.formatter = new AsYouTypeFormatter({\r\n\t\t\tstate: this.state,\r\n\t\t\tmetadata: this.metadata\r\n\t\t})\r\n\t\tthis.parser = new AsYouTypeParser({\r\n\t\t\tdefaultCountry: this.defaultCountry,\r\n\t\t\tdefaultCallingCode: this.defaultCallingCode,\r\n\t\t\tmetadata: this.metadata,\r\n\t\t\tstate: this.state,\r\n\t\t\tonNationalSignificantNumberChange: () => {\r\n\t\t\t\tthis.determineTheCountryIfNeeded()\r\n\t\t\t\tthis.formatter.reset(this.metadata.numberingPlan, this.state)\r\n\t\t\t}\r\n\t\t})\r\n\t\tthis.state.reset(this.defaultCountry, this.defaultCallingCode)\r\n\t\tthis.formattedOutput = ''\r\n\t\treturn this\r\n\t}\r\n\r\n\t/**\r\n\t * Returns `true` if the phone number is being input in international format.\r\n\t * In other words, returns `true` if and only if the parsed phone number starts with a `\"+\"`.\r\n\t * @return {boolean}\r\n\t */\r\n\tisInternational() {\r\n\t\treturn this.state.international\r\n\t}\r\n\r\n\t/**\r\n\t * Returns the \"country calling code\" part of the phone number.\r\n\t * Returns `undefined` if the number is not being input in international format.\r\n\t * Returns \"country calling code\" for \"non-geographic\" phone numbering plans too.\r\n\t * @return {string} [callingCode]\r\n\t */\r\n\tgetCallingCode() {\r\n\t\treturn this.state.callingCode\r\n\t}\r\n\r\n\t// A legacy alias.\r\n\tgetCountryCallingCode() {\r\n\t\treturn this.getCallingCode()\r\n\t}\r\n\r\n\t/**\r\n\t * Returns a two-letter country code of the phone number.\r\n\t * Returns `undefined` for \"non-geographic\" phone numbering plans.\r\n\t * Returns `undefined` if no phone number has been input yet.\r\n\t * @return {string} [country]\r\n\t */\r\n\tgetCountry() {\r\n\t\tconst { digits, country } = this.state\r\n\t\t// If no digits have been input yet,\r\n\t\t// then `this.country` is the `defaultCountry`.\r\n\t\t// Won't return the `defaultCountry` in such case.\r\n\t\tif (!digits) {\r\n\t\t\treturn\r\n\t\t}\r\n\t\tlet countryCode = country\r\n\t\t/* istanbul ignore if */\r\n\t\tif (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {\r\n\t\t\t// `AsYouType.getCountry()` returns `undefined`\r\n\t\t\t// for \"non-geographic\" phone numbering plans.\r\n\t\t\tif (countryCode === '001') {\r\n\t\t\t\tcountryCode = undefined\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn countryCode\r\n\t}\r\n\r\n\tdetermineTheCountryIfNeeded() {\r\n\t\t// Suppose a user enters a phone number in international format,\r\n\t\t// and there're several countries corresponding to that country calling code,\r\n\t\t// and a country has been derived from the number, and then\r\n\t\t// a user enters one more digit and the number is no longer\r\n\t\t// valid for the derived country, so the country should be re-derived\r\n\t\t// on every new digit in those cases.\r\n\t\t//\r\n\t\t// If the phone number is being input in national format,\r\n\t\t// then it could be a case when `defaultCountry` wasn't specified\r\n\t\t// when creating `AsYouType` instance, and just `defaultCallingCode` was specified,\r\n\t\t// and that \"calling code\" could correspond to a \"non-geographic entity\",\r\n\t\t// or there could be several countries corresponding to that country calling code.\r\n\t\t// In those cases, `this.country` is `undefined` and should be derived\r\n\t\t// from the number. Again, if country calling code is ambiguous, then\r\n\t\t// `this.country` should be re-derived with each new digit.\r\n\t\t//\r\n\t\tif (!this.state.country || this.isCountryCallingCodeAmbiguous()) {\r\n\t\t\tthis.determineTheCountry()\r\n\t\t}\r\n\t}\r\n\r\n\t// Prepends `+CountryCode ` in case of an international phone number\r\n\tgetFullNumber(formattedNationalNumber) {\r\n\t\tif (this.isInternational()) {\r\n\t\t\tconst prefix = (text) => this.formatter.getInternationalPrefixBeforeCountryCallingCode(this.state, {\r\n\t\t\t\tspacing: text ? true : false\r\n\t\t\t}) + text\r\n\t\t\tconst { callingCode } = this.state\r\n\t\t\tif (!callingCode) {\r\n\t\t\t\treturn prefix(`${this.state.getDigitsWithoutInternationalPrefix()}`)\r\n\t\t\t}\r\n\t\t\tif (!formattedNationalNumber) {\r\n\t\t\t\treturn prefix(callingCode)\r\n\t\t\t}\r\n\t\t\treturn prefix(`${callingCode} ${formattedNationalNumber}`)\r\n\t\t}\r\n\t\treturn formattedNationalNumber\r\n\t}\r\n\r\n\tgetNonFormattedNationalNumberWithPrefix() {\r\n\t\tconst {\r\n\t\t\tnationalSignificantNumber,\r\n\t\t\tcomplexPrefixBeforeNationalSignificantNumber,\r\n\t\t\tnationalPrefix\r\n\t\t} = this.state\r\n\t\tlet number = nationalSignificantNumber\r\n\t\tconst prefix = complexPrefixBeforeNationalSignificantNumber || nationalPrefix\r\n\t\tif (prefix) {\r\n\t\t\tnumber = prefix + number\r\n\t\t}\r\n\t\treturn number\r\n\t}\r\n\r\n\tgetNonFormattedNumber() {\r\n\t\tconst { nationalSignificantNumberMatchesInput } = this.state\r\n\t\treturn this.getFullNumber(\r\n\t\t\tnationalSignificantNumberMatchesInput\r\n\t\t\t\t? this.getNonFormattedNationalNumberWithPrefix()\r\n\t\t\t\t: this.state.getNationalDigits()\r\n\t\t)\r\n\t}\r\n\r\n\tgetNonFormattedTemplate() {\r\n\t\tconst number = this.getNonFormattedNumber()\r\n\t\tif (number) {\r\n\t\t\treturn number.replace(/[\\+\\d]/g, DIGIT_PLACEHOLDER)\r\n\t\t}\r\n\t}\r\n\r\n\tisCountryCallingCodeAmbiguous() {\r\n\t\tconst { callingCode } = this.state\r\n\t\tconst countryCodes = this.metadata.getCountryCodesForCallingCode(callingCode)\r\n\t\treturn countryCodes && countryCodes.length > 1\r\n\t}\r\n\r\n\t// Determines the country of the phone number\r\n\t// entered so far based on the country phone code\r\n\t// and the national phone number.\r\n\tdetermineTheCountry() {\r\n\t\tthis.state.setCountry(getCountryByCallingCode(\r\n\t\t\tthis.isInternational() ? this.state.callingCode : this.defaultCallingCode,\r\n\t\t\tthis.state.nationalSignificantNumber,\r\n\t\t\tthis.metadata\r\n\t\t))\r\n\t}\r\n\r\n\t/**\r\n\t * Returns an instance of `PhoneNumber` class.\r\n\t * Will return `undefined` if no national (significant) number\r\n\t * digits have been entered so far, or if no `defaultCountry` has been\r\n\t * set and the user enters a phone number not in international format.\r\n\t */\r\n\tgetNumber() {\r\n\t\tlet {\r\n\t\t\tnationalSignificantNumber,\r\n\t\t\tcarrierCode\r\n\t\t} = this.state\r\n\t\tif (this.isInternational()) {\r\n\t\t\tif (!this.state.callingCode) {\r\n\t\t\t\treturn\r\n\t\t\t}\r\n\t\t} else {\r\n\t\t\tif (!this.state.country && !this.defaultCallingCode) {\r\n\t\t\t\treturn\r\n\t\t\t}\r\n\t\t}\r\n\t\tif (!nationalSignificantNumber) {\r\n\t\t\treturn\r\n\t\t}\r\n\t\tconst countryCode = this.getCountry()\r\n\t\tconst callingCode = this.getCountryCallingCode() || this.defaultCallingCode\r\n\t\tconst phoneNumber = new PhoneNumber(\r\n\t\t\tcountryCode || callingCode,\r\n\t\t\tnationalSignificantNumber,\r\n\t\t\tthis.metadata.metadata\r\n\t\t)\r\n\t\tif (carrierCode) {\r\n\t\t\tphoneNumber.carrierCode = carrierCode\r\n\t\t}\r\n\t\t// Phone number extensions are not supported by \"As You Type\" formatter.\r\n\t\treturn phoneNumber\r\n\t}\r\n\r\n\t/**\r\n\t * Returns `true` if the phone number is \"possible\".\r\n\t * Is just a shortcut for `PhoneNumber.isPossible()`.\r\n\t * @return {boolean}\r\n\t */\r\n\tisPossible() {\r\n\t\tconst phoneNumber = this.getNumber()\r\n\t\tif (!phoneNumber) {\r\n\t\t\treturn false\r\n\t\t}\r\n\t\treturn phoneNumber.isPossible()\r\n\t}\r\n\r\n\t/**\r\n\t * Returns `true` if the phone number is \"valid\".\r\n\t * Is just a shortcut for `PhoneNumber.isValid()`.\r\n\t * @return {boolean}\r\n\t */\r\n\tisValid() {\r\n\t\tconst phoneNumber = this.getNumber()\r\n\t\tif (!phoneNumber) {\r\n\t\t\treturn false\r\n\t\t}\r\n\t\treturn phoneNumber.isValid()\r\n\t}\r\n\r\n\t/**\r\n\t * @deprecated\r\n\t * This method is used in `react-phone-number-input/source/input-control.js`\r\n\t * in versions before `3.0.16`.\r\n\t */\r\n\tgetNationalNumber() {\r\n\t\treturn this.state.nationalSignificantNumber\r\n\t}\r\n\r\n\t/**\r\n\t * Returns the phone number characters entered by the user.\r\n\t * @return {string}\r\n\t */\r\n\tgetChars() {\r\n\t\treturn (this.state.international ? '+' : '') + this.state.digits\r\n\t}\r\n\r\n\t/**\r\n\t * Returns the template for the formatted phone number.\r\n\t * @return {string}\r\n\t */\r\n\tgetTemplate() {\r\n\t\treturn this.formatter.getTemplate(this.state) || this.getNonFormattedTemplate() || ''\r\n\t}\r\n}"],"file":"AsYouType.js"}
1
+ {"version":3,"sources":["../source/AsYouType.js"],"names":["USE_NON_GEOGRAPHIC_COUNTRY_CODE","AsYouType","optionsOrDefaultCountry","metadata","Metadata","getCountryAndCallingCode","defaultCountry","defaultCallingCode","reset","hasCountry","undefined","isNonGeographicCallingCode","text","parser","input","state","digits","justLeadingPlus","formattedOutput","determineTheCountryIfNeeded","nationalSignificantNumber","formatter","narrowDownMatchingFormats","formattedNationalNumber","hasSelectedNumberingPlan","format","reExtractNationalSignificantNumber","nationalDigits","getNationalDigits","getFullNumber","getNonFormattedNumber","AsYouTypeState","onCountryChange","country","onCallingCodeChange","callingCode","selectNumberingPlan","numberingPlan","AsYouTypeFormatter","AsYouTypeParser","onNationalSignificantNumberChange","international","isInternational","getCallingCode","_getCountry","isCountryCallingCodeAmbiguous","determineTheCountry","prefix","getInternationalPrefixBeforeCountryCallingCode","spacing","getDigitsWithoutInternationalPrefix","complexPrefixBeforeNationalSignificantNumber","nationalPrefix","number","nationalSignificantNumberMatchesInput","getNonFormattedNationalNumberWithPrefix","replace","DIGIT_PLACEHOLDER","countryCodes","getCountryCodesForCallingCode","length","setCountry","callingCode_","countryCallingCode","carrierCode","phoneNumber","PhoneNumber","getNumber","isPossible","isValid","getTemplate","getNonFormattedTemplate"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AACA;;AACA;;AACA;;;;;;;;;;;;;;;;;;;;;;AAEA,IAAMA,+BAA+B,GAAG,KAAxC;;IAEqBC,S;;;AACpB;;;;AAIA,qBAAYC,uBAAZ,EAAqCC,QAArC,EAA+C;AAAA;;AAC9C,SAAKA,QAAL,GAAgB,IAAIC,oBAAJ,CAAaD,QAAb,CAAhB;;AAD8C,gCAED,KAAKE,wBAAL,CAA8BH,uBAA9B,CAFC;AAAA;AAAA,QAEvCI,cAFuC;AAAA,QAEvBC,kBAFuB;;AAG9C,SAAKD,cAAL,GAAsBA,cAAtB;AACA,SAAKC,kBAAL,GAA0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,CAAWC,MAA1D;AACA;AAED;;;;;;;kCAIc;AACb,aAAO,KAAKK,SAAL,CAAeiD,WAAf,CAA2B,KAAKvD,KAAhC,KAA0C,KAAKwD,uBAAL,EAA1C,IAA4E,EAAnF;AACA","sourcesContent":["import Metadata from './metadata'\r\nimport PhoneNumber from './PhoneNumber'\r\nimport AsYouTypeState from './AsYouTypeState'\r\nimport AsYouTypeFormatter, { DIGIT_PLACEHOLDER } from './AsYouTypeFormatter'\r\nimport AsYouTypeParser, { extractFormattedDigitsAndPlus } from './AsYouTypeParser'\r\nimport getCountryByCallingCode from './helpers/getCountryByCallingCode'\r\n\r\nconst USE_NON_GEOGRAPHIC_COUNTRY_CODE = false\r\n\r\nexport default class AsYouType {\r\n\t/**\r\n\t * @param {(string|object)?} [optionsOrDefaultCountry] - The default country used for parsing non-international phone numbers. Can also be an `options` object.\r\n\t * @param {Object} metadata\r\n\t */\r\n\tconstructor(optionsOrDefaultCountry, metadata) {\r\n\t\tthis.metadata = new Metadata(metadata)\r\n\t\tconst [defaultCountry, defaultCallingCode] = this.getCountryAndCallingCode(optionsOrDefaultCountry)\r\n\t\tthis.defaultCountry = defaultCountry\r\n\t\tthis.defaultCallingCode = defaultCallingCode\r\n\t\tthis.reset()\r\n\t}\r\n\r\n\tgetCountryAndCallingCode(optionsOrDefaultCountry) {\r\n\t\t// Set `defaultCountry` and `defaultCallingCode` options.\r\n\t\tlet defaultCountry\r\n\t\tlet defaultCallingCode\r\n\t\t// Turns out `null` also has type \"object\". Weird.\r\n\t\tif (optionsOrDefaultCountry) {\r\n\t\t\tif (typeof optionsOrDefaultCountry === 'object') {\r\n\t\t\t\tdefaultCountry = optionsOrDefaultCountry.defaultCountry\r\n\t\t\t\tdefaultCallingCode = optionsOrDefaultCountry.defaultCallingCode\r\n\t\t\t} else {\r\n\t\t\t\tdefaultCountry = optionsOrDefaultCountry\r\n\t\t\t}\r\n\t\t}\r\n\t\tif (defaultCountry && !this.metadata.hasCountry(defaultCountry)) {\r\n\t\t\tdefaultCountry = undefined\r\n\t\t}\r\n\t\tif (defaultCallingCode) {\r\n\t\t\t/* istanbul ignore if */\r\n\t\t\tif (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {\r\n\t\t\t\tif (this.metadata.isNonGeographicCallingCode(defaultCallingCode)) {\r\n\t\t\t\t\tdefaultCountry = '001'\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn [defaultCountry, defaultCallingCode]\r\n\t}\r\n\r\n\t/**\r\n\t * Inputs \"next\" phone number characters.\r\n\t * @param {string} text\r\n\t * @return {string} Formatted phone number characters that have been input so far.\r\n\t */\r\n\tinput(text) {\r\n\t\tconst {\r\n\t\t\tdigits,\r\n\t\t\tjustLeadingPlus\r\n\t\t} = this.parser.input(text, this.state)\r\n\t\tif (justLeadingPlus) {\r\n\t\t\tthis.formattedOutput = '+'\r\n\t\t} else if (digits) {\r\n\t\t\tthis.determineTheCountryIfNeeded()\r\n\t\t\t// Match the available formats by the currently available leading digits.\r\n\t\t\tif (this.state.nationalSignificantNumber) {\r\n\t\t\t\tthis.formatter.narrowDownMatchingFormats(this.state)\r\n\t\t\t}\r\n\t\t\tlet formattedNationalNumber\r\n\t\t\tif (this.metadata.hasSelectedNumberingPlan()) {\r\n\t\t\t\tformattedNationalNumber = this.formatter.format(digits, this.state)\r\n\t\t\t}\r\n\t\t\tif (formattedNationalNumber === undefined) {\r\n\t\t\t\t// See if another national (significant) number could be re-extracted.\r\n\t\t\t\tif (this.parser.reExtractNationalSignificantNumber(this.state)) {\r\n\t\t\t\t\tthis.determineTheCountryIfNeeded()\r\n\t\t\t\t\t// If it could, then re-try formatting the new national (significant) number.\r\n\t\t\t\t\tconst nationalDigits = this.state.getNationalDigits()\r\n\t\t\t\t\tif (nationalDigits) {\r\n\t\t\t\t\t\tformattedNationalNumber = this.formatter.format(nationalDigits, this.state)\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tthis.formattedOutput = formattedNationalNumber\r\n\t\t\t\t? this.getFullNumber(formattedNationalNumber)\r\n\t\t\t\t: this.getNonFormattedNumber()\r\n\t\t}\r\n\t\treturn this.formattedOutput\r\n\t}\r\n\r\n\treset() {\r\n\t\tthis.state = new AsYouTypeState({\r\n\t\t\tonCountryChange: (country) => {\r\n\t\t\t\t// Before version `1.6.0`, the official `AsYouType` formatter API\r\n\t\t\t\t// included the `.country` property of an `AsYouType` instance.\r\n\t\t\t\t// Since that property (along with the others) have been moved to\r\n\t\t\t\t// `this.state`, `this.country` property is emulated for compatibility\r\n\t\t\t\t// with the old versions.\r\n\t\t\t\tthis.country = country\r\n\t\t\t},\r\n\t\t\tonCallingCodeChange: (country, callingCode) => {\r\n\t\t\t\tthis.metadata.selectNumberingPlan(country, callingCode)\r\n\t\t\t\tthis.formatter.reset(this.metadata.numberingPlan, this.state)\r\n\t\t\t\tthis.parser.reset(this.metadata.numberingPlan)\r\n\t\t\t}\r\n\t\t})\r\n\t\tthis.formatter = new AsYouTypeFormatter({\r\n\t\t\tstate: this.state,\r\n\t\t\tmetadata: this.metadata\r\n\t\t})\r\n\t\tthis.parser = new AsYouTypeParser({\r\n\t\t\tdefaultCountry: this.defaultCountry,\r\n\t\t\tdefaultCallingCode: this.defaultCallingCode,\r\n\t\t\tmetadata: this.metadata,\r\n\t\t\tstate: this.state,\r\n\t\t\tonNationalSignificantNumberChange: () => {\r\n\t\t\t\tthis.determineTheCountryIfNeeded()\r\n\t\t\t\tthis.formatter.reset(this.metadata.numberingPlan, this.state)\r\n\t\t\t}\r\n\t\t})\r\n\t\tthis.state.reset(this.defaultCountry, this.defaultCallingCode)\r\n\t\tthis.formattedOutput = ''\r\n\t\treturn this\r\n\t}\r\n\r\n\t/**\r\n\t * Returns `true` if the phone number is being input in international format.\r\n\t * In other words, returns `true` if and only if the parsed phone number starts with a `\"+\"`.\r\n\t * @return {boolean}\r\n\t */\r\n\tisInternational() {\r\n\t\treturn this.state.international\r\n\t}\r\n\r\n\t/**\r\n\t * Returns the \"calling code\" part of the phone number when it's being input\r\n\t * in an international format.\r\n\t * If no valid calling code has been entered so far, returns `undefined`.\r\n\t * @return {string} [callingCode]\r\n\t */\r\n\tgetCallingCode() {\r\n\t\t // If the number is being input in national format and some \"default calling code\"\r\n\t\t // has been passed to `AsYouType` constructor, then `this.state.callingCode`\r\n\t\t // is equal to that \"default calling code\".\r\n\t\t //\r\n\t\t // If the number is being input in national format and no \"default calling code\"\r\n\t\t // has been passed to `AsYouType` constructor, then returns `undefined`,\r\n\t\t // even if a \"default country\" has been passed to `AsYouType` constructor.\r\n\t\t //\r\n\t\tif (this.isInternational()) {\r\n\t\t\treturn this.state.callingCode\r\n\t\t}\r\n\t}\r\n\r\n\t// A legacy alias.\r\n\tgetCountryCallingCode() {\r\n\t\treturn this.getCallingCode()\r\n\t}\r\n\r\n\t/**\r\n\t * Returns a two-letter country code of the phone number.\r\n\t * Returns `undefined` for \"non-geographic\" phone numbering plans.\r\n\t * Returns `undefined` if no phone number has been input yet.\r\n\t * @return {string} [country]\r\n\t */\r\n\tgetCountry() {\r\n\t\tconst { digits } = this.state\r\n\t\t// Return `undefined` if no digits have been input yet.\r\n\t\tif (digits) {\r\n\t\t\treturn this._getCountry()\r\n\t\t}\r\n\t}\r\n\r\n\t/**\r\n\t * Returns a two-letter country code of the phone number.\r\n\t * Returns `undefined` for \"non-geographic\" phone numbering plans.\r\n\t * @return {string} [country]\r\n\t */\r\n\t_getCountry() {\r\n\t\tconst { country } = this.state\r\n\t\t/* istanbul ignore if */\r\n\t\tif (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {\r\n\t\t\t// `AsYouType.getCountry()` returns `undefined`\r\n\t\t\t// for \"non-geographic\" phone numbering plans.\r\n\t\t\tif (country === '001') {\r\n\t\t\t\treturn\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn country\r\n\t}\r\n\r\n\tdetermineTheCountryIfNeeded() {\r\n\t\t// Suppose a user enters a phone number in international format,\r\n\t\t// and there're several countries corresponding to that country calling code,\r\n\t\t// and a country has been derived from the number, and then\r\n\t\t// a user enters one more digit and the number is no longer\r\n\t\t// valid for the derived country, so the country should be re-derived\r\n\t\t// on every new digit in those cases.\r\n\t\t//\r\n\t\t// If the phone number is being input in national format,\r\n\t\t// then it could be a case when `defaultCountry` wasn't specified\r\n\t\t// when creating `AsYouType` instance, and just `defaultCallingCode` was specified,\r\n\t\t// and that \"calling code\" could correspond to a \"non-geographic entity\",\r\n\t\t// or there could be several countries corresponding to that country calling code.\r\n\t\t// In those cases, `this.country` is `undefined` and should be derived\r\n\t\t// from the number. Again, if country calling code is ambiguous, then\r\n\t\t// `this.country` should be re-derived with each new digit.\r\n\t\t//\r\n\t\tif (!this.state.country || this.isCountryCallingCodeAmbiguous()) {\r\n\t\t\tthis.determineTheCountry()\r\n\t\t}\r\n\t}\r\n\r\n\t// Prepends `+CountryCode ` in case of an international phone number\r\n\tgetFullNumber(formattedNationalNumber) {\r\n\t\tif (this.isInternational()) {\r\n\t\t\tconst prefix = (text) => this.formatter.getInternationalPrefixBeforeCountryCallingCode(this.state, {\r\n\t\t\t\tspacing: text ? true : false\r\n\t\t\t}) + text\r\n\t\t\tconst { callingCode } = this.state\r\n\t\t\tif (!callingCode) {\r\n\t\t\t\treturn prefix(`${this.state.getDigitsWithoutInternationalPrefix()}`)\r\n\t\t\t}\r\n\t\t\tif (!formattedNationalNumber) {\r\n\t\t\t\treturn prefix(callingCode)\r\n\t\t\t}\r\n\t\t\treturn prefix(`${callingCode} ${formattedNationalNumber}`)\r\n\t\t}\r\n\t\treturn formattedNationalNumber\r\n\t}\r\n\r\n\tgetNonFormattedNationalNumberWithPrefix() {\r\n\t\tconst {\r\n\t\t\tnationalSignificantNumber,\r\n\t\t\tcomplexPrefixBeforeNationalSignificantNumber,\r\n\t\t\tnationalPrefix\r\n\t\t} = this.state\r\n\t\tlet number = nationalSignificantNumber\r\n\t\tconst prefix = complexPrefixBeforeNationalSignificantNumber || nationalPrefix\r\n\t\tif (prefix) {\r\n\t\t\tnumber = prefix + number\r\n\t\t}\r\n\t\treturn number\r\n\t}\r\n\r\n\tgetNonFormattedNumber() {\r\n\t\tconst { nationalSignificantNumberMatchesInput } = this.state\r\n\t\treturn this.getFullNumber(\r\n\t\t\tnationalSignificantNumberMatchesInput\r\n\t\t\t\t? this.getNonFormattedNationalNumberWithPrefix()\r\n\t\t\t\t: this.state.getNationalDigits()\r\n\t\t)\r\n\t}\r\n\r\n\tgetNonFormattedTemplate() {\r\n\t\tconst number = this.getNonFormattedNumber()\r\n\t\tif (number) {\r\n\t\t\treturn number.replace(/[\\+\\d]/g, DIGIT_PLACEHOLDER)\r\n\t\t}\r\n\t}\r\n\r\n\tisCountryCallingCodeAmbiguous() {\r\n\t\tconst { callingCode } = this.state\r\n\t\tconst countryCodes = this.metadata.getCountryCodesForCallingCode(callingCode)\r\n\t\treturn countryCodes && countryCodes.length > 1\r\n\t}\r\n\r\n\t// Determines the country of the phone number\r\n\t// entered so far based on the country phone code\r\n\t// and the national phone number.\r\n\tdetermineTheCountry() {\r\n\t\tthis.state.setCountry(getCountryByCallingCode(\r\n\t\t\tthis.isInternational() ? this.state.callingCode : this.defaultCallingCode,\r\n\t\t\tthis.state.nationalSignificantNumber,\r\n\t\t\tthis.metadata\r\n\t\t))\r\n\t}\r\n\r\n\t/**\r\n\t * Returns a E.164 phone number value for the user's input.\r\n\t *\r\n\t * For example, for country `\"US\"` and input `\"(222) 333-4444\"`\r\n\t * it will return `\"+12223334444\"`.\r\n\t *\r\n\t * For international phone number input, it will also auto-correct\r\n\t * some minor errors such as using a national prefix when writing\r\n\t * an international phone number. For example, if the user inputs\r\n\t * `\"+44 0 7400 000000\"` then it will return an auto-corrected\r\n\t * `\"+447400000000\"` phone number value.\r\n\t *\r\n\t * Will return `undefined` if no digits have been input,\r\n\t * or when inputting a phone number in national format and no\r\n\t * default country or default \"country calling code\" have been set.\r\n\t *\r\n\t * @return {string} [value]\r\n\t */\r\n\tgetNumberValue() {\r\n\t\tconst {\r\n\t\t\tdigits,\r\n\t\t\tcallingCode,\r\n\t\t\tcountry,\r\n\t\t\tnationalSignificantNumber\r\n\t\t} = this.state\r\n\r\n\t \t// Will return `undefined` if no digits have been input.\r\n\t\tif (!digits) {\r\n\t\t\treturn\r\n\t\t}\r\n\r\n\t\tif (this.isInternational()) {\r\n\t\t\tif (callingCode) {\r\n\t\t\t\treturn '+' + callingCode + nationalSignificantNumber\r\n\t\t\t} else {\r\n\t\t\t\treturn '+' + digits\r\n\t\t\t}\r\n\t\t} else {\r\n\t\t\tif (country || callingCode) {\r\n\t\t\t\tconst callingCode_ = country ? this.metadata.countryCallingCode() : callingCode\r\n\t\t\t\treturn '+' + callingCode_ + nationalSignificantNumber\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\t/**\r\n\t * Returns an instance of `PhoneNumber` class.\r\n\t * Will return `undefined` if no national (significant) number\r\n\t * digits have been entered so far, or if no `defaultCountry` has been\r\n\t * set and the user enters a phone number not in international format.\r\n\t */\r\n\tgetNumber() {\r\n\t\tconst {\r\n\t\t\tnationalSignificantNumber,\r\n\t\t\tcarrierCode,\r\n\t\t\tcallingCode\r\n\t\t} = this.state\r\n\r\n\t\t// `this._getCountry()` is basically same as `this.state.country`\r\n\t\t// with the only change that it return `undefined` in case of a\r\n\t\t// \"non-geographic\" numbering plan instead of `\"001\"` \"internal use\" value.\r\n\t\tconst country = this._getCountry()\r\n\r\n\t\tif (!nationalSignificantNumber) {\r\n\t\t\treturn\r\n\t\t}\r\n\r\n\t\tif (!country && !callingCode) {\r\n\t\t\treturn\r\n\t\t}\r\n\r\n\t\tconst phoneNumber = new PhoneNumber(\r\n\t\t\tcountry || callingCode,\r\n\t\t\tnationalSignificantNumber,\r\n\t\t\tthis.metadata.metadata\r\n\t\t)\r\n\t\tif (carrierCode) {\r\n\t\t\tphoneNumber.carrierCode = carrierCode\r\n\t\t}\r\n\t\t// Phone number extensions are not supported by \"As You Type\" formatter.\r\n\t\treturn phoneNumber\r\n\t}\r\n\r\n\t/**\r\n\t * Returns `true` if the phone number is \"possible\".\r\n\t * Is just a shortcut for `PhoneNumber.isPossible()`.\r\n\t * @return {boolean}\r\n\t */\r\n\tisPossible() {\r\n\t\tconst phoneNumber = this.getNumber()\r\n\t\tif (!phoneNumber) {\r\n\t\t\treturn false\r\n\t\t}\r\n\t\treturn phoneNumber.isPossible()\r\n\t}\r\n\r\n\t/**\r\n\t * Returns `true` if the phone number is \"valid\".\r\n\t * Is just a shortcut for `PhoneNumber.isValid()`.\r\n\t * @return {boolean}\r\n\t */\r\n\tisValid() {\r\n\t\tconst phoneNumber = this.getNumber()\r\n\t\tif (!phoneNumber) {\r\n\t\t\treturn false\r\n\t\t}\r\n\t\treturn phoneNumber.isValid()\r\n\t}\r\n\r\n\t/**\r\n\t * @deprecated\r\n\t * This method is used in `react-phone-number-input/source/input-control.js`\r\n\t * in versions before `3.0.16`.\r\n\t */\r\n\tgetNationalNumber() {\r\n\t\treturn this.state.nationalSignificantNumber\r\n\t}\r\n\r\n\t/**\r\n\t * Returns the phone number characters entered by the user.\r\n\t * @return {string}\r\n\t */\r\n\tgetChars() {\r\n\t\treturn (this.state.international ? '+' : '') + this.state.digits\r\n\t}\r\n\r\n\t/**\r\n\t * Returns the template for the formatted phone number.\r\n\t * @return {string}\r\n\t */\r\n\tgetTemplate() {\r\n\t\treturn this.formatter.getTemplate(this.state) || this.getNonFormattedTemplate() || ''\r\n\t}\r\n}"],"file":"AsYouType.js"}
@@ -961,6 +961,226 @@ describe('AsYouType', function () {
961
961
  formatter.reset();
962
962
  formatter.input('123');
963
963
  formatter.getChars().should.equal('123');
964
+ }); // A test confirming the case when input `"11"` for country `"US"`
965
+ // produces `value` `"+11"`.
966
+ // https://gitlab.com/catamphetamine/react-phone-number-input/-/issues/113
967
+
968
+ it('should determine the national (significant) part correctly when input with national prefix in US', function () {
969
+ var formatter = new AsYouType('US'); // As soon as the user has input `"11"`, no `format` matches
970
+ // those "national number" digits in the `"US"` country metadata.
971
+ // Since no `format` matches, the number doesn't seem like a valid one,
972
+ // so it attempts to see if the user "forgot" to input a `"+"` at the start.
973
+ // And it looks like they might've to.
974
+ // So it acts as if the leading `"+"` is there,
975
+ // as if the user's input is `"+11"`.
976
+ // See `AsYouType.fixMissingPlus()` function.
977
+
978
+ formatter.input('1 122 222 2222 3').should.equal('1 1 222 222 2223');
979
+ formatter.getNumber().nationalNumber.should.equal('2222222223');
980
+ });
981
+ });
982
+ describe('AsYouType.getNumberValue()', function () {
983
+ it('should return E.164 number value (national number, with national prefix, default country: US)', function () {
984
+ var formatter = new AsYouType('US');
985
+ expect(formatter.getNumberValue()).to.be.undefined;
986
+ formatter.input('');
987
+ expect(formatter.getNumberValue()).to.be.undefined;
988
+ formatter.input('1');
989
+ formatter.getNumberValue().should.equal('+1');
990
+ formatter.input('2');
991
+ formatter.getNumberValue().should.equal('+12');
992
+ formatter.input('1');
993
+ formatter.getNumberValue().should.equal('+121');
994
+ formatter.input('3');
995
+ formatter.getNumberValue().should.equal('+1213');
996
+ formatter.input('373-4253');
997
+ formatter.getNumberValue().should.equal('+12133734253');
998
+ formatter.input('4');
999
+ formatter.getNumberValue().should.equal('+121337342534');
1000
+ });
1001
+ it('should return E.164 number value (national number, with national prefix, default calling code: 1)', function () {
1002
+ var formatter = new AsYouType({
1003
+ defaultCallingCode: '1'
1004
+ });
1005
+ expect(formatter.getNumberValue()).to.be.undefined;
1006
+ formatter.input('');
1007
+ expect(formatter.getNumberValue()).to.be.undefined;
1008
+ formatter.input('1');
1009
+ formatter.getNumberValue().should.equal('+1');
1010
+ formatter.input('2');
1011
+ formatter.getNumberValue().should.equal('+12');
1012
+ formatter.input('1');
1013
+ formatter.getNumberValue().should.equal('+121');
1014
+ formatter.input('3');
1015
+ formatter.getNumberValue().should.equal('+1213');
1016
+ formatter.input('373-4253');
1017
+ formatter.getNumberValue().should.equal('+12133734253');
1018
+ formatter.input('4');
1019
+ formatter.getNumberValue().should.equal('+121337342534');
1020
+ });
1021
+ it('should return E.164 number value (national number, default country: US)', function () {
1022
+ var formatter = new AsYouType('US');
1023
+ expect(formatter.getNumberValue()).to.be.undefined;
1024
+ formatter.input('');
1025
+ expect(formatter.getNumberValue()).to.be.undefined;
1026
+ formatter.input('2');
1027
+ formatter.getNumberValue().should.equal('+12');
1028
+ formatter.input('1');
1029
+ formatter.getNumberValue().should.equal('+121');
1030
+ formatter.input('3');
1031
+ formatter.getNumberValue().should.equal('+1213');
1032
+ formatter.input('373-4253');
1033
+ formatter.getNumberValue().should.equal('+12133734253');
1034
+ formatter.input('4');
1035
+ formatter.getNumberValue().should.equal('+121337342534');
1036
+ });
1037
+ it('should return E.164 number value (national number, default calling code: 1)', function () {
1038
+ var formatter = new AsYouType({
1039
+ defaultCallingCode: '1'
1040
+ });
1041
+ expect(formatter.getNumberValue()).to.be.undefined;
1042
+ formatter.input('');
1043
+ expect(formatter.getNumberValue()).to.be.undefined;
1044
+ formatter.input('2');
1045
+ formatter.getNumberValue().should.equal('+12');
1046
+ formatter.input('1');
1047
+ formatter.getNumberValue().should.equal('+121');
1048
+ formatter.input('3');
1049
+ formatter.getNumberValue().should.equal('+1213');
1050
+ formatter.input('373-4253');
1051
+ formatter.getNumberValue().should.equal('+12133734253');
1052
+ formatter.input('4');
1053
+ formatter.getNumberValue().should.equal('+121337342534');
1054
+ });
1055
+ it('should return E.164 number value (international number, default country: US)', function () {
1056
+ var formatter = new AsYouType('US');
1057
+ expect(formatter.getNumberValue()).to.be.undefined;
1058
+ formatter.input('');
1059
+ expect(formatter.getNumberValue()).to.be.undefined;
1060
+ formatter.input('+');
1061
+ expect(formatter.getNumberValue()).to.be.undefined;
1062
+ formatter.input('1');
1063
+ formatter.getNumberValue().should.equal('+1');
1064
+ formatter.input('1');
1065
+ formatter.getNumberValue().should.equal('+1');
1066
+ formatter.input('2');
1067
+ formatter.getNumberValue().should.equal('+12');
1068
+ formatter.input('1');
1069
+ formatter.getNumberValue().should.equal('+121');
1070
+ formatter.input('3');
1071
+ formatter.getNumberValue().should.equal('+1213');
1072
+ formatter.input('373-4253');
1073
+ formatter.getNumberValue().should.equal('+12133734253');
1074
+ formatter.input('4');
1075
+ formatter.getNumberValue().should.equal('+121337342534');
1076
+ });
1077
+ it('should return E.164 number value (international number, other default country: RU)', function () {
1078
+ var formatter = new AsYouType('RU');
1079
+ expect(formatter.getNumberValue()).to.be.undefined;
1080
+ formatter.input('');
1081
+ expect(formatter.getNumberValue()).to.be.undefined;
1082
+ formatter.input('+');
1083
+ expect(formatter.getNumberValue()).to.be.undefined;
1084
+ formatter.input('1');
1085
+ formatter.getNumberValue().should.equal('+1');
1086
+ formatter.input('1');
1087
+ formatter.getNumberValue().should.equal('+1');
1088
+ formatter.input('2');
1089
+ formatter.getNumberValue().should.equal('+12');
1090
+ formatter.input('1');
1091
+ formatter.getNumberValue().should.equal('+121');
1092
+ formatter.input('3');
1093
+ formatter.getNumberValue().should.equal('+1213');
1094
+ formatter.input('373-4253');
1095
+ formatter.getNumberValue().should.equal('+12133734253');
1096
+ formatter.input('4');
1097
+ formatter.getNumberValue().should.equal('+121337342534');
1098
+ });
1099
+ it('should return E.164 number value (international number, default calling code: 1)', function () {
1100
+ var formatter = new AsYouType('US', {
1101
+ defaultCallingCode: '1'
1102
+ });
1103
+ expect(formatter.getNumberValue()).to.be.undefined;
1104
+ formatter.input('');
1105
+ expect(formatter.getNumberValue()).to.be.undefined;
1106
+ formatter.input('+');
1107
+ expect(formatter.getNumberValue()).to.be.undefined;
1108
+ formatter.input('1');
1109
+ formatter.getNumberValue().should.equal('+1');
1110
+ formatter.input('1');
1111
+ formatter.getNumberValue().should.equal('+1');
1112
+ formatter.input('2');
1113
+ formatter.getNumberValue().should.equal('+12');
1114
+ formatter.input('1');
1115
+ formatter.getNumberValue().should.equal('+121');
1116
+ formatter.input('3');
1117
+ formatter.getNumberValue().should.equal('+1213');
1118
+ formatter.input('373-4253');
1119
+ formatter.getNumberValue().should.equal('+12133734253');
1120
+ formatter.input('4');
1121
+ formatter.getNumberValue().should.equal('+121337342534');
1122
+ });
1123
+ it('should return E.164 number value (international number, other default calling code: 7)', function () {
1124
+ var formatter = new AsYouType('US', {
1125
+ defaultCallingCode: '7'
1126
+ });
1127
+ expect(formatter.getNumberValue()).to.be.undefined;
1128
+ formatter.input('');
1129
+ expect(formatter.getNumberValue()).to.be.undefined;
1130
+ formatter.input('+');
1131
+ expect(formatter.getNumberValue()).to.be.undefined;
1132
+ formatter.input('1');
1133
+ formatter.getNumberValue().should.equal('+1');
1134
+ formatter.input('1');
1135
+ formatter.getNumberValue().should.equal('+1');
1136
+ formatter.input('2');
1137
+ formatter.getNumberValue().should.equal('+12');
1138
+ formatter.input('1');
1139
+ formatter.getNumberValue().should.equal('+121');
1140
+ formatter.input('3');
1141
+ formatter.getNumberValue().should.equal('+1213');
1142
+ formatter.input('373-4253');
1143
+ formatter.getNumberValue().should.equal('+12133734253');
1144
+ formatter.input('4');
1145
+ formatter.getNumberValue().should.equal('+121337342534');
1146
+ });
1147
+ it('should return E.164 number value (international number)', function () {
1148
+ var formatter = new AsYouType();
1149
+ expect(formatter.getNumberValue()).to.be.undefined;
1150
+ formatter.input('');
1151
+ expect(formatter.getNumberValue()).to.be.undefined;
1152
+ formatter.input('+');
1153
+ expect(formatter.getNumberValue()).to.be.undefined;
1154
+ formatter.input('1');
1155
+ formatter.getNumberValue().should.equal('+1');
1156
+ formatter.input('1');
1157
+ formatter.getNumberValue().should.equal('+1');
1158
+ formatter.input('2');
1159
+ formatter.getNumberValue().should.equal('+12');
1160
+ formatter.input('1');
1161
+ formatter.getNumberValue().should.equal('+121');
1162
+ formatter.input('3');
1163
+ formatter.getNumberValue().should.equal('+1213');
1164
+ formatter.input('373-4253');
1165
+ formatter.getNumberValue().should.equal('+12133734253');
1166
+ formatter.input('4');
1167
+ formatter.getNumberValue().should.equal('+121337342534');
1168
+ });
1169
+ it('should return E.164 number value (national number) (no default country or calling code)', function () {
1170
+ var formatter = new AsYouType();
1171
+ expect(formatter.getNumberValue()).to.be.undefined;
1172
+ formatter.input('');
1173
+ expect(formatter.getNumberValue()).to.be.undefined;
1174
+ formatter.input('1');
1175
+ expect(formatter.getNumberValue()).to.be.undefined;
1176
+ formatter.input('12');
1177
+ expect(formatter.getNumberValue()).to.be.undefined;
1178
+ formatter.input('3');
1179
+ expect(formatter.getNumberValue()).to.be.undefined;
1180
+ formatter.input('373-4253');
1181
+ expect(formatter.getNumberValue()).to.be.undefined;
1182
+ formatter.input('4');
1183
+ expect(formatter.getNumberValue()).to.be.undefined;
964
1184
  });
965
1185
  });
966
1186