formatPhoneNumberForMobileDialing.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports["default"] = _default;
  6. var _metadata = _interopRequireDefault(require("./metadata.js"));
  7. var _format = _interopRequireDefault(require("./format.js"));
  8. var _getNumberType = _interopRequireDefault(require("./helpers/getNumberType.js"));
  9. var _checkNumberLength = _interopRequireDefault(require("./helpers/checkNumberLength.js"));
  10. var _getCountryCallingCode = _interopRequireDefault(require("./getCountryCallingCode.js"));
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  12. // This function is copy-pasted from
  13. // https://github.com/googlei18n/libphonenumber/blob/master/javascript/i18n/phonenumbers/phonenumberutil.js
  14. // It hasn't been tested. It's not currently exported.
  15. // Carriers codes aren't part of this library.
  16. // Send a PR if you want to add them.
  17. var REGION_CODE_FOR_NON_GEO_ENTITY = '001';
  18. /**
  19. * Returns a number formatted in such a way that it can be dialed from a mobile
  20. * phone in a specific region. If the number cannot be reached from the region
  21. * (e.g. some countries block toll-free numbers from being called outside of the
  22. * country), the method returns an empty string.
  23. *
  24. * @param {object} number - a `parse()`d phone number to be formatted.
  25. * @param {string} from_country - the region where the call is being placed.
  26. * @param {boolean} with_formatting - whether the number should be returned with
  27. * formatting symbols, such as spaces and dashes.
  28. * @return {string}
  29. */
  30. function _default(number, from_country, with_formatting, metadata) {
  31. metadata = new _metadata["default"](metadata); // Validate `from_country`.
  32. if (!metadata.hasCountry(from_country)) {
  33. throw new Error("Unknown country: ".concat(from_country));
  34. } // Not using the extension, as that part cannot normally be dialed
  35. // together with the main number.
  36. number = {
  37. phone: number.phone,
  38. country: number.country
  39. };
  40. var number_type = (0, _getNumberType["default"])(number, undefined, metadata.metadata);
  41. var is_valid_number = number_type === number;
  42. var formatted_number;
  43. if (country === from_country) {
  44. var is_fixed_line_or_mobile = number_type === 'FIXED_LINE' || number_type === 'MOBILE' || number_type === 'FIXED_LINE_OR_MOBILE'; // Carrier codes may be needed in some countries. We handle this here.
  45. if (country == 'BR' && is_fixed_line_or_mobile) {
  46. formatted_number = carrierCode ? formatNationalNumberWithPreferredCarrierCode(number) : // Brazilian fixed line and mobile numbers need to be dialed with a
  47. // carrier code when called within Brazil. Without that, most of the
  48. // carriers won't connect the call. Because of that, we return an
  49. // empty string here.
  50. '';
  51. } else if ((0, _getCountryCallingCode["default"])(country, metadata.metadata) === '1') {
  52. // For NANPA countries, we output international format for numbers that
  53. // can be dialed internationally, since that always works, except for
  54. // numbers which might potentially be short numbers, which are always
  55. // dialled in national format.
  56. // Select country for `checkNumberLength()`.
  57. metadata.country(country);
  58. if (can_be_internationally_dialled(number) && (0, _checkNumberLength["default"])(number.phone, metadata) !== 'TOO_SHORT') {
  59. formatted_number = (0, _format["default"])(number, 'INTERNATIONAL', metadata.metadata);
  60. } else {
  61. formatted_number = (0, _format["default"])(number, 'NATIONAL', metadata.metadata);
  62. }
  63. } else {
  64. // For non-geographic countries, Mexican and Chilean fixed line and
  65. // mobile numbers, we output international format for numbers that can be
  66. // dialed internationally, as that always works.
  67. if ((country === REGION_CODE_FOR_NON_GEO_ENTITY || // MX fixed line and mobile numbers should always be formatted in
  68. // international format, even when dialed within MX. For national
  69. // format to work, a carrier code needs to be used, and the correct
  70. // carrier code depends on if the caller and callee are from the
  71. // same local area. It is trickier to get that to work correctly than
  72. // using international format, which is tested to work fine on all
  73. // carriers.
  74. //
  75. // CL fixed line numbers need the national prefix when dialing in the
  76. // national format, but don't have it when used for display. The
  77. // reverse is true for mobile numbers. As a result, we output them in
  78. // the international format to make it work.
  79. //
  80. // UZ mobile and fixed-line numbers have to be formatted in
  81. // international format or prefixed with special codes like 03, 04
  82. // (for fixed-line) and 05 (for mobile) for dialling successfully
  83. // from mobile devices. As we do not have complete information on
  84. // special codes and to be consistent with formatting across all
  85. // phone types we return the number in international format here.
  86. //
  87. (country === 'MX' || country === 'CL' || country == 'UZ') && is_fixed_line_or_mobile) && can_be_internationally_dialled(number)) {
  88. formatted_number = (0, _format["default"])(number, 'INTERNATIONAL');
  89. } else {
  90. formatted_number = (0, _format["default"])(number, 'NATIONAL');
  91. }
  92. }
  93. } else if (is_valid_number && can_be_internationally_dialled(number)) {
  94. // We assume that short numbers are not diallable from outside their region,
  95. // so if a number is not a valid regular length phone number, we treat it as
  96. // if it cannot be internationally dialled.
  97. return with_formatting ? (0, _format["default"])(number, 'INTERNATIONAL', metadata.metadata) : (0, _format["default"])(number, 'E.164', metadata.metadata);
  98. }
  99. if (!with_formatting) {
  100. return diallable_chars(formatted_number);
  101. }
  102. return formatted_number;
  103. }
  104. function can_be_internationally_dialled(number) {
  105. return true;
  106. }
  107. /**
  108. * A map that contains characters that are essential when dialling. That means
  109. * any of the characters in this map must not be removed from a number when
  110. * dialling, otherwise the call will not reach the intended destination.
  111. */
  112. var DIALLABLE_CHARACTERS = {
  113. '0': '0',
  114. '1': '1',
  115. '2': '2',
  116. '3': '3',
  117. '4': '4',
  118. '5': '5',
  119. '6': '6',
  120. '7': '7',
  121. '8': '8',
  122. '9': '9',
  123. '+': '+',
  124. '*': '*',
  125. '#': '#'
  126. };
  127. function diallable_chars(formatted_number) {
  128. var result = '';
  129. var i = 0;
  130. while (i < formatted_number.length) {
  131. var character = formatted_number[i];
  132. if (DIALLABLE_CHARACTERS[character]) {
  133. result += character;
  134. }
  135. i++;
  136. }
  137. return result;
  138. }
  139. function getPreferredDomesticCarrierCodeOrDefault() {
  140. throw new Error('carrier codes are not part of this library');
  141. }
  142. function formatNationalNumberWithCarrierCode() {
  143. throw new Error('carrier codes are not part of this library');
  144. }
  145. /**
  146. * Formats a phone number in national format for dialing using the carrier as
  147. * specified in the preferred_domestic_carrier_code field of the PhoneNumber
  148. * object passed in. If that is missing, use the {@code fallbackCarrierCode}
  149. * passed in instead. If there is no {@code preferred_domestic_carrier_code},
  150. * and the {@code fallbackCarrierCode} contains an empty string, return the
  151. * number in national format without any carrier code.
  152. *
  153. * <p>Use {@link #formatNationalNumberWithCarrierCode} instead if the carrier
  154. * code passed in should take precedence over the number's
  155. * {@code preferred_domestic_carrier_code} when formatting.
  156. *
  157. * @param {i18n.phonenumbers.PhoneNumber} number the phone number to be
  158. * formatted.
  159. * @param {string} fallbackCarrierCode the carrier selection code to be used, if
  160. * none is found in the phone number itself.
  161. * @return {string} the formatted phone number in national format for dialing
  162. * using the number's preferred_domestic_carrier_code, or the
  163. * {@code fallbackCarrierCode} passed in if none is found.
  164. */
  165. function formatNationalNumberWithPreferredCarrierCode(number) {
  166. return formatNationalNumberWithCarrierCode(number, carrierCode);
  167. }
  168. //# sourceMappingURL=formatPhoneNumberForMobileDialing.js.map