Blame view

天文台pc/tianwentai-ui/node_modules/date-fns/intlFormat.mjs 3.96 KB
bc518174   王天杨   提交两个项目文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
  import { toDate } from "./toDate.mjs";
  
  /**
   * The locale string (see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locales_argument).
   */
  
  /**
   * The format options (see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#options)
   */
  
  /**
   * The locale options.
   */
  
  /**
   * @name intlFormat
   * @category Common Helpers
   * @summary Format the date with Intl.DateTimeFormat (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat).
   *
   * @description
   * Return the formatted date string in the given format.
   * The method uses [`Intl.DateTimeFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat) inside.
   * formatOptions are the same as [`Intl.DateTimeFormat` options](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat#using_options)
   *
   * > ⚠️ Please note that before Node version 13.0.0, only the locale data for en-US is available by default.
   *
   * @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).
   *
   * @param date - The date to format
   *
   * @returns The formatted date string
   *
   * @throws `date` must not be Invalid Date
   *
   * @example
   * // Represent 4 October 2019 in middle-endian format:
   * const result = intlFormat(new Date(2019, 9, 4, 12, 30, 13, 456))
   * //=> 10/4/2019
   */
  
  /**
   * @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).
   *
   * @param date - The date to format
   * @param localeOptions - An object with locale
   *
   * @returns The formatted date string
   *
   * @throws `date` must not be Invalid Date
   *
   * @example
   * // Represent 4 October 2019 in Korean.
   * // Convert the date with locale's options.
   * const result = intlFormat(new Date(2019, 9, 4, 12, 30, 13, 456), {
   *   locale: 'ko-KR',
   * })
   * //=> 2019. 10. 4.
   */
  
  /**
   * @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).
   *
   * @param date - The date to format
   * @param formatOptions - The format options
   *
   * @returns The formatted date string
   *
   * @throws `date` must not be Invalid Date
   *
   * @example
   * // Represent 4 October 2019.
   * // Convert the date with format's options.
   * const result = intlFormat.default(new Date(2019, 9, 4, 12, 30, 13, 456), {
   *   year: 'numeric',
   *   month: 'numeric',
   *   day: 'numeric',
   *   hour: 'numeric',
   * })
   * //=> 10/4/2019, 12 PM
   */
  
  /**
   * @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).
   *
   * @param date - The date to format
   * @param formatOptions - The format options
   * @param localeOptions - An object with locale
   *
   * @returns The formatted date string
   *
   * @throws `date` must not be Invalid Date
   *
   * @example
   * // Represent 4 October 2019 in German.
   * // Convert the date with format's options and locale's options.
   * const result = intlFormat(new Date(2019, 9, 4, 12, 30, 13, 456), {
   *   weekday: 'long',
   *   year: 'numeric',
   *   month: 'long',
   *   day: 'numeric',
   * }, {
   *   locale: 'de-DE',
   * })
   * //=> Freitag, 4. Oktober 2019
   */
  
  export function intlFormat(date, formatOrLocale, localeOptions) {
    let formatOptions;
  
    if (isFormatOptions(formatOrLocale)) {
      formatOptions = formatOrLocale;
    } else {
      localeOptions = formatOrLocale;
    }
  
    return new Intl.DateTimeFormat(localeOptions?.locale, formatOptions).format(
      toDate(date),
    );
  }
  
  function isFormatOptions(opts) {
    return opts !== undefined && !("locale" in opts);
  }
  
  // Fallback for modularized imports:
  export default intlFormat;