libphonenumber-js 1.10.24 → 1.10.26

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 (94) hide show
  1. package/CODE_OF_CONDUCT.md +78 -0
  2. package/README.md +16 -6
  3. package/build/PhoneNumber.js +6 -0
  4. package/build/PhoneNumber.js.map +1 -1
  5. package/build/PhoneNumberMatcher.js +53 -62
  6. package/build/PhoneNumberMatcher.js.map +1 -1
  7. package/build/PhoneNumberMatcher.test.js +8 -0
  8. package/build/PhoneNumberMatcher.test.js.map +1 -1
  9. package/build/findNumbers/Leniency.js +60 -50
  10. package/build/findNumbers/Leniency.js.map +1 -1
  11. package/build/findNumbers/matchPhoneNumberStringAgainstPhoneNumber.js +75 -0
  12. package/build/findNumbers/matchPhoneNumberStringAgainstPhoneNumber.js.map +1 -0
  13. package/build/format.js +1 -0
  14. package/build/format.js.map +1 -1
  15. package/build/helpers/RFC3966.test.js +3 -1
  16. package/build/helpers/RFC3966.test.js.map +1 -1
  17. package/build/helpers/extractCountryCallingCode.js +44 -4
  18. package/build/helpers/extractCountryCallingCode.js.map +1 -1
  19. package/build/helpers/extractCountryCallingCode.test.js +2 -0
  20. package/build/helpers/extractCountryCallingCode.test.js.map +1 -1
  21. package/build/helpers/extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js +86 -0
  22. package/build/helpers/extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js.map +1 -0
  23. package/build/helpers/extractPhoneContext.js +97 -0
  24. package/build/helpers/extractPhoneContext.js.map +1 -0
  25. package/build/helpers/extractPhoneContext.test.js +72 -0
  26. package/build/helpers/extractPhoneContext.test.js.map +1 -0
  27. package/build/parse.js +19 -9
  28. package/build/parse.js.map +1 -1
  29. package/bundle/libphonenumber-js.min.js +1 -1
  30. package/bundle/libphonenumber-js.min.js.map +1 -1
  31. package/bundle/libphonenumber-max.js +1 -1
  32. package/bundle/libphonenumber-max.js.map +1 -1
  33. package/bundle/libphonenumber-min.js +1 -1
  34. package/bundle/libphonenumber-min.js.map +1 -1
  35. package/bundle/libphonenumber-mobile.js +1 -1
  36. package/bundle/libphonenumber-mobile.js.map +1 -1
  37. package/core/index.d.ts +3 -3
  38. package/custom.d.ts +4 -4
  39. package/es6/PhoneNumber.js +6 -0
  40. package/es6/PhoneNumber.js.map +1 -1
  41. package/es6/PhoneNumberMatcher.js +53 -62
  42. package/es6/PhoneNumberMatcher.js.map +1 -1
  43. package/es6/PhoneNumberMatcher.test.js +8 -0
  44. package/es6/PhoneNumberMatcher.test.js.map +1 -1
  45. package/es6/findNumbers/Leniency.js +56 -50
  46. package/es6/findNumbers/Leniency.js.map +1 -1
  47. package/es6/findNumbers/matchPhoneNumberStringAgainstPhoneNumber.js +66 -0
  48. package/es6/findNumbers/matchPhoneNumberStringAgainstPhoneNumber.js.map +1 -0
  49. package/es6/format.js +1 -1
  50. package/es6/format.js.map +1 -1
  51. package/es6/helpers/RFC3966.test.js +3 -1
  52. package/es6/helpers/RFC3966.test.js.map +1 -1
  53. package/es6/helpers/extractCountryCallingCode.js +44 -4
  54. package/es6/helpers/extractCountryCallingCode.js.map +1 -1
  55. package/es6/helpers/extractCountryCallingCode.test.js +2 -0
  56. package/es6/helpers/extractCountryCallingCode.test.js.map +1 -1
  57. package/es6/helpers/extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js +70 -0
  58. package/es6/helpers/extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js.map +1 -0
  59. package/es6/helpers/extractPhoneContext.js +82 -0
  60. package/es6/helpers/extractPhoneContext.js.map +1 -0
  61. package/es6/helpers/extractPhoneContext.test.js +66 -0
  62. package/es6/helpers/extractPhoneContext.test.js.map +1 -0
  63. package/es6/parse.js +20 -10
  64. package/es6/parse.js.map +1 -1
  65. package/examples.mobile.json.d.ts +1 -1
  66. package/index.d.ts +3 -3
  67. package/metadata.full.json +1 -1
  68. package/metadata.full.json.d.ts +1 -1
  69. package/metadata.full.json.js +1 -1
  70. package/metadata.max.json +1 -1
  71. package/metadata.max.json.d.ts +1 -1
  72. package/metadata.max.json.js +1 -1
  73. package/metadata.min.json +1 -1
  74. package/metadata.min.json.d.ts +1 -1
  75. package/metadata.min.json.js +1 -1
  76. package/metadata.mobile.json +1 -1
  77. package/metadata.mobile.json.d.ts +1 -1
  78. package/metadata.mobile.json.js +1 -1
  79. package/min/index.d.ts +3 -3
  80. package/package.json +4 -4
  81. package/source/AsYouTypeFormatter.PatternMatcher.d.ts +1 -1
  82. package/source/PhoneNumber.js +6 -0
  83. package/source/PhoneNumberMatcher.js +55 -58
  84. package/source/PhoneNumberMatcher.test.js +4 -0
  85. package/source/findNumbers/Leniency.js +281 -271
  86. package/source/findNumbers/matchPhoneNumberStringAgainstPhoneNumber.js +64 -0
  87. package/source/format.js +1 -1
  88. package/source/helpers/RFC3966.test.js +3 -0
  89. package/source/helpers/extractCountryCallingCode.js +48 -3
  90. package/source/helpers/extractCountryCallingCode.test.js +2 -0
  91. package/source/helpers/extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js +74 -0
  92. package/source/helpers/extractPhoneContext.js +103 -0
  93. package/source/helpers/extractPhoneContext.test.js +104 -0
  94. package/source/parse.js +18 -8
@@ -12,7 +12,7 @@ import { MAX_LENGTH_COUNTRY_CODE } from '../constants.js';
12
12
  * @param {string} [country] — Default country.
13
13
  * @param {string} [callingCode] — Default calling code (some phone numbering plans are non-geographic).
14
14
  * @param {object} metadata
15
- * @return {object} `{ countryCallingCode: string?, number: string }`
15
+ * @return {object} `{ countryCallingCodeSource: string?, countryCallingCode: string?, number: string }`
16
16
  * @example
17
17
  * // Returns `{ countryCallingCode: "1", number: "2133734253" }`.
18
18
  * extractCountryCallingCode('2133734253', 'US', null, metadata)
@@ -24,7 +24,9 @@ import { MAX_LENGTH_COUNTRY_CODE } from '../constants.js';
24
24
  export default function extractCountryCallingCode(number, country, callingCode, metadata) {
25
25
  if (!number) {
26
26
  return {};
27
- } // If this is not an international phone number,
27
+ }
28
+
29
+ var isNumberWithIddPrefix; // If this is not an international phone number,
28
30
  // then either extract an "IDD" prefix, or extract a
29
31
  // country calling code from a number by autocorrecting it
30
32
  // by prepending a leading `+` in cases when it starts
@@ -32,7 +34,6 @@ export default function extractCountryCallingCode(number, country, callingCode,
32
34
  // https://wikitravel.org/en/International_dialling_prefix
33
35
  // https://github.com/catamphetamine/libphonenumber-js/issues/376
34
36
 
35
-
36
37
  if (number[0] !== '+') {
37
38
  // Convert an "out-of-country" dialing phone number
38
39
  // to a proper international phone number.
@@ -41,6 +42,7 @@ export default function extractCountryCallingCode(number, country, callingCode,
41
42
  // for subsequent parsing.
42
43
 
43
44
  if (numberWithoutIDD && numberWithoutIDD !== number) {
45
+ isNumberWithIddPrefix = true;
44
46
  number = '+' + numberWithoutIDD;
45
47
  } else {
46
48
  // Check to see if the number starts with the country calling code
@@ -54,6 +56,7 @@ export default function extractCountryCallingCode(number, country, callingCode,
54
56
 
55
57
  if (countryCallingCode) {
56
58
  return {
59
+ countryCallingCodeSource: 'FROM_NUMBER_WITHOUT_PLUS_SIGN',
57
60
  countryCallingCode: countryCallingCode,
58
61
  number: shorterNumber
59
62
  };
@@ -61,6 +64,8 @@ export default function extractCountryCallingCode(number, country, callingCode,
61
64
  }
62
65
 
63
66
  return {
67
+ // No need to set it to `UNSPECIFIED`. It can be just `undefined`.
68
+ // countryCallingCodeSource: 'UNSPECIFIED',
64
69
  number: number
65
70
  };
66
71
  }
@@ -89,6 +94,7 @@ export default function extractCountryCallingCode(number, country, callingCode,
89
94
  if (metadata.hasCallingCode(_countryCallingCode)) {
90
95
  metadata.selectNumberingPlan(_countryCallingCode);
91
96
  return {
97
+ countryCallingCodeSource: isNumberWithIddPrefix ? 'FROM_NUMBER_WITH_IDD' : 'FROM_NUMBER_WITH_PLUS_SIGN',
92
98
  countryCallingCode: _countryCallingCode,
93
99
  number: number.slice(i)
94
100
  };
@@ -98,5 +104,39 @@ export default function extractCountryCallingCode(number, country, callingCode,
98
104
  }
99
105
 
100
106
  return {};
101
- }
107
+ } // The possible values for the returned `countryCallingCodeSource` are:
108
+ //
109
+ // Copy-pasted from:
110
+ // https://github.com/google/libphonenumber/blob/master/resources/phonenumber.proto
111
+ //
112
+ // // The source from which the country_code is derived. This is not set in the
113
+ // // general parsing method, but in the method that parses and keeps raw_input.
114
+ // // New fields could be added upon request.
115
+ // enum CountryCodeSource {
116
+ // // Default value returned if this is not set, because the phone number was
117
+ // // created using parse, not parseAndKeepRawInput. hasCountryCodeSource will
118
+ // // return false if this is the case.
119
+ // UNSPECIFIED = 0;
120
+ //
121
+ // // The country_code is derived based on a phone number with a leading "+",
122
+ // // e.g. the French number "+33 1 42 68 53 00".
123
+ // FROM_NUMBER_WITH_PLUS_SIGN = 1;
124
+ //
125
+ // // The country_code is derived based on a phone number with a leading IDD,
126
+ // // e.g. the French number "011 33 1 42 68 53 00", as it is dialled from US.
127
+ // FROM_NUMBER_WITH_IDD = 5;
128
+ //
129
+ // // The country_code is derived based on a phone number without a leading
130
+ // // "+", e.g. the French number "33 1 42 68 53 00" when defaultCountry is
131
+ // // supplied as France.
132
+ // FROM_NUMBER_WITHOUT_PLUS_SIGN = 10;
133
+ //
134
+ // // The country_code is derived NOT based on the phone number itself, but
135
+ // // from the defaultCountry parameter provided in the parsing function by the
136
+ // // clients. This happens mostly for numbers written in the national format
137
+ // // (without country code). For example, this would be set when parsing the
138
+ // // French number "01 42 68 53 00", when defaultCountry is supplied as
139
+ // // France.
140
+ // FROM_DEFAULT_COUNTRY = 20;
141
+ // }
102
142
  //# sourceMappingURL=extractCountryCallingCode.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"extractCountryCallingCode.js","names":["stripIddPrefix","extractCountryCallingCodeFromInternationalNumberWithoutPlusSign","Metadata","MAX_LENGTH_COUNTRY_CODE","extractCountryCallingCode","number","country","callingCode","metadata","numberWithoutIDD","countryCallingCode","shorterNumber","i","length","slice","hasCallingCode","selectNumberingPlan"],"sources":["../../source/helpers/extractCountryCallingCode.js"],"sourcesContent":["import stripIddPrefix from './stripIddPrefix.js'\r\nimport extractCountryCallingCodeFromInternationalNumberWithoutPlusSign from './extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js'\r\nimport Metadata from '../metadata.js'\r\nimport { MAX_LENGTH_COUNTRY_CODE } from '../constants.js'\r\n\r\n/**\r\n * Converts a phone number digits (possibly with a `+`)\r\n * into a calling code and the rest phone number digits.\r\n * The \"rest phone number digits\" could include\r\n * a national prefix, carrier code, and national\r\n * (significant) number.\r\n * @param {string} number — Phone number digits (possibly with a `+`).\r\n * @param {string} [country] — Default country.\r\n * @param {string} [callingCode] — Default calling code (some phone numbering plans are non-geographic).\r\n * @param {object} metadata\r\n * @return {object} `{ countryCallingCode: string?, number: string }`\r\n * @example\r\n * // Returns `{ countryCallingCode: \"1\", number: \"2133734253\" }`.\r\n * extractCountryCallingCode('2133734253', 'US', null, metadata)\r\n * extractCountryCallingCode('2133734253', null, '1', metadata)\r\n * extractCountryCallingCode('+12133734253', null, null, metadata)\r\n * extractCountryCallingCode('+12133734253', 'RU', null, metadata)\r\n */\r\nexport default function extractCountryCallingCode(\r\n\tnumber,\r\n\tcountry,\r\n\tcallingCode,\r\n\tmetadata\r\n) {\r\n\tif (!number) {\r\n\t\treturn {}\r\n\t}\r\n\r\n\t// If this is not an international phone number,\r\n\t// then either extract an \"IDD\" prefix, or extract a\r\n\t// country calling code from a number by autocorrecting it\r\n\t// by prepending a leading `+` in cases when it starts\r\n\t// with the country calling code.\r\n\t// https://wikitravel.org/en/International_dialling_prefix\r\n\t// https://github.com/catamphetamine/libphonenumber-js/issues/376\r\n\tif (number[0] !== '+') {\r\n\t\t// Convert an \"out-of-country\" dialing phone number\r\n\t\t// to a proper international phone number.\r\n\t\tconst numberWithoutIDD = stripIddPrefix(number, country, callingCode, metadata)\r\n\t\t// If an IDD prefix was stripped then\r\n\t\t// convert the number to international one\r\n\t\t// for subsequent parsing.\r\n\t\tif (numberWithoutIDD && numberWithoutIDD !== number) {\r\n\t\t\tnumber = '+' + numberWithoutIDD\r\n\t\t} else {\r\n\t\t\t// Check to see if the number starts with the country calling code\r\n\t\t\t// for the default country. If so, we remove the country calling code,\r\n\t\t\t// and do some checks on the validity of the number before and after.\r\n\t\t\t// https://github.com/catamphetamine/libphonenumber-js/issues/376\r\n\t\t\tif (country || callingCode) {\r\n\t\t\t\tconst {\r\n\t\t\t\t\tcountryCallingCode,\r\n\t\t\t\t\tnumber: shorterNumber\r\n\t\t\t\t} = extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(\r\n\t\t\t\t\tnumber,\r\n\t\t\t\t\tcountry,\r\n\t\t\t\t\tcallingCode,\r\n\t\t\t\t\tmetadata\r\n\t\t\t\t)\r\n\t\t\t\tif (countryCallingCode) {\r\n\t\t\t\t\treturn {\r\n\t\t\t\t\t\tcountryCallingCode,\r\n\t\t\t\t\t\tnumber: shorterNumber\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\treturn { number }\r\n\t\t}\r\n\t}\r\n\r\n\t// Fast abortion: country codes do not begin with a '0'\r\n\tif (number[1] === '0') {\r\n\t\treturn {}\r\n\t}\r\n\r\n\tmetadata = new Metadata(metadata)\r\n\r\n\t// The thing with country phone codes\r\n\t// is that they are orthogonal to each other\r\n\t// i.e. there's no such country phone code A\r\n\t// for which country phone code B exists\r\n\t// where B starts with A.\r\n\t// Therefore, while scanning digits,\r\n\t// if a valid country code is found,\r\n\t// that means that it is the country code.\r\n\t//\r\n\tlet i = 2\r\n\twhile (i - 1 <= MAX_LENGTH_COUNTRY_CODE && i <= number.length) {\r\n\t\tconst countryCallingCode = number.slice(1, i)\r\n\t\tif (metadata.hasCallingCode(countryCallingCode)) {\r\n\t\t\tmetadata.selectNumberingPlan(countryCallingCode)\r\n\t\t\treturn {\r\n\t\t\t\tcountryCallingCode,\r\n\t\t\t\tnumber: number.slice(i)\r\n\t\t\t}\r\n\t\t}\r\n\t\ti++\r\n\t}\r\n\r\n\treturn {}\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,CAAX,CApDC,CAsDD;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;EACA,IAAII,CAAC,GAAG,CAAR;;EACA,OAAOA,CAAC,GAAG,CAAJ,IAAST,uBAAT,IAAoCS,CAAC,IAAIP,MAAM,CAACQ,MAAvD,EAA+D;IAC9D,IAAMH,mBAAkB,GAAGL,MAAM,CAACS,KAAP,CAAa,CAAb,EAAgBF,CAAhB,CAA3B;;IACA,IAAIJ,QAAQ,CAACO,cAAT,CAAwBL,mBAAxB,CAAJ,EAAiD;MAChDF,QAAQ,CAACQ,mBAAT,CAA6BN,mBAA7B;MACA,OAAO;QACNA,kBAAkB,EAAlBA,mBADM;QAENL,MAAM,EAAEA,MAAM,CAACS,KAAP,CAAaF,CAAb;MAFF,CAAP;IAIA;;IACDA,CAAC;EACD;;EAED,OAAO,EAAP;AACA"}
1
+ {"version":3,"file":"extractCountryCallingCode.js","names":["stripIddPrefix","extractCountryCallingCodeFromInternationalNumberWithoutPlusSign","Metadata","MAX_LENGTH_COUNTRY_CODE","extractCountryCallingCode","number","country","callingCode","metadata","isNumberWithIddPrefix","numberWithoutIDD","countryCallingCode","shorterNumber","countryCallingCodeSource","i","length","slice","hasCallingCode","selectNumberingPlan"],"sources":["../../source/helpers/extractCountryCallingCode.js"],"sourcesContent":["import stripIddPrefix from './stripIddPrefix.js'\r\nimport extractCountryCallingCodeFromInternationalNumberWithoutPlusSign from './extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js'\r\nimport Metadata from '../metadata.js'\r\nimport { MAX_LENGTH_COUNTRY_CODE } from '../constants.js'\r\n\r\n/**\r\n * Converts a phone number digits (possibly with a `+`)\r\n * into a calling code and the rest phone number digits.\r\n * The \"rest phone number digits\" could include\r\n * a national prefix, carrier code, and national\r\n * (significant) number.\r\n * @param {string} number — Phone number digits (possibly with a `+`).\r\n * @param {string} [country] — Default country.\r\n * @param {string} [callingCode] — Default calling code (some phone numbering plans are non-geographic).\r\n * @param {object} metadata\r\n * @return {object} `{ countryCallingCodeSource: string?, countryCallingCode: string?, number: string }`\r\n * @example\r\n * // Returns `{ countryCallingCode: \"1\", number: \"2133734253\" }`.\r\n * extractCountryCallingCode('2133734253', 'US', null, metadata)\r\n * extractCountryCallingCode('2133734253', null, '1', metadata)\r\n * extractCountryCallingCode('+12133734253', null, null, metadata)\r\n * extractCountryCallingCode('+12133734253', 'RU', null, metadata)\r\n */\r\nexport default function extractCountryCallingCode(\r\n\tnumber,\r\n\tcountry,\r\n\tcallingCode,\r\n\tmetadata\r\n) {\r\n\tif (!number) {\r\n\t\treturn {}\r\n\t}\r\n\r\n\tlet isNumberWithIddPrefix\r\n\r\n\t// If this is not an international phone number,\r\n\t// then either extract an \"IDD\" prefix, or extract a\r\n\t// country calling code from a number by autocorrecting it\r\n\t// by prepending a leading `+` in cases when it starts\r\n\t// with the country calling code.\r\n\t// https://wikitravel.org/en/International_dialling_prefix\r\n\t// https://github.com/catamphetamine/libphonenumber-js/issues/376\r\n\tif (number[0] !== '+') {\r\n\t\t// Convert an \"out-of-country\" dialing phone number\r\n\t\t// to a proper international phone number.\r\n\t\tconst numberWithoutIDD = stripIddPrefix(number, country, callingCode, metadata)\r\n\t\t// If an IDD prefix was stripped then\r\n\t\t// convert the number to international one\r\n\t\t// for subsequent parsing.\r\n\t\tif (numberWithoutIDD && numberWithoutIDD !== number) {\r\n\t\t\tisNumberWithIddPrefix = true\r\n\t\t\tnumber = '+' + numberWithoutIDD\r\n\t\t} else {\r\n\t\t\t// Check to see if the number starts with the country calling code\r\n\t\t\t// for the default country. If so, we remove the country calling code,\r\n\t\t\t// and do some checks on the validity of the number before and after.\r\n\t\t\t// https://github.com/catamphetamine/libphonenumber-js/issues/376\r\n\t\t\tif (country || callingCode) {\r\n\t\t\t\tconst {\r\n\t\t\t\t\tcountryCallingCode,\r\n\t\t\t\t\tnumber: shorterNumber\r\n\t\t\t\t} = extractCountryCallingCodeFromInternationalNumberWithoutPlusSign(\r\n\t\t\t\t\tnumber,\r\n\t\t\t\t\tcountry,\r\n\t\t\t\t\tcallingCode,\r\n\t\t\t\t\tmetadata\r\n\t\t\t\t)\r\n\t\t\t\tif (countryCallingCode) {\r\n\t\t\t\t\treturn {\r\n\t\t\t\t\t\tcountryCallingCodeSource: 'FROM_NUMBER_WITHOUT_PLUS_SIGN',\r\n\t\t\t\t\t\tcountryCallingCode,\r\n\t\t\t\t\t\tnumber: shorterNumber\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\treturn {\r\n\t\t\t\t// No need to set it to `UNSPECIFIED`. It can be just `undefined`.\r\n\t\t\t\t// countryCallingCodeSource: 'UNSPECIFIED',\r\n\t\t\t\tnumber\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\t// Fast abortion: country codes do not begin with a '0'\r\n\tif (number[1] === '0') {\r\n\t\treturn {}\r\n\t}\r\n\r\n\tmetadata = new Metadata(metadata)\r\n\r\n\t// The thing with country phone codes\r\n\t// is that they are orthogonal to each other\r\n\t// i.e. there's no such country phone code A\r\n\t// for which country phone code B exists\r\n\t// where B starts with A.\r\n\t// Therefore, while scanning digits,\r\n\t// if a valid country code is found,\r\n\t// that means that it is the country code.\r\n\t//\r\n\tlet i = 2\r\n\twhile (i - 1 <= MAX_LENGTH_COUNTRY_CODE && i <= number.length) {\r\n\t\tconst countryCallingCode = number.slice(1, i)\r\n\t\tif (metadata.hasCallingCode(countryCallingCode)) {\r\n\t\t\tmetadata.selectNumberingPlan(countryCallingCode)\r\n\t\t\treturn {\r\n\t\t\t\tcountryCallingCodeSource: isNumberWithIddPrefix ? 'FROM_NUMBER_WITH_IDD' : 'FROM_NUMBER_WITH_PLUS_SIGN',\r\n\t\t\t\tcountryCallingCode,\r\n\t\t\t\tnumber: number.slice(i)\r\n\t\t\t}\r\n\t\t}\r\n\t\ti++\r\n\t}\r\n\r\n\treturn {}\r\n}\r\n\r\n// The possible values for the returned `countryCallingCodeSource` are:\r\n//\r\n// Copy-pasted from:\r\n// https://github.com/google/libphonenumber/blob/master/resources/phonenumber.proto\r\n//\r\n// // The source from which the country_code is derived. This is not set in the\r\n// // general parsing method, but in the method that parses and keeps raw_input.\r\n// // New fields could be added upon request.\r\n// enum CountryCodeSource {\r\n// // Default value returned if this is not set, because the phone number was\r\n// // created using parse, not parseAndKeepRawInput. hasCountryCodeSource will\r\n// // return false if this is the case.\r\n// UNSPECIFIED = 0;\r\n//\r\n// // The country_code is derived based on a phone number with a leading \"+\",\r\n// // e.g. the French number \"+33 1 42 68 53 00\".\r\n// FROM_NUMBER_WITH_PLUS_SIGN = 1;\r\n//\r\n// // The country_code is derived based on a phone number with a leading IDD,\r\n// // e.g. the French number \"011 33 1 42 68 53 00\", as it is dialled from US.\r\n// FROM_NUMBER_WITH_IDD = 5;\r\n//\r\n// // The country_code is derived based on a phone number without a leading\r\n// // \"+\", e.g. the French number \"33 1 42 68 53 00\" when defaultCountry is\r\n// // supplied as France.\r\n// FROM_NUMBER_WITHOUT_PLUS_SIGN = 10;\r\n//\r\n// // The country_code is derived NOT based on the phone number itself, but\r\n// // from the defaultCountry parameter provided in the parsing function by the\r\n// // clients. This happens mostly for numbers written in the national format\r\n// // (without country code). For example, this would be set when parsing the\r\n// // French number \"01 42 68 53 00\", when defaultCountry is supplied as\r\n// // France.\r\n// FROM_DEFAULT_COUNTRY = 20;\r\n// }"],"mappings":"AAAA,OAAOA,cAAP,MAA2B,qBAA3B;AACA,OAAOC,+DAAP,MAA4E,sEAA5E;AACA,OAAOC,QAAP,MAAqB,gBAArB;AACA,SAASC,uBAAT,QAAwC,iBAAxC;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,eAAe,SAASC,yBAAT,CACdC,MADc,EAEdC,OAFc,EAGdC,WAHc,EAIdC,QAJc,EAKb;EACD,IAAI,CAACH,MAAL,EAAa;IACZ,OAAO,EAAP;EACA;;EAED,IAAII,qBAAJ,CALC,CAOD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xB,CAAJ,EAAiD;MAChDH,QAAQ,CAACU,mBAAT,CAA6BP,mBAA7B;MACA,OAAO;QACNE,wBAAwB,EAAEJ,qBAAqB,GAAG,sBAAH,GAA4B,4BADrE;QAENE,kBAAkB,EAAlBA,mBAFM;QAGNN,MAAM,EAAEA,MAAM,CAACW,KAAP,CAAaF,CAAb;MAHF,CAAP;IAKA;;IACDA,CAAC;EACD;;EAED,OAAO,EAAP;AACA,C,CAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA"}
@@ -3,10 +3,12 @@ import metadata from '../../metadata.min.json' assert { type: 'json' };
3
3
  describe('extractCountryCallingCode', function () {
4
4
  it('should extract country calling code from a number', function () {
5
5
  extractCountryCallingCode('+78005553535', null, null, metadata).should.deep.equal({
6
+ countryCallingCodeSource: 'FROM_NUMBER_WITH_PLUS_SIGN',
6
7
  countryCallingCode: '7',
7
8
  number: '8005553535'
8
9
  });
9
10
  extractCountryCallingCode('+7800', null, null, metadata).should.deep.equal({
11
+ countryCallingCodeSource: 'FROM_NUMBER_WITH_PLUS_SIGN',
10
12
  countryCallingCode: '7',
11
13
  number: '800'
12
14
  });
@@ -1 +1 @@
1
- {"version":3,"file":"extractCountryCallingCode.test.js","names":["extractCountryCallingCode","metadata","type","describe","it","should","deep","equal","countryCallingCode","number"],"sources":["../../source/helpers/extractCountryCallingCode.test.js"],"sourcesContent":["import extractCountryCallingCode from './extractCountryCallingCode.js'\r\nimport metadata from '../../metadata.min.json' assert { type: 'json' }\r\n\r\ndescribe('extractCountryCallingCode', () => {\r\n\tit('should extract country calling code from a number', () => {\r\n\t\textractCountryCallingCode('+78005553535', null, null, metadata).should.deep.equal({\r\n\t\t\tcountryCallingCode: '7',\r\n\t\t\tnumber: '8005553535'\r\n\t\t})\r\n\r\n\t\textractCountryCallingCode('+7800', null, null, metadata).should.deep.equal({\r\n\t\t\tcountryCallingCode: '7',\r\n\t\t\tnumber: '800'\r\n\t\t})\r\n\r\n\t\textractCountryCallingCode('', null, null, metadata).should.deep.equal({})\r\n\t})\r\n})\r\n"],"mappings":"AAAA,OAAOA,yBAAP,MAAsC,gCAAtC;AACA,OAAOC,QAAP,MAAqB,yBAArB,UAAwDC,IAAI,EAAE,MAA9D;AAEAC,QAAQ,CAAC,2BAAD,EAA8B,YAAM;EAC3CC,EAAE,CAAC,mDAAD,EAAsD,YAAM;IAC7DJ,yBAAyB,CAAC,cAAD,EAAiB,IAAjB,EAAuB,IAAvB,EAA6BC,QAA7B,CAAzB,CAAgEI,MAAhE,CAAuEC,IAAvE,CAA4EC,KAA5E,CAAkF;MACjFC,kBAAkB,EAAE,GAD6D;MAEjFC,MAAM,EAAE;IAFyE,CAAlF;IAKAT,yBAAyB,CAAC,OAAD,EAAU,IAAV,EAAgB,IAAhB,EAAsBC,QAAtB,CAAzB,CAAyDI,MAAzD,CAAgEC,IAAhE,CAAqEC,KAArE,CAA2E;MAC1EC,kBAAkB,EAAE,GADsD;MAE1EC,MAAM,EAAE;IAFkE,CAA3E;IAKAT,yBAAyB,CAAC,EAAD,EAAK,IAAL,EAAW,IAAX,EAAiBC,QAAjB,CAAzB,CAAoDI,MAApD,CAA2DC,IAA3D,CAAgEC,KAAhE,CAAsE,EAAtE;EACA,CAZC,CAAF;AAaA,CAdO,CAAR"}
1
+ {"version":3,"file":"extractCountryCallingCode.test.js","names":["extractCountryCallingCode","metadata","type","describe","it","should","deep","equal","countryCallingCodeSource","countryCallingCode","number"],"sources":["../../source/helpers/extractCountryCallingCode.test.js"],"sourcesContent":["import extractCountryCallingCode from './extractCountryCallingCode.js'\r\nimport metadata from '../../metadata.min.json' assert { type: 'json' }\r\n\r\ndescribe('extractCountryCallingCode', () => {\r\n\tit('should extract country calling code from a number', () => {\r\n\t\textractCountryCallingCode('+78005553535', null, null, metadata).should.deep.equal({\r\n\t\t\tcountryCallingCodeSource: 'FROM_NUMBER_WITH_PLUS_SIGN',\r\n\t\t\tcountryCallingCode: '7',\r\n\t\t\tnumber: '8005553535'\r\n\t\t})\r\n\r\n\t\textractCountryCallingCode('+7800', null, null, metadata).should.deep.equal({\r\n\t\t\tcountryCallingCodeSource: 'FROM_NUMBER_WITH_PLUS_SIGN',\r\n\t\t\tcountryCallingCode: '7',\r\n\t\t\tnumber: '800'\r\n\t\t})\r\n\r\n\t\textractCountryCallingCode('', null, null, metadata).should.deep.equal({})\r\n\t})\r\n})\r\n"],"mappings":"AAAA,OAAOA,yBAAP,MAAsC,gCAAtC;AACA,OAAOC,QAAP,MAAqB,yBAArB,UAAwDC,IAAI,EAAE,MAA9D;AAEAC,QAAQ,CAAC,2BAAD,EAA8B,YAAM;EAC3CC,EAAE,CAAC,mDAAD,EAAsD,YAAM;IAC7DJ,yBAAyB,CAAC,cAAD,EAAiB,IAAjB,EAAuB,IAAvB,EAA6BC,QAA7B,CAAzB,CAAgEI,MAAhE,CAAuEC,IAAvE,CAA4EC,KAA5E,CAAkF;MACjFC,wBAAwB,EAAE,4BADuD;MAEjFC,kBAAkB,EAAE,GAF6D;MAGjFC,MAAM,EAAE;IAHyE,CAAlF;IAMAV,yBAAyB,CAAC,OAAD,EAAU,IAAV,EAAgB,IAAhB,EAAsBC,QAAtB,CAAzB,CAAyDI,MAAzD,CAAgEC,IAAhE,CAAqEC,KAArE,CAA2E;MAC1EC,wBAAwB,EAAE,4BADgD;MAE1EC,kBAAkB,EAAE,GAFsD;MAG1EC,MAAM,EAAE;IAHkE,CAA3E;IAMAV,yBAAyB,CAAC,EAAD,EAAK,IAAL,EAAW,IAAX,EAAiBC,QAAjB,CAAzB,CAAoDI,MAApD,CAA2DC,IAA3D,CAAgEC,KAAhE,CAAsE,EAAtE;EACA,CAdC,CAAF;AAeA,CAhBO,CAAR"}
@@ -0,0 +1,70 @@
1
+ import extractPhoneContext, { isPhoneContextValid, PLUS_SIGN, RFC3966_PREFIX_, RFC3966_PHONE_CONTEXT_, RFC3966_ISDN_SUBADDRESS_ } from './extractPhoneContext.js';
2
+ import ParseError from '../ParseError.js';
3
+ /**
4
+ * @param {string} numberToParse
5
+ * @param {string} nationalNumber
6
+ * @return {}
7
+ */
8
+
9
+ export default function extractFormattedPhoneNumberFromPossibleRfc3966NumberUri(numberToParse, _ref) {
10
+ var extractFormattedPhoneNumber = _ref.extractFormattedPhoneNumber;
11
+ var phoneContext = extractPhoneContext(numberToParse);
12
+
13
+ if (!isPhoneContextValid(phoneContext)) {
14
+ throw new ParseError('NOT_A_NUMBER');
15
+ }
16
+
17
+ var phoneNumberString;
18
+
19
+ if (phoneContext === null) {
20
+ // Extract a possible number from the string passed in.
21
+ // (this strips leading characters that could not be the start of a phone number)
22
+ phoneNumberString = extractFormattedPhoneNumber(numberToParse) || '';
23
+ } else {
24
+ phoneNumberString = ''; // If the phone context contains a phone number prefix, we need to capture
25
+ // it, whereas domains will be ignored.
26
+
27
+ if (phoneContext.charAt(0) === PLUS_SIGN) {
28
+ phoneNumberString += phoneContext;
29
+ } // Now append everything between the "tel:" prefix and the phone-context.
30
+ // This should include the national number, an optional extension or
31
+ // isdn-subaddress component. Note we also handle the case when "tel:" is
32
+ // missing, as we have seen in some of the phone number inputs.
33
+ // In that case, we append everything from the beginning.
34
+
35
+
36
+ var indexOfRfc3966Prefix = numberToParse.indexOf(RFC3966_PREFIX_);
37
+ var indexOfNationalNumber; // RFC 3966 "tel:" prefix is preset at this stage because
38
+ // `isPhoneContextValid()` requires it to be present.
39
+
40
+ /* istanbul ignore else */
41
+
42
+ if (indexOfRfc3966Prefix >= 0) {
43
+ indexOfNationalNumber = indexOfRfc3966Prefix + RFC3966_PREFIX_.length;
44
+ } else {
45
+ indexOfNationalNumber = 0;
46
+ }
47
+
48
+ var indexOfPhoneContext = numberToParse.indexOf(RFC3966_PHONE_CONTEXT_);
49
+ phoneNumberString += numberToParse.substring(indexOfNationalNumber, indexOfPhoneContext);
50
+ } // Delete the isdn-subaddress and everything after it if it is present.
51
+ // Note extension won't appear at the same time with isdn-subaddress
52
+ // according to paragraph 5.3 of the RFC3966 spec.
53
+
54
+
55
+ var indexOfIsdn = phoneNumberString.indexOf(RFC3966_ISDN_SUBADDRESS_);
56
+
57
+ if (indexOfIsdn > 0) {
58
+ phoneNumberString = phoneNumberString.substring(0, indexOfIsdn);
59
+ } // If both phone context and isdn-subaddress are absent but other
60
+ // parameters are present, the parameters are left in nationalNumber.
61
+ // This is because we are concerned about deleting content from a potential
62
+ // number string when there is no strong evidence that the number is
63
+ // actually written in RFC3966.
64
+
65
+
66
+ if (phoneNumberString !== '') {
67
+ return phoneNumberString;
68
+ }
69
+ }
70
+ //# sourceMappingURL=extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js","names":["extractPhoneContext","isPhoneContextValid","PLUS_SIGN","RFC3966_PREFIX_","RFC3966_PHONE_CONTEXT_","RFC3966_ISDN_SUBADDRESS_","ParseError","extractFormattedPhoneNumberFromPossibleRfc3966NumberUri","numberToParse","extractFormattedPhoneNumber","phoneContext","phoneNumberString","charAt","indexOfRfc3966Prefix","indexOf","indexOfNationalNumber","length","indexOfPhoneContext","substring","indexOfIsdn"],"sources":["../../source/helpers/extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js"],"sourcesContent":["import extractPhoneContext, {\r\n\tisPhoneContextValid,\r\n\tPLUS_SIGN,\r\n\tRFC3966_PREFIX_,\r\n\tRFC3966_PHONE_CONTEXT_,\r\n\tRFC3966_ISDN_SUBADDRESS_\r\n} from './extractPhoneContext.js'\r\n\r\nimport ParseError from '../ParseError.js'\r\n\r\n/**\r\n * @param {string} numberToParse\r\n * @param {string} nationalNumber\r\n * @return {}\r\n */\r\nexport default function extractFormattedPhoneNumberFromPossibleRfc3966NumberUri(numberToParse, {\r\n\textractFormattedPhoneNumber\r\n}) {\r\n\tconst phoneContext = extractPhoneContext(numberToParse)\r\n\tif (!isPhoneContextValid(phoneContext)) {\r\n\t\tthrow new ParseError('NOT_A_NUMBER')\r\n\t}\r\n\r\n\tlet phoneNumberString\r\n\r\n\tif (phoneContext === null) {\r\n\t\t// Extract a possible number from the string passed in.\r\n\t\t// (this strips leading characters that could not be the start of a phone number)\r\n\t\tphoneNumberString = extractFormattedPhoneNumber(numberToParse) || ''\r\n\t} else {\r\n\t\tphoneNumberString = ''\r\n\r\n\t\t// If the phone context contains a phone number prefix, we need to capture\r\n\t\t// it, whereas domains will be ignored.\r\n\t\tif (phoneContext.charAt(0) === PLUS_SIGN) {\r\n\t\t\tphoneNumberString += phoneContext\r\n\t\t}\r\n\r\n\t\t// Now append everything between the \"tel:\" prefix and the phone-context.\r\n\t\t// This should include the national number, an optional extension or\r\n\t\t// isdn-subaddress component. Note we also handle the case when \"tel:\" is\r\n\t\t// missing, as we have seen in some of the phone number inputs.\r\n\t\t// In that case, we append everything from the beginning.\r\n\t\tconst indexOfRfc3966Prefix = numberToParse.indexOf(RFC3966_PREFIX_)\r\n\t\tlet indexOfNationalNumber\r\n\t\t// RFC 3966 \"tel:\" prefix is preset at this stage because\r\n\t\t// `isPhoneContextValid()` requires it to be present.\r\n\t\t/* istanbul ignore else */\r\n\t\tif (indexOfRfc3966Prefix >= 0) {\r\n\t\t\tindexOfNationalNumber = indexOfRfc3966Prefix + RFC3966_PREFIX_.length\r\n\t\t} else {\r\n\t\t\tindexOfNationalNumber = 0\r\n\t\t}\r\n\t\tconst indexOfPhoneContext = numberToParse.indexOf(RFC3966_PHONE_CONTEXT_)\r\n\t\tphoneNumberString += numberToParse.substring(indexOfNationalNumber, indexOfPhoneContext)\r\n\t}\r\n\r\n\t// Delete the isdn-subaddress and everything after it if it is present.\r\n\t// Note extension won't appear at the same time with isdn-subaddress\r\n\t// according to paragraph 5.3 of the RFC3966 spec.\r\n\tconst indexOfIsdn = phoneNumberString.indexOf(RFC3966_ISDN_SUBADDRESS_)\r\n\tif (indexOfIsdn > 0) {\r\n\t\tphoneNumberString = phoneNumberString.substring(0, indexOfIsdn)\r\n\t}\r\n\t// If both phone context and isdn-subaddress are absent but other\r\n\t// parameters are present, the parameters are left in nationalNumber.\r\n\t// This is because we are concerned about deleting content from a potential\r\n\t// number string when there is no strong evidence that the number is\r\n\t// actually written in RFC3966.\r\n\r\n\tif (phoneNumberString !== '') {\r\n\t\treturn phoneNumberString\r\n\t}\r\n}"],"mappings":"AAAA,OAAOA,mBAAP,IACCC,mBADD,EAECC,SAFD,EAGCC,eAHD,EAICC,sBAJD,EAKCC,wBALD,QAMO,0BANP;AAQA,OAAOC,UAAP,MAAuB,kBAAvB;AAEA;AACA;AACA;AACA;AACA;;AACA,eAAe,SAASC,uDAAT,CAAiEC,aAAjE,QAEZ;EAAA,IADFC,2BACE,QADFA,2BACE;EACF,IAAMC,YAAY,GAAGV,mBAAmB,CAACQ,aAAD,CAAxC;;EACA,IAAI,CAACP,mBAAmB,CAACS,YAAD,CAAxB,EAAwC;IACvC,MAAM,IAAIJ,UAAJ,CAAe,cAAf,CAAN;EACA;;EAED,IAAIK,iBAAJ;;EAEA,IAAID,YAAY,KAAK,IAArB,EAA2B;IAC1B;IACA;IACAC,iBAAiB,GAAGF,2BAA2B,CAACD,aAAD,CAA3B,IAA8C,EAAlE;EACA,CAJD,MAIO;IACNG,iBAAiB,GAAG,EAApB,CADM,CAGN;IACA;;IACA,IAAID,YAAY,CAACE,MAAb,CAAoB,CAApB,MAA2BV,SAA/B,EAA0C;MACzCS,iBAAiB,IAAID,YAArB;IACA,CAPK,CASN;IACA;IACA;IACA;IACA;;;IACA,IAAMG,oBAAoB,GAAGL,aAAa,CAACM,OAAd,CAAsBX,eAAtB,CAA7B;IACA,IAAIY,qBAAJ,CAfM,CAgBN;IACA;;IACA;;IACA,IAAIF,oBAAoB,IAAI,CAA5B,EAA+B;MAC9BE,qBAAqB,GAAGF,oBAAoB,GAAGV,eAAe,CAACa,MAA/D;IACA,CAFD,MAEO;MACND,qBAAqB,GAAG,CAAxB;IACA;;IACD,IAAME,mBAAmB,GAAGT,aAAa,CAACM,OAAd,CAAsBV,sBAAtB,CAA5B;IACAO,iBAAiB,IAAIH,aAAa,CAACU,SAAd,CAAwBH,qBAAxB,EAA+CE,mBAA/C,CAArB;EACA,CAtCC,CAwCF;EACA;EACA;;;EACA,IAAME,WAAW,GAAGR,iBAAiB,CAACG,OAAlB,CAA0BT,wBAA1B,CAApB;;EACA,IAAIc,WAAW,GAAG,CAAlB,EAAqB;IACpBR,iBAAiB,GAAGA,iBAAiB,CAACO,SAAlB,CAA4B,CAA5B,EAA+BC,WAA/B,CAApB;EACA,CA9CC,CA+CF;EACA;EACA;EACA;EACA;;;EAEA,IAAIR,iBAAiB,KAAK,EAA1B,EAA8B;IAC7B,OAAOA,iBAAP;EACA;AACD"}
@@ -0,0 +1,82 @@
1
+ // When phone numbers are written in `RFC3966` format — `"tel:+12133734253"` —
2
+ // they can have their "calling code" part written separately in a `phone-context` parameter.
3
+ // Example: `"tel:12133734253;phone-context=+1"`.
4
+ // This function parses the full phone number from the local number and the `phone-context`
5
+ // when the `phone-context` contains a `+` sign.
6
+ import { VALID_DIGITS // PLUS_CHARS
7
+ } from '../constants.js';
8
+ export var PLUS_SIGN = '+';
9
+ var RFC3966_VISUAL_SEPARATOR_ = '[\\-\\.\\(\\)]?';
10
+ var RFC3966_PHONE_DIGIT_ = '(' + '[' + VALID_DIGITS + ']' + '|' + RFC3966_VISUAL_SEPARATOR_ + ')';
11
+ var RFC3966_GLOBAL_NUMBER_DIGITS_ = '^' + '\\' + PLUS_SIGN + RFC3966_PHONE_DIGIT_ + '*' + '[' + VALID_DIGITS + ']' + RFC3966_PHONE_DIGIT_ + '*' + '$';
12
+ /**
13
+ * Regular expression of valid global-number-digits for the phone-context
14
+ * parameter, following the syntax defined in RFC3966.
15
+ */
16
+
17
+ var RFC3966_GLOBAL_NUMBER_DIGITS_PATTERN_ = new RegExp(RFC3966_GLOBAL_NUMBER_DIGITS_, 'g'); // In this port of Google's library, we don't accept alpha characters in phone numbers.
18
+ // const ALPHANUM_ = VALID_ALPHA_ + VALID_DIGITS
19
+
20
+ var ALPHANUM_ = VALID_DIGITS;
21
+ var RFC3966_DOMAINLABEL_ = '[' + ALPHANUM_ + ']+((\\-)*[' + ALPHANUM_ + '])*';
22
+ var VALID_ALPHA_ = 'a-zA-Z';
23
+ var RFC3966_TOPLABEL_ = '[' + VALID_ALPHA_ + ']+((\\-)*[' + ALPHANUM_ + '])*';
24
+ var RFC3966_DOMAINNAME_ = '^(' + RFC3966_DOMAINLABEL_ + '\\.)*' + RFC3966_TOPLABEL_ + '\\.?$';
25
+ /**
26
+ * Regular expression of valid domainname for the phone-context parameter,
27
+ * following the syntax defined in RFC3966.
28
+ */
29
+
30
+ var RFC3966_DOMAINNAME_PATTERN_ = new RegExp(RFC3966_DOMAINNAME_, 'g');
31
+ export var RFC3966_PREFIX_ = 'tel:';
32
+ export var RFC3966_PHONE_CONTEXT_ = ';phone-context=';
33
+ export var RFC3966_ISDN_SUBADDRESS_ = ';isub=';
34
+ /**
35
+ * Extracts the value of the phone-context parameter of `numberToExtractFrom`,
36
+ * following the syntax defined in RFC3966.
37
+ *
38
+ * @param {string} numberToExtractFrom
39
+ * @return {string|null} the extracted string (possibly empty), or `null` if no phone-context parameter is found.
40
+ */
41
+
42
+ export default function extractPhoneContext(numberToExtractFrom) {
43
+ var indexOfPhoneContext = numberToExtractFrom.indexOf(RFC3966_PHONE_CONTEXT_); // If no phone-context parameter is present
44
+
45
+ if (indexOfPhoneContext < 0) {
46
+ return null;
47
+ }
48
+
49
+ var phoneContextStart = indexOfPhoneContext + RFC3966_PHONE_CONTEXT_.length; // If phone-context parameter is empty
50
+
51
+ if (phoneContextStart >= numberToExtractFrom.length) {
52
+ return '';
53
+ }
54
+
55
+ var phoneContextEnd = numberToExtractFrom.indexOf(';', phoneContextStart); // If phone-context is not the last parameter
56
+
57
+ if (phoneContextEnd >= 0) {
58
+ return numberToExtractFrom.substring(phoneContextStart, phoneContextEnd);
59
+ } else {
60
+ return numberToExtractFrom.substring(phoneContextStart);
61
+ }
62
+ }
63
+ /**
64
+ * Returns whether the value of phoneContext follows the syntax defined in RFC3966.
65
+ *
66
+ * @param {string|null} phoneContext
67
+ * @return {boolean}
68
+ */
69
+
70
+ export function isPhoneContextValid(phoneContext) {
71
+ if (phoneContext === null) {
72
+ return true;
73
+ }
74
+
75
+ if (phoneContext.length === 0) {
76
+ return false;
77
+ } // Does phone-context value match pattern of global-number-digits or domainname.
78
+
79
+
80
+ return RFC3966_GLOBAL_NUMBER_DIGITS_PATTERN_.test(phoneContext) || RFC3966_DOMAINNAME_PATTERN_.test(phoneContext);
81
+ }
82
+ //# sourceMappingURL=extractPhoneContext.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extractPhoneContext.js","names":["VALID_DIGITS","PLUS_SIGN","RFC3966_VISUAL_SEPARATOR_","RFC3966_PHONE_DIGIT_","RFC3966_GLOBAL_NUMBER_DIGITS_","RFC3966_GLOBAL_NUMBER_DIGITS_PATTERN_","RegExp","ALPHANUM_","RFC3966_DOMAINLABEL_","VALID_ALPHA_","RFC3966_TOPLABEL_","RFC3966_DOMAINNAME_","RFC3966_DOMAINNAME_PATTERN_","RFC3966_PREFIX_","RFC3966_PHONE_CONTEXT_","RFC3966_ISDN_SUBADDRESS_","extractPhoneContext","numberToExtractFrom","indexOfPhoneContext","indexOf","phoneContextStart","length","phoneContextEnd","substring","isPhoneContextValid","phoneContext","test"],"sources":["../../source/helpers/extractPhoneContext.js"],"sourcesContent":["// When phone numbers are written in `RFC3966` format — `\"tel:+12133734253\"` —\r\n// they can have their \"calling code\" part written separately in a `phone-context` parameter.\r\n// Example: `\"tel:12133734253;phone-context=+1\"`.\r\n// This function parses the full phone number from the local number and the `phone-context`\r\n// when the `phone-context` contains a `+` sign.\r\n\r\nimport {\r\n VALID_DIGITS,\r\n // PLUS_CHARS\r\n} from '../constants.js'\r\n\r\nexport const PLUS_SIGN = '+'\r\n\r\nconst RFC3966_VISUAL_SEPARATOR_ = '[\\\\-\\\\.\\\\(\\\\)]?'\r\n\r\nconst RFC3966_PHONE_DIGIT_ = '(' + '[' + VALID_DIGITS + ']' + '|' + RFC3966_VISUAL_SEPARATOR_ + ')'\r\n\r\nconst RFC3966_GLOBAL_NUMBER_DIGITS_ =\r\n\t'^' +\r\n\t'\\\\' +\r\n\tPLUS_SIGN +\r\n\tRFC3966_PHONE_DIGIT_ +\r\n\t'*' +\r\n\t'[' + VALID_DIGITS + ']' +\r\n\tRFC3966_PHONE_DIGIT_ +\r\n\t'*' +\r\n\t'$'\r\n\r\n/**\r\n * Regular expression of valid global-number-digits for the phone-context\r\n * parameter, following the syntax defined in RFC3966.\r\n */\r\nconst RFC3966_GLOBAL_NUMBER_DIGITS_PATTERN_ = new RegExp(RFC3966_GLOBAL_NUMBER_DIGITS_, 'g')\r\n\r\n// In this port of Google's library, we don't accept alpha characters in phone numbers.\r\n// const ALPHANUM_ = VALID_ALPHA_ + VALID_DIGITS\r\nconst ALPHANUM_ = VALID_DIGITS\r\n\r\nconst RFC3966_DOMAINLABEL_ = '[' + ALPHANUM_ + ']+((\\\\-)*[' + ALPHANUM_ + '])*'\r\n\r\nconst VALID_ALPHA_ = 'a-zA-Z'\r\nconst RFC3966_TOPLABEL_ = '[' + VALID_ALPHA_ + ']+((\\\\-)*[' + ALPHANUM_ + '])*'\r\n\r\nconst RFC3966_DOMAINNAME_ = '^(' + RFC3966_DOMAINLABEL_ + '\\\\.)*' + RFC3966_TOPLABEL_ + '\\\\.?$'\r\n\r\n/**\r\n * Regular expression of valid domainname for the phone-context parameter,\r\n * following the syntax defined in RFC3966.\r\n */\r\nconst RFC3966_DOMAINNAME_PATTERN_ = new RegExp(RFC3966_DOMAINNAME_, 'g')\r\n\r\nexport const RFC3966_PREFIX_ = 'tel:'\r\nexport const RFC3966_PHONE_CONTEXT_ = ';phone-context='\r\nexport const RFC3966_ISDN_SUBADDRESS_ = ';isub='\r\n\r\n/**\r\n * Extracts the value of the phone-context parameter of `numberToExtractFrom`,\r\n * following the syntax defined in RFC3966.\r\n *\r\n * @param {string} numberToExtractFrom\r\n * @return {string|null} the extracted string (possibly empty), or `null` if no phone-context parameter is found.\r\n */\r\nexport default function extractPhoneContext(numberToExtractFrom) {\r\n\tconst indexOfPhoneContext = numberToExtractFrom.indexOf(RFC3966_PHONE_CONTEXT_)\r\n\t// If no phone-context parameter is present\r\n\tif (indexOfPhoneContext < 0) {\r\n\t\treturn null\r\n\t}\r\n\r\n\tconst phoneContextStart = indexOfPhoneContext + RFC3966_PHONE_CONTEXT_.length\r\n\t// If phone-context parameter is empty\r\n\tif (phoneContextStart >= numberToExtractFrom.length) {\r\n\t\treturn ''\r\n\t}\r\n\r\n\tconst phoneContextEnd = numberToExtractFrom.indexOf(';', phoneContextStart)\r\n\t// If phone-context is not the last parameter\r\n\tif (phoneContextEnd >= 0) {\r\n\t\treturn numberToExtractFrom.substring(phoneContextStart, phoneContextEnd)\r\n\t} else {\r\n\t\treturn numberToExtractFrom.substring(phoneContextStart)\r\n\t}\r\n}\r\n\r\n/**\r\n * Returns whether the value of phoneContext follows the syntax defined in RFC3966.\r\n *\r\n * @param {string|null} phoneContext\r\n * @return {boolean}\r\n */\r\nexport function isPhoneContextValid(phoneContext) {\r\n\tif (phoneContext === null) {\r\n\t\treturn true\r\n\t}\r\n\r\n\tif (phoneContext.length === 0) {\r\n\t\treturn false\r\n\t}\r\n\r\n\t// Does phone-context value match pattern of global-number-digits or domainname.\r\n\treturn RFC3966_GLOBAL_NUMBER_DIGITS_PATTERN_.test(phoneContext) ||\r\n\t\tRFC3966_DOMAINNAME_PATTERN_.test(phoneContext)\r\n}"],"mappings":"AAAA;AACA;AACA;AACA;AACA;AAEA,SACEA,YADF,CAEE;AAFF,OAGO,iBAHP;AAKA,OAAO,IAAMC,SAAS,GAAG,GAAlB;AAEP,IAAMC,yBAAyB,GAAG,iBAA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,IAAID,iBAAiB,IAAIH,mBAAmB,CAACI,MAA7C,EAAqD;IACpD,OAAO,EAAP;EACA;;EAED,IAAMC,eAAe,GAAGL,mBAAmB,CAACE,OAApB,CAA4B,GAA5B,EAAiCC,iBAAjC,CAAxB,CAbgE,CAchE;;EACA,IAAIE,eAAe,IAAI,CAAvB,EAA0B;IACzB,OAAOL,mBAAmB,CAACM,SAApB,CAA8BH,iBAA9B,EAAiDE,eAAjD,CAAP;EACA,CAFD,MAEO;IACN,OAAOL,mBAAmB,CAACM,SAApB,CAA8BH,iBAA9B,CAAP;EACA;AACD;AAED;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,SAASI,mBAAT,CAA6BC,YAA7B,EAA2C;EACjD,IAAIA,YAAY,KAAK,IAArB,EAA2B;IAC1B,OAAO,IAAP;EACA;;EAED,IAAIA,YAAY,CAACJ,MAAb,KAAwB,CAA5B,EAA+B;IAC9B,OAAO,KAAP;EACA,CAPgD,CASjD;;;EACA,OAAOhB,qCAAqC,CAACqB,IAAtC,CAA2CD,YAA3C,KACNb,2BAA2B,CAACc,IAA5B,CAAiCD,YAAjC,CADD;AAEA"}
@@ -0,0 +1,66 @@
1
+ import parsePhoneNumber_ from '../parsePhoneNumber.js';
2
+ import PhoneNumber from '../PhoneNumber.js';
3
+ import metadata from '../../metadata.min.json' assert { type: 'json' };
4
+
5
+ function parsePhoneNumber() {
6
+ for (var _len = arguments.length, parameters = new Array(_len), _key = 0; _key < _len; _key++) {
7
+ parameters[_key] = arguments[_key];
8
+ }
9
+
10
+ parameters.push(metadata);
11
+ return parsePhoneNumber_.apply(this, parameters);
12
+ }
13
+
14
+ describe('extractPhoneContext', function () {
15
+ it('should parse RFC 3966 phone number URIs', function () {
16
+ // context = ";phone-context=" descriptor
17
+ // descriptor = domainname / global-number-digits
18
+ var NZ_NUMBER = new PhoneNumber('64', '33316005', metadata); // Valid global-phone-digits
19
+
20
+ expectPhoneNumbersToBeEqual(parsePhoneNumber('tel:033316005;phone-context=+64'), NZ_NUMBER);
21
+ expectPhoneNumbersToBeEqual(parsePhoneNumber('tel:033316005;phone-context=+64;{this isn\'t part of phone-context anymore!}'), NZ_NUMBER);
22
+ var nzFromPhoneContext = new PhoneNumber('64', '3033316005', metadata);
23
+ expectPhoneNumbersToBeEqual(parsePhoneNumber('tel:033316005;phone-context=+64-3'), nzFromPhoneContext);
24
+ var brFromPhoneContext = new PhoneNumber('55', '5033316005', metadata);
25
+ expectPhoneNumbersToBeEqual(parsePhoneNumber('tel:033316005;phone-context=+(555)'), brFromPhoneContext);
26
+ var usFromPhoneContext = new PhoneNumber('1', '23033316005', metadata);
27
+ expectPhoneNumbersToBeEqual(parsePhoneNumber('tel:033316005;phone-context=+-1-2.3()'), usFromPhoneContext); // Valid domainname.
28
+
29
+ expectPhoneNumbersToBeEqual(parsePhoneNumber('tel:033316005;phone-context=abc.nz', 'NZ'), NZ_NUMBER);
30
+ expectPhoneNumbersToBeEqual(parsePhoneNumber('tel:033316005;phone-context=www.PHONE-numb3r.com', 'NZ'), NZ_NUMBER);
31
+ expectPhoneNumbersToBeEqual(parsePhoneNumber('tel:033316005;phone-context=a', 'NZ'), NZ_NUMBER);
32
+ expectPhoneNumbersToBeEqual(parsePhoneNumber('tel:033316005;phone-context=3phone.J.', 'NZ'), NZ_NUMBER);
33
+ expectPhoneNumbersToBeEqual(parsePhoneNumber('tel:033316005;phone-context=a--z', 'NZ'), NZ_NUMBER); // Should strip ISDN subaddress.
34
+
35
+ expectPhoneNumbersToBeEqual(parsePhoneNumber('tel:033316005;isub=/@;phone-context=+64', 'NZ'), NZ_NUMBER); // // Should support incorrectly-written RFC 3966 phone numbers:
36
+ // // the ones written without a `tel:` prefix.
37
+ // expectPhoneNumbersToBeEqual(
38
+ // parsePhoneNumber('033316005;phone-context=+64', 'NZ'),
39
+ // NZ_NUMBER
40
+ // )
41
+ // Invalid descriptor.
42
+
43
+ expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=');
44
+ expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=+');
45
+ expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=64');
46
+ expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=++64');
47
+ expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=+abc');
48
+ expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=.');
49
+ expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=3phone');
50
+ expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=a-.nz');
51
+ expectToThrowForInvalidPhoneContext('tel:033316005;phone-context=a{b}c');
52
+ });
53
+ });
54
+
55
+ function expectToThrowForInvalidPhoneContext(string) {
56
+ expect(parsePhoneNumber(string)).to.be.undefined;
57
+ }
58
+
59
+ function expectPhoneNumbersToBeEqual(phoneNumber1, phoneNumber2) {
60
+ if (!phoneNumber1 || !phoneNumber2) {
61
+ return false;
62
+ }
63
+
64
+ return phoneNumber1.number === phoneNumber2.number && phoneNumber1.ext === phoneNumber2.ext;
65
+ }
66
+ //# sourceMappingURL=extractPhoneContext.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"extractPhoneContext.test.js","names":["parsePhoneNumber_","PhoneNumber","metadata","type","parsePhoneNumber","parameters","push","apply","describe","it","NZ_NUMBER","expectPhoneNumbersToBeEqual","nzFromPhoneContext","brFromPhoneContext","usFromPhoneContext","expectToThrowForInvalidPhoneContext","string","expect","to","be","undefined","phoneNumber1","phoneNumber2","number","ext"],"sources":["../../source/helpers/extractPhoneContext.test.js"],"sourcesContent":["import parsePhoneNumber_ from '../parsePhoneNumber.js'\r\nimport PhoneNumber from '../PhoneNumber.js'\r\nimport metadata from '../../metadata.min.json' assert { type: 'json' }\r\n\r\nfunction parsePhoneNumber(...parameters) {\r\n\tparameters.push(metadata)\r\n\treturn parsePhoneNumber_.apply(this, parameters)\r\n}\r\n\r\ndescribe('extractPhoneContext', function() {\r\n\tit('should parse RFC 3966 phone number URIs', function() {\r\n\t\t// context = \";phone-context=\" descriptor\r\n\t\t// descriptor = domainname / global-number-digits\r\n\r\n\t\tconst NZ_NUMBER = new PhoneNumber('64', '33316005', metadata)\r\n\r\n\t\t// Valid global-phone-digits\r\n\t\texpectPhoneNumbersToBeEqual(\r\n\t\t\tparsePhoneNumber('tel:033316005;phone-context=+64'),\r\n\t\t\tNZ_NUMBER\r\n\t\t)\r\n\r\n\t\texpectPhoneNumbersToBeEqual(\r\n\t\t\tparsePhoneNumber('tel:033316005;phone-context=+64;{this isn\\'t part of phone-context anymore!}'),\r\n\t\t\tNZ_NUMBER\r\n\t\t)\r\n\r\n\t\tconst nzFromPhoneContext = new PhoneNumber('64', '3033316005', metadata)\r\n\t\texpectPhoneNumbersToBeEqual(\r\n\t\t\tparsePhoneNumber('tel:033316005;phone-context=+64-3'),\r\n\t\t\tnzFromPhoneContext\r\n\t\t)\r\n\r\n\t\tconst brFromPhoneContext = new PhoneNumber('55', '5033316005', metadata)\r\n\t\texpectPhoneNumbersToBeEqual(\r\n\t\t\tparsePhoneNumber('tel:033316005;phone-context=+(555)'),\r\n\t\t\tbrFromPhoneContext\r\n\t\t)\r\n\r\n\t\tconst usFromPhoneContext = new PhoneNumber('1', '23033316005', metadata)\r\n\t\texpectPhoneNumbersToBeEqual(\r\n\t\t\tparsePhoneNumber('tel:033316005;phone-context=+-1-2.3()'),\r\n\t\t\tusFromPhoneContext\r\n\t\t)\r\n\r\n\t\t// Valid domainname.\r\n\t\texpectPhoneNumbersToBeEqual(\r\n\t\t\tparsePhoneNumber('tel:033316005;phone-context=abc.nz', 'NZ'),\r\n\t\t\tNZ_NUMBER\r\n\t\t)\r\n\t\texpectPhoneNumbersToBeEqual(\r\n\t\t\tparsePhoneNumber('tel:033316005;phone-context=www.PHONE-numb3r.com', 'NZ'),\r\n\t\t\tNZ_NUMBER\r\n\t\t)\r\n\t\texpectPhoneNumbersToBeEqual(\r\n\t\t\tparsePhoneNumber('tel:033316005;phone-context=a', 'NZ'),\r\n\t\t\tNZ_NUMBER\r\n\t\t)\r\n\t\texpectPhoneNumbersToBeEqual(\r\n\t\t\tparsePhoneNumber('tel:033316005;phone-context=3phone.J.', 'NZ'),\r\n\t\t\tNZ_NUMBER\r\n\t\t)\r\n\t\texpectPhoneNumbersToBeEqual(\r\n\t\t\tparsePhoneNumber('tel:033316005;phone-context=a--z', 'NZ'),\r\n\t\t\tNZ_NUMBER\r\n\t\t)\r\n\r\n\t\t// Should strip ISDN subaddress.\r\n\t\texpectPhoneNumbersToBeEqual(\r\n\t\t\tparsePhoneNumber('tel:033316005;isub=/@;phone-context=+64', 'NZ'),\r\n\t\t\tNZ_NUMBER\r\n\t\t)\r\n\r\n\t\t// // Should support incorrectly-written RFC 3966 phone numbers:\r\n\t\t// // the ones written without a `tel:` prefix.\r\n\t\t// expectPhoneNumbersToBeEqual(\r\n\t\t// \tparsePhoneNumber('033316005;phone-context=+64', 'NZ'),\r\n\t\t// \tNZ_NUMBER\r\n\t\t// )\r\n\r\n\t\t// Invalid descriptor.\r\n\t\texpectToThrowForInvalidPhoneContext('tel:033316005;phone-context=')\r\n\t\texpectToThrowForInvalidPhoneContext('tel:033316005;phone-context=+')\r\n\t\texpectToThrowForInvalidPhoneContext('tel:033316005;phone-context=64')\r\n\t\texpectToThrowForInvalidPhoneContext('tel:033316005;phone-context=++64')\r\n\t\texpectToThrowForInvalidPhoneContext('tel:033316005;phone-context=+abc')\r\n\t\texpectToThrowForInvalidPhoneContext('tel:033316005;phone-context=.')\r\n\t\texpectToThrowForInvalidPhoneContext('tel:033316005;phone-context=3phone')\r\n\t\texpectToThrowForInvalidPhoneContext('tel:033316005;phone-context=a-.nz')\r\n\t\texpectToThrowForInvalidPhoneContext('tel:033316005;phone-context=a{b}c')\r\n\t})\r\n})\r\n\r\nfunction expectToThrowForInvalidPhoneContext(string) {\r\n\texpect(parsePhoneNumber(string)).to.be.undefined\r\n}\r\n\r\nfunction expectPhoneNumbersToBeEqual(phoneNumber1, phoneNumber2) {\r\n\tif (!phoneNumber1 || !phoneNumber2) {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C,YAAtB,EAAoC;IACnC,OAAO,KAAP;EACA;;EACD,OAAOD,YAAY,CAACE,MAAb,KAAwBD,YAAY,CAACC,MAArC,IACNF,YAAY,CAACG,GAAb,KAAqBF,YAAY,CAACE,GADnC;AAEA"}
package/es6/parse.js CHANGED
@@ -9,14 +9,15 @@ import isViablePhoneNumber, { isViablePhoneNumberStart } from './helpers/isViabl
9
9
  import extractExtension from './helpers/extension/extractExtension.js';
10
10
  import parseIncompletePhoneNumber from './parseIncompletePhoneNumber.js';
11
11
  import getCountryCallingCode from './getCountryCallingCode.js';
12
- import { isPossibleNumber } from './isPossible.js';
13
- import { parseRFC3966 } from './helpers/RFC3966.js';
12
+ import { isPossibleNumber } from './isPossible.js'; // import { parseRFC3966 } from './helpers/RFC3966.js'
13
+
14
14
  import PhoneNumber from './PhoneNumber.js';
15
15
  import matchesEntirely from './helpers/matchesEntirely.js';
16
16
  import extractCountryCallingCode from './helpers/extractCountryCallingCode.js';
17
17
  import extractNationalNumber from './helpers/extractNationalNumber.js';
18
18
  import stripIddPrefix from './helpers/stripIddPrefix.js';
19
- import getCountryByCallingCode from './helpers/getCountryByCallingCode.js'; // We don't allow input strings for parsing to be longer than 250 chars.
19
+ import getCountryByCallingCode from './helpers/getCountryByCallingCode.js';
20
+ import extractFormattedPhoneNumberFromPossibleRfc3966NumberUri from './helpers/extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js'; // We don't allow input strings for parsing to be longer than 250 chars.
20
21
  // This prevents malicious input from consuming CPU.
21
22
 
22
23
  var MAX_INPUT_STRING_LENGTH = 250; // This consists of the plus symbol, digits, and arabic-indic digits.
@@ -88,6 +89,7 @@ export default function parse(text, options, metadata) {
88
89
  country = _parsePhoneNumber.country,
89
90
  nationalNumber = _parsePhoneNumber.nationalNumber,
90
91
  countryCallingCode = _parsePhoneNumber.countryCallingCode,
92
+ countryCallingCodeSource = _parsePhoneNumber.countryCallingCodeSource,
91
93
  carrierCode = _parsePhoneNumber.carrierCode;
92
94
 
93
95
  if (!metadata.hasSelectedNumberingPlan()) {
@@ -144,6 +146,7 @@ export default function parse(text, options, metadata) {
144
146
  phoneNumber.ext = ext;
145
147
  }
146
148
 
149
+ phoneNumber.__countryCallingCodeSource = countryCallingCodeSource;
147
150
  return phoneNumber;
148
151
  } // Check if national phone number pattern matches the number.
149
152
  // National number pattern is different for each country,
@@ -180,7 +183,7 @@ export default function parse(text, options, metadata) {
180
183
  * extractFormattedPhoneNumber("Call (213) 373-4253 for assistance.")
181
184
  */
182
185
 
183
- function extractFormattedPhoneNumber(text, extract, throwOnError) {
186
+ function _extractFormattedPhoneNumber(text, extract, throwOnError) {
184
187
  if (!text) {
185
188
  return;
186
189
  }
@@ -217,12 +220,16 @@ function extractFormattedPhoneNumber(text, extract, throwOnError) {
217
220
 
218
221
 
219
222
  function parseInput(text, v2, extract) {
220
- // Parse RFC 3966 phone number URI.
221
- if (text && text.indexOf('tel:') === 0) {
222
- return parseRFC3966(text);
223
- }
224
-
225
- var number = extractFormattedPhoneNumber(text, extract, v2); // If the phone number is not viable, then abort.
223
+ // // Parse RFC 3966 phone number URI.
224
+ // if (text && text.indexOf('tel:') === 0) {
225
+ // return parseRFC3966(text)
226
+ // }
227
+ // let number = extractFormattedPhoneNumber(text, extract, v2)
228
+ var number = extractFormattedPhoneNumberFromPossibleRfc3966NumberUri(text, {
229
+ extractFormattedPhoneNumber: function extractFormattedPhoneNumber(text) {
230
+ return _extractFormattedPhoneNumber(text, extract, v2);
231
+ }
232
+ }); // If the phone number is not viable, then abort.
226
233
 
227
234
  if (!number) {
228
235
  return {};
@@ -280,6 +287,7 @@ function result(country, nationalNumber, ext) {
280
287
  function parsePhoneNumber(formattedPhoneNumber, defaultCountry, defaultCallingCode, metadata) {
281
288
  // Extract calling code from phone number.
282
289
  var _extractCountryCallin = extractCountryCallingCode(parseIncompletePhoneNumber(formattedPhoneNumber), defaultCountry, defaultCallingCode, metadata.metadata),
290
+ countryCallingCodeSource = _extractCountryCallin.countryCallingCodeSource,
283
291
  countryCallingCode = _extractCountryCallin.countryCallingCode,
284
292
  number = _extractCountryCallin.number; // Choose a country by `countryCallingCode`.
285
293
 
@@ -309,6 +317,7 @@ function parsePhoneNumber(formattedPhoneNumber, defaultCountry, defaultCallingCo
309
317
 
310
318
  if (!number) {
311
319
  return {
320
+ countryCallingCodeSource: countryCallingCodeSource,
312
321
  countryCallingCode: countryCallingCode
313
322
  };
314
323
  }
@@ -344,6 +353,7 @@ function parsePhoneNumber(formattedPhoneNumber, defaultCountry, defaultCallingCo
344
353
  return {
345
354
  country: country,
346
355
  countryCallingCode: countryCallingCode,
356
+ countryCallingCodeSource: countryCallingCodeSource,
347
357
  nationalNumber: nationalNumber,
348
358
  carrierCode: carrierCode
349
359
  };