format.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
  2. function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  3. 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); }
  4. 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; }
  5. function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
  6. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  7. import _formatNumber from '../format.js';
  8. import parse from '../parse.js';
  9. import isObject from '../helpers/isObject.js';
  10. export default function formatNumber() {
  11. var _normalizeArguments = normalizeArguments(arguments),
  12. input = _normalizeArguments.input,
  13. format = _normalizeArguments.format,
  14. options = _normalizeArguments.options,
  15. metadata = _normalizeArguments.metadata;
  16. return _formatNumber(input, format, options, metadata);
  17. } // Sort out arguments
  18. function normalizeArguments(args) {
  19. var _Array$prototype$slic = Array.prototype.slice.call(args),
  20. _Array$prototype$slic2 = _slicedToArray(_Array$prototype$slic, 5),
  21. arg_1 = _Array$prototype$slic2[0],
  22. arg_2 = _Array$prototype$slic2[1],
  23. arg_3 = _Array$prototype$slic2[2],
  24. arg_4 = _Array$prototype$slic2[3],
  25. arg_5 = _Array$prototype$slic2[4];
  26. var input;
  27. var format;
  28. var options;
  29. var metadata; // Sort out arguments.
  30. // If the phone number is passed as a string.
  31. // `format('8005553535', ...)`.
  32. if (typeof arg_1 === 'string') {
  33. // If country code is supplied.
  34. // `format('8005553535', 'RU', 'NATIONAL', [options], metadata)`.
  35. if (typeof arg_3 === 'string') {
  36. format = arg_3;
  37. if (arg_5) {
  38. options = arg_4;
  39. metadata = arg_5;
  40. } else {
  41. metadata = arg_4;
  42. }
  43. input = parse(arg_1, {
  44. defaultCountry: arg_2,
  45. extended: true
  46. }, metadata);
  47. } // Just an international phone number is supplied
  48. // `format('+78005553535', 'NATIONAL', [options], metadata)`.
  49. else {
  50. if (typeof arg_2 !== 'string') {
  51. throw new Error('`format` argument not passed to `formatNumber(number, format)`');
  52. }
  53. format = arg_2;
  54. if (arg_4) {
  55. options = arg_3;
  56. metadata = arg_4;
  57. } else {
  58. metadata = arg_3;
  59. }
  60. input = parse(arg_1, {
  61. extended: true
  62. }, metadata);
  63. }
  64. } // If the phone number is passed as a parsed number object.
  65. // `format({ phone: '8005553535', country: 'RU' }, 'NATIONAL', [options], metadata)`.
  66. else if (isObject(arg_1)) {
  67. input = arg_1;
  68. format = arg_2;
  69. if (arg_4) {
  70. options = arg_3;
  71. metadata = arg_4;
  72. } else {
  73. metadata = arg_3;
  74. }
  75. } else throw new TypeError('A phone number must either be a string or an object of shape { phone, [country] }.'); // Legacy lowercase formats.
  76. if (format === 'International') {
  77. format = 'INTERNATIONAL';
  78. } else if (format === 'National') {
  79. format = 'NATIONAL';
  80. }
  81. return {
  82. input: input,
  83. format: format,
  84. options: options,
  85. metadata: metadata
  86. };
  87. }
  88. //# sourceMappingURL=format.js.map