extractCountryCallingCode.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports["default"] = extractCountryCallingCode;
  6. var _stripIddPrefix = _interopRequireDefault(require("./stripIddPrefix.js"));
  7. var _extractCountryCallingCodeFromInternationalNumberWithoutPlusSign = _interopRequireDefault(require("./extractCountryCallingCodeFromInternationalNumberWithoutPlusSign.js"));
  8. var _metadata = _interopRequireDefault(require("../metadata.js"));
  9. var _constants = require("../constants.js");
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  11. /**
  12. * Converts a phone number digits (possibly with a `+`)
  13. * into a calling code and the rest phone number digits.
  14. * The "rest phone number digits" could include
  15. * a national prefix, carrier code, and national
  16. * (significant) number.
  17. * @param {string} number — Phone number digits (possibly with a `+`).
  18. * @param {string} [country] — Default country.
  19. * @param {string} [callingCode] — Default calling code (some phone numbering plans are non-geographic).
  20. * @param {object} metadata
  21. * @return {object} `{ countryCallingCodeSource: string?, countryCallingCode: string?, number: string }`
  22. * @example
  23. * // Returns `{ countryCallingCode: "1", number: "2133734253" }`.
  24. * extractCountryCallingCode('2133734253', 'US', null, metadata)
  25. * extractCountryCallingCode('2133734253', null, '1', metadata)
  26. * extractCountryCallingCode('+12133734253', null, null, metadata)
  27. * extractCountryCallingCode('+12133734253', 'RU', null, metadata)
  28. */
  29. function extractCountryCallingCode(number, country, callingCode, metadata) {
  30. if (!number) {
  31. return {};
  32. }
  33. var isNumberWithIddPrefix; // If this is not an international phone number,
  34. // then either extract an "IDD" prefix, or extract a
  35. // country calling code from a number by autocorrecting it
  36. // by prepending a leading `+` in cases when it starts
  37. // with the country calling code.
  38. // https://wikitravel.org/en/International_dialling_prefix
  39. // https://github.com/catamphetamine/libphonenumber-js/issues/376
  40. if (number[0] !== '+') {
  41. // Convert an "out-of-country" dialing phone number
  42. // to a proper international phone number.
  43. var numberWithoutIDD = (0, _stripIddPrefix["default"])(number, country, callingCode, metadata); // If an IDD prefix was stripped then
  44. // convert the number to international one
  45. // for subsequent parsing.
  46. if (numberWithoutIDD && numberWithoutIDD !== number) {
  47. isNumberWithIddPrefix = true;
  48. number = '+' + numberWithoutIDD;
  49. } else {
  50. // Check to see if the number starts with the country calling code
  51. // for the default country. If so, we remove the country calling code,
  52. // and do some checks on the validity of the number before and after.
  53. // https://github.com/catamphetamine/libphonenumber-js/issues/376
  54. if (country || callingCode) {
  55. var _extractCountryCallin = (0, _extractCountryCallingCodeFromInternationalNumberWithoutPlusSign["default"])(number, country, callingCode, metadata),
  56. countryCallingCode = _extractCountryCallin.countryCallingCode,
  57. shorterNumber = _extractCountryCallin.number;
  58. if (countryCallingCode) {
  59. return {
  60. countryCallingCodeSource: 'FROM_NUMBER_WITHOUT_PLUS_SIGN',
  61. countryCallingCode: countryCallingCode,
  62. number: shorterNumber
  63. };
  64. }
  65. }
  66. return {
  67. // No need to set it to `UNSPECIFIED`. It can be just `undefined`.
  68. // countryCallingCodeSource: 'UNSPECIFIED',
  69. number: number
  70. };
  71. }
  72. } // Fast abortion: country codes do not begin with a '0'
  73. if (number[1] === '0') {
  74. return {};
  75. }
  76. metadata = new _metadata["default"](metadata); // The thing with country phone codes
  77. // is that they are orthogonal to each other
  78. // i.e. there's no such country phone code A
  79. // for which country phone code B exists
  80. // where B starts with A.
  81. // Therefore, while scanning digits,
  82. // if a valid country code is found,
  83. // that means that it is the country code.
  84. //
  85. var i = 2;
  86. while (i - 1 <= _constants.MAX_LENGTH_COUNTRY_CODE && i <= number.length) {
  87. var _countryCallingCode = number.slice(1, i);
  88. if (metadata.hasCallingCode(_countryCallingCode)) {
  89. metadata.selectNumberingPlan(_countryCallingCode);
  90. return {
  91. countryCallingCodeSource: isNumberWithIddPrefix ? 'FROM_NUMBER_WITH_IDD' : 'FROM_NUMBER_WITH_PLUS_SIGN',
  92. countryCallingCode: _countryCallingCode,
  93. number: number.slice(i)
  94. };
  95. }
  96. i++;
  97. }
  98. return {};
  99. } // The possible values for the returned `countryCallingCodeSource` are:
  100. //
  101. // Copy-pasted from:
  102. // https://github.com/google/libphonenumber/blob/master/resources/phonenumber.proto
  103. //
  104. // // The source from which the country_code is derived. This is not set in the
  105. // // general parsing method, but in the method that parses and keeps raw_input.
  106. // // New fields could be added upon request.
  107. // enum CountryCodeSource {
  108. // // Default value returned if this is not set, because the phone number was
  109. // // created using parse, not parseAndKeepRawInput. hasCountryCodeSource will
  110. // // return false if this is the case.
  111. // UNSPECIFIED = 0;
  112. //
  113. // // The country_code is derived based on a phone number with a leading "+",
  114. // // e.g. the French number "+33 1 42 68 53 00".
  115. // FROM_NUMBER_WITH_PLUS_SIGN = 1;
  116. //
  117. // // The country_code is derived based on a phone number with a leading IDD,
  118. // // e.g. the French number "011 33 1 42 68 53 00", as it is dialled from US.
  119. // FROM_NUMBER_WITH_IDD = 5;
  120. //
  121. // // The country_code is derived based on a phone number without a leading
  122. // // "+", e.g. the French number "33 1 42 68 53 00" when defaultCountry is
  123. // // supplied as France.
  124. // FROM_NUMBER_WITHOUT_PLUS_SIGN = 10;
  125. //
  126. // // The country_code is derived NOT based on the phone number itself, but
  127. // // from the defaultCountry parameter provided in the parsing function by the
  128. // // clients. This happens mostly for numbers written in the national format
  129. // // (without country code). For example, this would be set when parsing the
  130. // // French number "01 42 68 53 00", when defaultCountry is supplied as
  131. // // France.
  132. // FROM_DEFAULT_COUNTRY = 20;
  133. // }
  134. //# sourceMappingURL=extractCountryCallingCode.js.map