formatPhoneNumberForMobileDialing.js 7.5 KB

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