format.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (it) return (it = it.call(o)).next.bind(it); if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  2. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  3. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  4. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
  5. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
  6. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  7. // This is a port of Google Android `libphonenumber`'s
  8. // `phonenumberutil.js` of December 31th, 2018.
  9. //
  10. // https://github.com/googlei18n/libphonenumber/commits/master/javascript/i18n/phonenumbers/phonenumberutil.js
  11. import matchesEntirely from './helpers/matchesEntirely.js';
  12. import formatNationalNumberUsingFormat from './helpers/formatNationalNumberUsingFormat.js';
  13. import Metadata, { getCountryCallingCode } from './metadata.js';
  14. import getIddPrefix from './helpers/getIddPrefix.js';
  15. import { formatRFC3966 } from './helpers/RFC3966.js';
  16. var DEFAULT_OPTIONS = {
  17. formatExtension: function formatExtension(formattedNumber, extension, metadata) {
  18. return "".concat(formattedNumber).concat(metadata.ext()).concat(extension);
  19. }
  20. };
  21. /**
  22. * Formats a phone number.
  23. *
  24. * format(phoneNumberInstance, 'INTERNATIONAL', { ..., v2: true }, metadata)
  25. * format(phoneNumberInstance, 'NATIONAL', { ..., v2: true }, metadata)
  26. *
  27. * format({ phone: '8005553535', country: 'RU' }, 'INTERNATIONAL', { ... }, metadata)
  28. * format({ phone: '8005553535', country: 'RU' }, 'NATIONAL', undefined, metadata)
  29. *
  30. * @param {object|PhoneNumber} input — If `options.v2: true` flag is passed, the `input` should be a `PhoneNumber` instance. Otherwise, it should be an object of shape `{ phone: '...', country: '...' }`.
  31. * @param {string} format
  32. * @param {object} [options]
  33. * @param {object} metadata
  34. * @return {string}
  35. */
  36. export default function formatNumber(input, format, options, metadata) {
  37. // Apply default options.
  38. if (options) {
  39. options = _objectSpread(_objectSpread({}, DEFAULT_OPTIONS), options);
  40. } else {
  41. options = DEFAULT_OPTIONS;
  42. }
  43. metadata = new Metadata(metadata);
  44. if (input.country && input.country !== '001') {
  45. // Validate `input.country`.
  46. if (!metadata.hasCountry(input.country)) {
  47. throw new Error("Unknown country: ".concat(input.country));
  48. }
  49. metadata.country(input.country);
  50. } else if (input.countryCallingCode) {
  51. metadata.selectNumberingPlan(input.countryCallingCode);
  52. } else return input.phone || '';
  53. var countryCallingCode = metadata.countryCallingCode();
  54. var nationalNumber = options.v2 ? input.nationalNumber : input.phone; // This variable should have been declared inside `case`s
  55. // but Babel has a bug and it says "duplicate variable declaration".
  56. var number;
  57. switch (format) {
  58. case 'NATIONAL':
  59. // Legacy argument support.
  60. // (`{ country: ..., phone: '' }`)
  61. if (!nationalNumber) {
  62. return '';
  63. }
  64. number = formatNationalNumber(nationalNumber, input.carrierCode, 'NATIONAL', metadata, options);
  65. return addExtension(number, input.ext, metadata, options.formatExtension);
  66. case 'INTERNATIONAL':
  67. // Legacy argument support.
  68. // (`{ country: ..., phone: '' }`)
  69. if (!nationalNumber) {
  70. return "+".concat(countryCallingCode);
  71. }
  72. number = formatNationalNumber(nationalNumber, null, 'INTERNATIONAL', metadata, options);
  73. number = "+".concat(countryCallingCode, " ").concat(number);
  74. return addExtension(number, input.ext, metadata, options.formatExtension);
  75. case 'E.164':
  76. // `E.164` doesn't define "phone number extensions".
  77. return "+".concat(countryCallingCode).concat(nationalNumber);
  78. case 'RFC3966':
  79. return formatRFC3966({
  80. number: "+".concat(countryCallingCode).concat(nationalNumber),
  81. ext: input.ext
  82. });
  83. // For reference, here's Google's IDD formatter:
  84. // https://github.com/google/libphonenumber/blob/32719cf74e68796788d1ca45abc85dcdc63ba5b9/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L1546
  85. // Not saying that this IDD formatter replicates it 1:1, but it seems to work.
  86. // Who would even need to format phone numbers in IDD format anyway?
  87. case 'IDD':
  88. if (!options.fromCountry) {
  89. return; // throw new Error('`fromCountry` option not passed for IDD-prefixed formatting.')
  90. }
  91. var formattedNumber = formatIDD(nationalNumber, input.carrierCode, countryCallingCode, options.fromCountry, metadata);
  92. return addExtension(formattedNumber, input.ext, metadata, options.formatExtension);
  93. default:
  94. throw new Error("Unknown \"format\" argument passed to \"formatNumber()\": \"".concat(format, "\""));
  95. }
  96. }
  97. function formatNationalNumber(number, carrierCode, formatAs, metadata, options) {
  98. var format = chooseFormatForNumber(metadata.formats(), number);
  99. if (!format) {
  100. return number;
  101. }
  102. return formatNationalNumberUsingFormat(number, format, {
  103. useInternationalFormat: formatAs === 'INTERNATIONAL',
  104. withNationalPrefix: format.nationalPrefixIsOptionalWhenFormattingInNationalFormat() && options && options.nationalPrefix === false ? false : true,
  105. carrierCode: carrierCode,
  106. metadata: metadata
  107. });
  108. }
  109. export function chooseFormatForNumber(availableFormats, nationalNnumber) {
  110. for (var _iterator = _createForOfIteratorHelperLoose(availableFormats), _step; !(_step = _iterator()).done;) {
  111. var format = _step.value;
  112. // Validate leading digits.
  113. // The test case for "else path" could be found by searching for
  114. // "format.leadingDigitsPatterns().length === 0".
  115. if (format.leadingDigitsPatterns().length > 0) {
  116. // The last leading_digits_pattern is used here, as it is the most detailed
  117. var lastLeadingDigitsPattern = format.leadingDigitsPatterns()[format.leadingDigitsPatterns().length - 1]; // If leading digits don't match then move on to the next phone number format
  118. if (nationalNnumber.search(lastLeadingDigitsPattern) !== 0) {
  119. continue;
  120. }
  121. } // Check that the national number matches the phone number format regular expression
  122. if (matchesEntirely(nationalNnumber, format.pattern())) {
  123. return format;
  124. }
  125. }
  126. }
  127. function addExtension(formattedNumber, ext, metadata, formatExtension) {
  128. return ext ? formatExtension(formattedNumber, ext, metadata) : formattedNumber;
  129. }
  130. function formatIDD(nationalNumber, carrierCode, countryCallingCode, fromCountry, metadata) {
  131. var fromCountryCallingCode = getCountryCallingCode(fromCountry, metadata.metadata); // When calling within the same country calling code.
  132. if (fromCountryCallingCode === countryCallingCode) {
  133. var formattedNumber = formatNationalNumber(nationalNumber, carrierCode, 'NATIONAL', metadata); // For NANPA regions, return the national format for these regions
  134. // but prefix it with the country calling code.
  135. if (countryCallingCode === '1') {
  136. return countryCallingCode + ' ' + formattedNumber;
  137. } // If regions share a country calling code, the country calling code need
  138. // not be dialled. This also applies when dialling within a region, so this
  139. // if clause covers both these cases. Technically this is the case for
  140. // dialling from La Reunion to other overseas departments of France (French
  141. // Guiana, Martinique, Guadeloupe), but not vice versa - so we don't cover
  142. // this edge case for now and for those cases return the version including
  143. // country calling code. Details here:
  144. // http://www.petitfute.com/voyage/225-info-pratiques-reunion
  145. //
  146. return formattedNumber;
  147. }
  148. var iddPrefix = getIddPrefix(fromCountry, undefined, metadata.metadata);
  149. if (iddPrefix) {
  150. return "".concat(iddPrefix, " ").concat(countryCallingCode, " ").concat(formatNationalNumber(nationalNumber, null, 'INTERNATIONAL', metadata));
  151. }
  152. }
  153. //# sourceMappingURL=format.js.map