index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = intlFormat;
  7. var _index = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  8. /**
  9. * @name intlFormat
  10. * @category Common Helpers
  11. * @summary Format the date with Intl.DateTimeFormat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat).
  12. *
  13. * @description
  14. * Return the formatted date string in the given format.
  15. * The method uses [`Intl.DateTimeFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat) inside.
  16. * formatOptions are the same as [`Intl.DateTimeFormat` options](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat#using_options)
  17. *
  18. * > ⚠️ Please note that before Node version 13.0.0, only the locale data for en-US is available by default.
  19. *
  20. * @param {Date|Number} argument - the original date.
  21. * @param {Object} [formatOptions] - an object with options.
  22. * @param {'lookup'|'best fit'} [formatOptions.localeMatcher='best fit'] - locale selection algorithm.
  23. * @param {'narrow'|'short'|'long'} [formatOptions.weekday] - representation the days of the week.
  24. * @param {'narrow'|'short'|'long'} [formatOptions.era] - representation of eras.
  25. * @param {'numeric'|'2-digit'} [formatOptions.year] - representation of years.
  26. * @param {'numeric'|'2-digit'|'narrow'|'short'|'long'} [formatOptions.month='numeric'] - representation of month.
  27. * @param {'numeric'|'2-digit'} [formatOptions.day='numeric'] - representation of day.
  28. * @param {'numeric'|'2-digit'} [formatOptions.hour='numeric'] - representation of hours.
  29. * @param {'numeric'|'2-digit'} [formatOptions.minute] - representation of minutes.
  30. * @param {'numeric'|'2-digit'} [formatOptions.second] - representation of seconds.
  31. * @param {'short'|'long'} [formatOptions.timeZoneName] - representation of names of time zones.
  32. * @param {'basic'|'best fit'} [formatOptions.formatMatcher='best fit'] - format selection algorithm.
  33. * @param {Boolean} [formatOptions.hour12] - determines whether to use 12-hour time format.
  34. * @param {String} [formatOptions.timeZone] - the time zone to use.
  35. * @param {Object} [localeOptions] - an object with locale.
  36. * @param {String|String[]} [localeOptions.locale] - the locale code
  37. * @returns {String} the formatted date string.
  38. * @throws {TypeError} 1 argument required.
  39. * @throws {RangeError} `date` must not be Invalid Date
  40. *
  41. * @example
  42. * // Represent 10 October 2019 in German.
  43. * // Convert the date with format's options and locale's options.
  44. * const result = intlFormat(new Date(2019, 9, 4, 12, 30, 13, 456), {
  45. * weekday: 'long',
  46. * year: 'numeric',
  47. * month: 'long',
  48. * day: 'numeric',
  49. * }, {
  50. * locale: 'de-DE',
  51. * })
  52. * //=> Freitag, 4. Oktober 2019
  53. *
  54. * @example
  55. * // Represent 10 October 2019.
  56. * // Convert the date with format's options.
  57. * const result = intlFormat.default(new Date(2019, 9, 4, 12, 30, 13, 456), {
  58. * year: 'numeric',
  59. * month: 'numeric',
  60. * day: 'numeric',
  61. * hour: 'numeric',
  62. * })
  63. * //=> 10/4/2019, 12 PM
  64. *
  65. * @example
  66. * // Represent 10 October 2019 in Korean.
  67. * // Convert the date with locale's options.
  68. * const result = intlFormat(new Date(2019, 9, 4, 12, 30, 13, 456), {
  69. * locale: 'ko-KR',
  70. * })
  71. * //=> 2019. 10. 4.
  72. *
  73. * @example
  74. * // Represent 10 October 2019 in middle-endian format:
  75. * const result = intlFormat(new Date(2019, 9, 4, 12, 30, 13, 456))
  76. * //=> 10/4/2019
  77. */
  78. function intlFormat(date, formatOrLocale, localeOptions) {
  79. var _localeOptions;
  80. (0, _index.default)(1, arguments);
  81. var formatOptions;
  82. if (isFormatOptions(formatOrLocale)) {
  83. formatOptions = formatOrLocale;
  84. } else {
  85. localeOptions = formatOrLocale;
  86. }
  87. return new Intl.DateTimeFormat((_localeOptions = localeOptions) === null || _localeOptions === void 0 ? void 0 : _localeOptions.locale, formatOptions).format(date);
  88. }
  89. function isFormatOptions(opts) {
  90. return opts !== undefined && !('locale' in opts);
  91. }
  92. module.exports = exports.default;