intlFormat.cjs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. "use strict";
  2. exports.intlFormat = intlFormat;
  3. var _index = require("./toDate.cjs");
  4. /**
  5. * The locale string (see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locales_argument).
  6. * @deprecated
  7. *
  8. * [TODO] Remove in v4
  9. */
  10. /**
  11. * The format options (see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#options)
  12. */
  13. /**
  14. * The locale options.
  15. */
  16. /**
  17. * @name intlFormat
  18. * @category Common Helpers
  19. * @summary Format the date with Intl.DateTimeFormat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat).
  20. *
  21. * @description
  22. * Return the formatted date string in the given format.
  23. * The method uses [`Intl.DateTimeFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat) inside.
  24. * formatOptions are the same as [`Intl.DateTimeFormat` options](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat#using_options)
  25. *
  26. * > ⚠️ Please note that before Node version 13.0.0, only the locale data for en-US is available by default.
  27. *
  28. * @param date - The date to format
  29. *
  30. * @returns The formatted date string
  31. *
  32. * @throws `date` must not be Invalid Date
  33. *
  34. * @example
  35. * // Represent 4 October 2019 in middle-endian format:
  36. * const result = intlFormat(new Date(2019, 9, 4, 12, 30, 13, 456))
  37. * //=> 10/4/2019
  38. */
  39. /**
  40. * @param date - The date to format
  41. * @param localeOptions - An object with locale
  42. *
  43. * @returns The formatted date string
  44. *
  45. * @throws `date` must not be Invalid Date
  46. *
  47. * @example
  48. * // Represent 4 October 2019 in Korean.
  49. * // Convert the date with locale's options.
  50. * const result = intlFormat(new Date(2019, 9, 4, 12, 30, 13, 456), {
  51. * locale: 'ko-KR',
  52. * })
  53. * //=> 2019. 10. 4.
  54. */
  55. /**
  56. * @param date - The date to format
  57. * @param formatOptions - The format options
  58. *
  59. * @returns The formatted date string
  60. *
  61. * @throws `date` must not be Invalid Date
  62. *
  63. * @example
  64. * // Represent 4 October 2019.
  65. * // Convert the date with format's options.
  66. * const result = intlFormat.default(new Date(2019, 9, 4, 12, 30, 13, 456), {
  67. * year: 'numeric',
  68. * month: 'numeric',
  69. * day: 'numeric',
  70. * hour: 'numeric',
  71. * })
  72. * //=> 10/4/2019, 12 PM
  73. */
  74. /**
  75. * @param date - The date to format
  76. * @param formatOptions - The format options
  77. * @param localeOptions - An object with locale
  78. *
  79. * @returns The formatted date string
  80. *
  81. * @throws `date` must not be Invalid Date
  82. *
  83. * @example
  84. * // Represent 4 October 2019 in German.
  85. * // Convert the date with format's options and locale's options.
  86. * const result = intlFormat(new Date(2019, 9, 4, 12, 30, 13, 456), {
  87. * weekday: 'long',
  88. * year: 'numeric',
  89. * month: 'long',
  90. * day: 'numeric',
  91. * }, {
  92. * locale: 'de-DE',
  93. * })
  94. * //=> Freitag, 4. Oktober 2019
  95. */
  96. function intlFormat(date, formatOrLocale, localeOptions) {
  97. let formatOptions;
  98. if (isFormatOptions(formatOrLocale)) {
  99. formatOptions = formatOrLocale;
  100. } else {
  101. localeOptions = formatOrLocale;
  102. }
  103. return new Intl.DateTimeFormat(localeOptions?.locale, formatOptions).format(
  104. (0, _index.toDate)(date),
  105. );
  106. }
  107. function isFormatOptions(opts) {
  108. return opts !== undefined && !("locale" in opts);
  109. }