intlFormat.js 3.9 KB

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