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/core/index.d.ts CHANGED
@@ -98,6 +98,7 @@ export class AsYouType {
98
98
  input(text: string): string;
99
99
  reset(): void;
100
100
  getNumber(): PhoneNumber | undefined;
101
+ getNumberValue(): E164Number | undefined;
101
102
  getChars(): string;
102
103
  getTemplate(): string;
103
104
  getCallingCode(): string | undefined;
package/custom.d.ts CHANGED
@@ -151,6 +151,7 @@ export class AsYouType {
151
151
  reset(): void;
152
152
  country: CountryCode | undefined;
153
153
  getNumber(): PhoneNumber | undefined;
154
+ getNumberValue(): E164Number | undefined;
154
155
  getNationalNumber(): string;
155
156
  getChars(): string;
156
157
  getTemplate(): string;
package/es6/AsYouType.js CHANGED
@@ -174,16 +174,26 @@ function () {
174
174
  return this.state.international;
175
175
  }
176
176
  /**
177
- * Returns the "country calling code" part of the phone number.
178
- * Returns `undefined` if the number is not being input in international format.
179
- * Returns "country calling code" for "non-geographic" phone numbering plans too.
177
+ * Returns the "calling code" part of the phone number when it's being input
178
+ * in an international format.
179
+ * If no valid calling code has been entered so far, returns `undefined`.
180
180
  * @return {string} [callingCode]
181
181
  */
182
182
 
183
183
  }, {
184
184
  key: "getCallingCode",
185
185
  value: function getCallingCode() {
186
- return this.state.callingCode;
186
+ // If the number is being input in national format and some "default calling code"
187
+ // has been passed to `AsYouType` constructor, then `this.state.callingCode`
188
+ // is equal to that "default calling code".
189
+ //
190
+ // If the number is being input in national format and no "default calling code"
191
+ // has been passed to `AsYouType` constructor, then returns `undefined`,
192
+ // even if a "default country" has been passed to `AsYouType` constructor.
193
+ //
194
+ if (this.isInternational()) {
195
+ return this.state.callingCode;
196
+ }
187
197
  } // A legacy alias.
188
198
 
189
199
  }, {
@@ -201,28 +211,33 @@ function () {
201
211
  }, {
202
212
  key: "getCountry",
203
213
  value: function getCountry() {
204
- var _this$state = this.state,
205
- digits = _this$state.digits,
206
- country = _this$state.country; // If no digits have been input yet,
207
- // then `this.country` is the `defaultCountry`.
208
- // Won't return the `defaultCountry` in such case.
214
+ var digits = this.state.digits; // Return `undefined` if no digits have been input yet.
209
215
 
210
- if (!digits) {
211
- return;
216
+ if (digits) {
217
+ return this._getCountry();
212
218
  }
219
+ }
220
+ /**
221
+ * Returns a two-letter country code of the phone number.
222
+ * Returns `undefined` for "non-geographic" phone numbering plans.
223
+ * @return {string} [country]
224
+ */
213
225
 
214
- var countryCode = country;
226
+ }, {
227
+ key: "_getCountry",
228
+ value: function _getCountry() {
229
+ var country = this.state.country;
215
230
  /* istanbul ignore if */
216
231
 
217
232
  if (USE_NON_GEOGRAPHIC_COUNTRY_CODE) {
218
233
  // `AsYouType.getCountry()` returns `undefined`
219
234
  // for "non-geographic" phone numbering plans.
220
- if (countryCode === '001') {
221
- countryCode = undefined;
235
+ if (country === '001') {
236
+ return;
222
237
  }
223
238
  }
224
239
 
225
- return countryCode;
240
+ return country;
226
241
  }
227
242
  }, {
228
243
  key: "determineTheCountryIfNeeded",
@@ -278,10 +293,10 @@ function () {
278
293
  }, {
279
294
  key: "getNonFormattedNationalNumberWithPrefix",
280
295
  value: function getNonFormattedNationalNumberWithPrefix() {
281
- var _this$state2 = this.state,
282
- nationalSignificantNumber = _this$state2.nationalSignificantNumber,
283
- complexPrefixBeforeNationalSignificantNumber = _this$state2.complexPrefixBeforeNationalSignificantNumber,
284
- nationalPrefix = _this$state2.nationalPrefix;
296
+ var _this$state = this.state,
297
+ nationalSignificantNumber = _this$state.nationalSignificantNumber,
298
+ complexPrefixBeforeNationalSignificantNumber = _this$state.complexPrefixBeforeNationalSignificantNumber,
299
+ nationalPrefix = _this$state.nationalPrefix;
285
300
  var number = nationalSignificantNumber;
286
301
  var prefix = complexPrefixBeforeNationalSignificantNumber || nationalPrefix;
287
302
 
@@ -321,6 +336,51 @@ function () {
321
336
  value: function determineTheCountry() {
322
337
  this.state.setCountry(getCountryByCallingCode(this.isInternational() ? this.state.callingCode : this.defaultCallingCode, this.state.nationalSignificantNumber, this.metadata));
323
338
  }
339
+ /**
340
+ * Returns a E.164 phone number value for the user's input.
341
+ *
342
+ * For example, for country `"US"` and input `"(222) 333-4444"`
343
+ * it will return `"+12223334444"`.
344
+ *
345
+ * For international phone number input, it will also auto-correct
346
+ * some minor errors such as using a national prefix when writing
347
+ * an international phone number. For example, if the user inputs
348
+ * `"+44 0 7400 000000"` then it will return an auto-corrected
349
+ * `"+447400000000"` phone number value.
350
+ *
351
+ * Will return `undefined` if no digits have been input,
352
+ * or when inputting a phone number in national format and no
353
+ * default country or default "country calling code" have been set.
354
+ *
355
+ * @return {string} [value]
356
+ */
357
+
358
+ }, {
359
+ key: "getNumberValue",
360
+ value: function getNumberValue() {
361
+ var _this$state2 = this.state,
362
+ digits = _this$state2.digits,
363
+ callingCode = _this$state2.callingCode,
364
+ country = _this$state2.country,
365
+ nationalSignificantNumber = _this$state2.nationalSignificantNumber; // Will return `undefined` if no digits have been input.
366
+
367
+ if (!digits) {
368
+ return;
369
+ }
370
+
371
+ if (this.isInternational()) {
372
+ if (callingCode) {
373
+ return '+' + callingCode + nationalSignificantNumber;
374
+ } else {
375
+ return '+' + digits;
376
+ }
377
+ } else {
378
+ if (country || callingCode) {
379
+ var callingCode_ = country ? this.metadata.countryCallingCode() : callingCode;
380
+ return '+' + callingCode_ + nationalSignificantNumber;
381
+ }
382
+ }
383
+ }
324
384
  /**
325
385
  * Returns an instance of `PhoneNumber` class.
326
386
  * Will return `undefined` if no national (significant) number
@@ -333,25 +393,22 @@ function () {
333
393
  value: function getNumber() {
334
394
  var _this$state3 = this.state,
335
395
  nationalSignificantNumber = _this$state3.nationalSignificantNumber,
336
- carrierCode = _this$state3.carrierCode;
396
+ carrierCode = _this$state3.carrierCode,
397
+ callingCode = _this$state3.callingCode; // `this._getCountry()` is basically same as `this.state.country`
398
+ // with the only change that it return `undefined` in case of a
399
+ // "non-geographic" numbering plan instead of `"001"` "internal use" value.
337
400
 
338
- if (this.isInternational()) {
339
- if (!this.state.callingCode) {
340
- return;
341
- }
342
- } else {
343
- if (!this.state.country && !this.defaultCallingCode) {
344
- return;
345
- }
346
- }
401
+ var country = this._getCountry();
347
402
 
348
403
  if (!nationalSignificantNumber) {
349
404
  return;
350
405
  }
351
406
 
352
- var countryCode = this.getCountry();
353
- var callingCode = this.getCountryCallingCode() || this.defaultCallingCode;
354
- var phoneNumber = new PhoneNumber(countryCode || callingCode, nationalSignificantNumber, this.metadata.metadata);
407
+ if (!country && !callingCode) {
408
+ return;
409
+ }
410
+
411
+ var phoneNumber = new PhoneNumber(country || callingCode, nationalSignificantNumber, this.metadata.metadata);
355
412
 
356
413
  if (carrierCode) {
357
414
  phoneNumber.carrierCode = carrierCode;
@@ -1 +1 @@
1
- {"version":3,"sources":["../source/AsYouType.js"],"names":["Metadata","PhoneNumber","AsYouTypeState","AsYouTypeFormatter","DIGIT_PLACEHOLDER","AsYouTypeParser","extractFormattedDigitsAndPlus","getCountryByCallingCode","USE_NON_GEOGRAPHIC_COUNTRY_CODE","AsYouType","optionsOrDefaultCountry","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","onCountryChange","country","onCallingCodeChange","callingCode","selectNumberingPlan","numberingPlan","onNationalSignificantNumberChange","international","getCallingCode","countryCode","isCountryCallingCodeAmbiguous","determineTheCountry","isInternational","prefix","getInternationalPrefixBeforeCountryCallingCode","spacing","getDigitsWithoutInternationalPrefix","complexPrefixBeforeNationalSignificantNumber","nationalPrefix","number","nationalSignificantNumberMatchesInput","getNonFormattedNationalNumberWithPrefix","replace","countryCodes","getCountryCodesForCallingCode","length","setCountry","carrierCode","getCountry","getCountryCallingCode","phoneNumber","getNumber","isPossible","isValid","getTemplate","getNonFormattedTemplate"],"mappings":";;;;;;;;;;;;;;;;AAAA,OAAOA,QAAP,MAAqB,YAArB;AACA,OAAOC,WAAP,MAAwB,eAAxB;AACA,OAAOC,cAAP,MAA2B,kBAA3B;AACA,OAAOC,kBAAP,IAA6BC,iBAA7B,QAAsD,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CAAe4C,WAAf,CAA2B,KAAKlD,KAAhC,KAA0C,KAAKmD,uBAAL,EAA1C,IAA4E,EAAnF;AACA;;;;;;SAlVmBhE,S","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":["Metadata","PhoneNumber","AsYouTypeState","AsYouTypeFormatter","DIGIT_PLACEHOLDER","AsYouTypeParser","extractFormattedDigitsAndPlus","getCountryByCallingCode","USE_NON_GEOGRAPHIC_COUNTRY_CODE","AsYouType","optionsOrDefaultCountry","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","onCountryChange","country","onCallingCodeChange","callingCode","selectNumberingPlan","numberingPlan","onNationalSignificantNumberChange","international","isInternational","getCallingCode","_getCountry","isCountryCallingCodeAmbiguous","determineTheCountry","prefix","getInternationalPrefixBeforeCountryCallingCode","spacing","getDigitsWithoutInternationalPrefix","complexPrefixBeforeNationalSignificantNumber","nationalPrefix","number","nationalSignificantNumberMatchesInput","getNonFormattedNationalNumberWithPrefix","replace","countryCodes","getCountryCodesForCallingCode","length","setCountry","callingCode_","countryCallingCode","carrierCode","phoneNumber","getNumber","isPossible","isValid","getTemplate","getNonFormattedTemplate"],"mappings":";;;;;;;;;;;;;;;;AAAA,OAAOA,QAAP,MAAqB,YAArB;AACA,OAAOC,WAAP,MAAwB,eAAxB;AACA,OAAOC,cAAP,MAA2B,kBAA3B;AACA,OAAOC,kBAAP,IAA6BC,iBAA7B,QAAsD,sBAAtD;AACA,OAAOC,eAAP,IAA0BC,6BAA1B,QAA+D,mBAA/D;AACA,OAAOC,uBAAP,MAAoC,mCAApC;AAEA,IAAMC,+BAA+B,GAAG,KAAxC;;IAEqBC,S;;;AACpB;;;;AAIA,qBAAYC,uBAAZ,EAAqCC,QAArC,EAA+C;AAAA;;AAC9C,SAAKA,QAAL,GAAgB,IAAIX,QAAJ,CAAaW,QAAb,CAAhB;;AAD8C,gCAED,KAAKC,wBAAL,CAA8BF,uBAA9B,CAFC;AAAA;AAAA,QAEvCG,cAFuC;AAAA,QAEvBC,kBAFuB;;AAG9C,SAAKD,cAAL,GAAsBA,cAAtB;AACA,SAAKC,kBAAL,GAA0BA,kBAA1B;AACA,SAAKC,KAAL;AACA;;;;6CAEwBL,uB,EAAyB;AACjD;AACA,UAAIG,cAAJ;AACA,UAAIC,kBAAJ,CAHiD,CAIjD;;AACA,UAAIJ,uBAAJ,EAA6B;AAC5B,YAAI,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;;;;SAhZmBhE,S","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"}
@@ -956,6 +956,226 @@ describe('AsYouType', function () {
956
956
  formatter.reset();
957
957
  formatter.input('123');
958
958
  formatter.getChars().should.equal('123');
959
+ }); // A test confirming the case when input `"11"` for country `"US"`
960
+ // produces `value` `"+11"`.
961
+ // https://gitlab.com/catamphetamine/react-phone-number-input/-/issues/113
962
+
963
+ it('should determine the national (significant) part correctly when input with national prefix in US', function () {
964
+ var formatter = new AsYouType('US'); // As soon as the user has input `"11"`, no `format` matches
965
+ // those "national number" digits in the `"US"` country metadata.
966
+ // Since no `format` matches, the number doesn't seem like a valid one,
967
+ // so it attempts to see if the user "forgot" to input a `"+"` at the start.
968
+ // And it looks like they might've to.
969
+ // So it acts as if the leading `"+"` is there,
970
+ // as if the user's input is `"+11"`.
971
+ // See `AsYouType.fixMissingPlus()` function.
972
+
973
+ formatter.input('1 122 222 2222 3').should.equal('1 1 222 222 2223');
974
+ formatter.getNumber().nationalNumber.should.equal('2222222223');
975
+ });
976
+ });
977
+ describe('AsYouType.getNumberValue()', function () {
978
+ it('should return E.164 number value (national number, with national prefix, default country: US)', function () {
979
+ var formatter = new AsYouType('US');
980
+ expect(formatter.getNumberValue()).to.be.undefined;
981
+ formatter.input('');
982
+ expect(formatter.getNumberValue()).to.be.undefined;
983
+ formatter.input('1');
984
+ formatter.getNumberValue().should.equal('+1');
985
+ formatter.input('2');
986
+ formatter.getNumberValue().should.equal('+12');
987
+ formatter.input('1');
988
+ formatter.getNumberValue().should.equal('+121');
989
+ formatter.input('3');
990
+ formatter.getNumberValue().should.equal('+1213');
991
+ formatter.input('373-4253');
992
+ formatter.getNumberValue().should.equal('+12133734253');
993
+ formatter.input('4');
994
+ formatter.getNumberValue().should.equal('+121337342534');
995
+ });
996
+ it('should return E.164 number value (national number, with national prefix, default calling code: 1)', function () {
997
+ var formatter = new AsYouType({
998
+ defaultCallingCode: '1'
999
+ });
1000
+ expect(formatter.getNumberValue()).to.be.undefined;
1001
+ formatter.input('');
1002
+ expect(formatter.getNumberValue()).to.be.undefined;
1003
+ formatter.input('1');
1004
+ formatter.getNumberValue().should.equal('+1');
1005
+ formatter.input('2');
1006
+ formatter.getNumberValue().should.equal('+12');
1007
+ formatter.input('1');
1008
+ formatter.getNumberValue().should.equal('+121');
1009
+ formatter.input('3');
1010
+ formatter.getNumberValue().should.equal('+1213');
1011
+ formatter.input('373-4253');
1012
+ formatter.getNumberValue().should.equal('+12133734253');
1013
+ formatter.input('4');
1014
+ formatter.getNumberValue().should.equal('+121337342534');
1015
+ });
1016
+ it('should return E.164 number value (national number, default country: US)', function () {
1017
+ var formatter = new AsYouType('US');
1018
+ expect(formatter.getNumberValue()).to.be.undefined;
1019
+ formatter.input('');
1020
+ expect(formatter.getNumberValue()).to.be.undefined;
1021
+ formatter.input('2');
1022
+ formatter.getNumberValue().should.equal('+12');
1023
+ formatter.input('1');
1024
+ formatter.getNumberValue().should.equal('+121');
1025
+ formatter.input('3');
1026
+ formatter.getNumberValue().should.equal('+1213');
1027
+ formatter.input('373-4253');
1028
+ formatter.getNumberValue().should.equal('+12133734253');
1029
+ formatter.input('4');
1030
+ formatter.getNumberValue().should.equal('+121337342534');
1031
+ });
1032
+ it('should return E.164 number value (national number, default calling code: 1)', function () {
1033
+ var formatter = new AsYouType({
1034
+ defaultCallingCode: '1'
1035
+ });
1036
+ expect(formatter.getNumberValue()).to.be.undefined;
1037
+ formatter.input('');
1038
+ expect(formatter.getNumberValue()).to.be.undefined;
1039
+ formatter.input('2');
1040
+ formatter.getNumberValue().should.equal('+12');
1041
+ formatter.input('1');
1042
+ formatter.getNumberValue().should.equal('+121');
1043
+ formatter.input('3');
1044
+ formatter.getNumberValue().should.equal('+1213');
1045
+ formatter.input('373-4253');
1046
+ formatter.getNumberValue().should.equal('+12133734253');
1047
+ formatter.input('4');
1048
+ formatter.getNumberValue().should.equal('+121337342534');
1049
+ });
1050
+ it('should return E.164 number value (international number, default country: US)', function () {
1051
+ var formatter = new AsYouType('US');
1052
+ expect(formatter.getNumberValue()).to.be.undefined;
1053
+ formatter.input('');
1054
+ expect(formatter.getNumberValue()).to.be.undefined;
1055
+ formatter.input('+');
1056
+ expect(formatter.getNumberValue()).to.be.undefined;
1057
+ formatter.input('1');
1058
+ formatter.getNumberValue().should.equal('+1');
1059
+ formatter.input('1');
1060
+ formatter.getNumberValue().should.equal('+1');
1061
+ formatter.input('2');
1062
+ formatter.getNumberValue().should.equal('+12');
1063
+ formatter.input('1');
1064
+ formatter.getNumberValue().should.equal('+121');
1065
+ formatter.input('3');
1066
+ formatter.getNumberValue().should.equal('+1213');
1067
+ formatter.input('373-4253');
1068
+ formatter.getNumberValue().should.equal('+12133734253');
1069
+ formatter.input('4');
1070
+ formatter.getNumberValue().should.equal('+121337342534');
1071
+ });
1072
+ it('should return E.164 number value (international number, other default country: RU)', function () {
1073
+ var formatter = new AsYouType('RU');
1074
+ expect(formatter.getNumberValue()).to.be.undefined;
1075
+ formatter.input('');
1076
+ expect(formatter.getNumberValue()).to.be.undefined;
1077
+ formatter.input('+');
1078
+ expect(formatter.getNumberValue()).to.be.undefined;
1079
+ formatter.input('1');
1080
+ formatter.getNumberValue().should.equal('+1');
1081
+ formatter.input('1');
1082
+ formatter.getNumberValue().should.equal('+1');
1083
+ formatter.input('2');
1084
+ formatter.getNumberValue().should.equal('+12');
1085
+ formatter.input('1');
1086
+ formatter.getNumberValue().should.equal('+121');
1087
+ formatter.input('3');
1088
+ formatter.getNumberValue().should.equal('+1213');
1089
+ formatter.input('373-4253');
1090
+ formatter.getNumberValue().should.equal('+12133734253');
1091
+ formatter.input('4');
1092
+ formatter.getNumberValue().should.equal('+121337342534');
1093
+ });
1094
+ it('should return E.164 number value (international number, default calling code: 1)', function () {
1095
+ var formatter = new AsYouType('US', {
1096
+ defaultCallingCode: '1'
1097
+ });
1098
+ expect(formatter.getNumberValue()).to.be.undefined;
1099
+ formatter.input('');
1100
+ expect(formatter.getNumberValue()).to.be.undefined;
1101
+ formatter.input('+');
1102
+ expect(formatter.getNumberValue()).to.be.undefined;
1103
+ formatter.input('1');
1104
+ formatter.getNumberValue().should.equal('+1');
1105
+ formatter.input('1');
1106
+ formatter.getNumberValue().should.equal('+1');
1107
+ formatter.input('2');
1108
+ formatter.getNumberValue().should.equal('+12');
1109
+ formatter.input('1');
1110
+ formatter.getNumberValue().should.equal('+121');
1111
+ formatter.input('3');
1112
+ formatter.getNumberValue().should.equal('+1213');
1113
+ formatter.input('373-4253');
1114
+ formatter.getNumberValue().should.equal('+12133734253');
1115
+ formatter.input('4');
1116
+ formatter.getNumberValue().should.equal('+121337342534');
1117
+ });
1118
+ it('should return E.164 number value (international number, other default calling code: 7)', function () {
1119
+ var formatter = new AsYouType('US', {
1120
+ defaultCallingCode: '7'
1121
+ });
1122
+ expect(formatter.getNumberValue()).to.be.undefined;
1123
+ formatter.input('');
1124
+ expect(formatter.getNumberValue()).to.be.undefined;
1125
+ formatter.input('+');
1126
+ expect(formatter.getNumberValue()).to.be.undefined;
1127
+ formatter.input('1');
1128
+ formatter.getNumberValue().should.equal('+1');
1129
+ formatter.input('1');
1130
+ formatter.getNumberValue().should.equal('+1');
1131
+ formatter.input('2');
1132
+ formatter.getNumberValue().should.equal('+12');
1133
+ formatter.input('1');
1134
+ formatter.getNumberValue().should.equal('+121');
1135
+ formatter.input('3');
1136
+ formatter.getNumberValue().should.equal('+1213');
1137
+ formatter.input('373-4253');
1138
+ formatter.getNumberValue().should.equal('+12133734253');
1139
+ formatter.input('4');
1140
+ formatter.getNumberValue().should.equal('+121337342534');
1141
+ });
1142
+ it('should return E.164 number value (international number)', function () {
1143
+ var formatter = new AsYouType();
1144
+ expect(formatter.getNumberValue()).to.be.undefined;
1145
+ formatter.input('');
1146
+ expect(formatter.getNumberValue()).to.be.undefined;
1147
+ formatter.input('+');
1148
+ expect(formatter.getNumberValue()).to.be.undefined;
1149
+ formatter.input('1');
1150
+ formatter.getNumberValue().should.equal('+1');
1151
+ formatter.input('1');
1152
+ formatter.getNumberValue().should.equal('+1');
1153
+ formatter.input('2');
1154
+ formatter.getNumberValue().should.equal('+12');
1155
+ formatter.input('1');
1156
+ formatter.getNumberValue().should.equal('+121');
1157
+ formatter.input('3');
1158
+ formatter.getNumberValue().should.equal('+1213');
1159
+ formatter.input('373-4253');
1160
+ formatter.getNumberValue().should.equal('+12133734253');
1161
+ formatter.input('4');
1162
+ formatter.getNumberValue().should.equal('+121337342534');
1163
+ });
1164
+ it('should return E.164 number value (national number) (no default country or calling code)', function () {
1165
+ var formatter = new AsYouType();
1166
+ expect(formatter.getNumberValue()).to.be.undefined;
1167
+ formatter.input('');
1168
+ expect(formatter.getNumberValue()).to.be.undefined;
1169
+ formatter.input('1');
1170
+ expect(formatter.getNumberValue()).to.be.undefined;
1171
+ formatter.input('12');
1172
+ expect(formatter.getNumberValue()).to.be.undefined;
1173
+ formatter.input('3');
1174
+ expect(formatter.getNumberValue()).to.be.undefined;
1175
+ formatter.input('373-4253');
1176
+ expect(formatter.getNumberValue()).to.be.undefined;
1177
+ formatter.input('4');
1178
+ expect(formatter.getNumberValue()).to.be.undefined;
959
1179
  });
960
1180
  });
961
1181