-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.ts
330 lines (310 loc) · 10.2 KB
/
index.ts
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
* Copyright 2022, SumUp Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type {
FormattableDate,
FormattableDateTime,
FormattableTime,
Locale,
} from '../../types/index.js';
import { DATE_STYLES, TIME_STYLES } from '../../data/date-time-styles.js';
import {
getDateTimeFormat,
isDateTimeFormatSupported,
isDateTimeFormatToPartsSupported,
isDateTimeStyleSupported,
} from './intl.js';
export { isDateTimeFormatSupported, isDateTimeFormatToPartsSupported };
/**
* Formats a `Date` with support for various
* [date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#datestyle)
* and [time](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#timestyle) styles.
*
* @example
* import { formatDateTime } from '@sumup-oss/intl';
*
* formatDateTime(new Date(2000, 1, 1), 'de-DE'); // '1.2.2000'
* formatDateTime(new Date(2000, 1, 1), ['ban', 'id']); // '1/2/2000'
* formatDateTime(new Date(2000, 1, 1, 12, 30), 'en-GB', {
* year: 'numeric',
* month: 'short',
* day: 'numeric',
* hour: '2-digit',
* minute: '2-digit',
* }); // 1 Feb 2000, 12:30
*
* @remarks
* In runtimes that don't support the `Intl.DateTimeFormat` API, the date is
* formatted using the `Date.toLocale(Date|Time)String` API.
*
* In runtimes that don't support the `dateStyle` and `timeStyle` options, the
* styles are approximated using fallback options.
*
* @category Date & Time
*/
export const formatDateTime = formatDateTimeFactory();
/**
* Formats a `Date` with support for various
* [date styles](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#datestyle).
*
* @example
* import { formatDate } from '@sumup-oss/intl';
*
* const date = new Date(2000, 1, 1);
* const locale = 'en-GB';
*
* formatDate(date, locale, 'short'); // '01/02/2000'
* formatDate(date, locale, 'medium'); // '1 Feb 2000'
* formatDate(date, locale, 'long'); // '1 February 2000'
* formatDate(date, locale, 'full'); // 'Tuesday, 1 February 2000'
*
* @remarks
* In runtimes that don't support the `Intl.DateTimeFormat` API, the date is
* formatted using the `Date.toLocale(Date|Time)String` API.
*
* In runtimes that don't support the `dateStyle` option, the styles are
* approximated using fallback options.
*
* @category Date & Time
*/
export function formatDate(
date: FormattableDate, // in UTC
locales?: Locale | Locale[],
dateStyle: Intl.DateTimeFormatOptions['dateStyle'] = 'short',
) {
return formatDateTime(date, locales, { dateStyle });
}
/**
* Formats a `Date` with support for various
* [time styles](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#datestyle).
*
* @example
* import { formatTime } from '@sumup-oss/intl';
*
* const time = new Date(2000, 1, 1, 9, 55);
* const locale = 'en-GB';
*
* formatTime(time, locale, 'short'); // '09:55'
* formatTime(time, locale, 'medium'); // '09:55:00'
* formatTime(time, locale, 'long'); // '09:55:00 CET'
* formatTime(time, locale, 'full'); // '09:55:00 Central European Standard Time'
*
* @remarks
* In runtimes that don't support the `Intl.DateTimeFormat` API, the date is
* formatted using the `Date.toLocale(Date|Time)String` API.
*
* In runtimes that don't support the `timeStyle` option, the styles are
* approximated using fallback options.
*
* @category Date & Time
*/
export function formatTime(
date: FormattableTime, // in UTC
locales?: Locale | Locale[],
timeStyle: Intl.DateTimeFormatOptions['timeStyle'] = 'short',
) {
return formatDateTime(date, locales, { timeStyle });
}
function formatDateTimeFactory(): (
date: FormattableDateTime, // in UTC
locales?: Locale | Locale[],
options?: Intl.DateTimeFormatOptions,
) => string {
if (!isDateTimeFormatSupported) {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
return (date, _locales, options) => {
const fallbackOptions = getFallbackOptions(options);
const includeDate = hasOptions(fallbackOptions, [
'dateStyle',
'day',
'month',
'year',
]);
const includeTime = hasOptions(fallbackOptions, [
'timeStyle',
'hour',
'minute',
'second',
]);
if (includeDate && includeTime) {
return date.toLocaleString();
}
if (includeDate && 'toLocaleDateString' in date) {
return date.toLocaleDateString();
}
if (includeTime && 'toLocaleTimeString' in date) {
return date.toLocaleTimeString();
}
return date.toLocaleString();
};
}
return (date, locales, options) => {
const fallbackOptions = getFallbackOptions(options);
const dateTimeFormat = getDateTimeFormat(locales, fallbackOptions);
return dateTimeFormat.format(date);
};
}
/**
* Formats a `Date` to parts with support for various
* [date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#datestyle)
* and [time](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat#timestyle) styles.
*
* @example
* import { formatDateTimeToParts } from '@sumup-oss/intl';
*
* const time = new Date(2000, 1, 1, 9, 55);
*
* formatDateTimeToParts(date, 'de-DE');
* // [
* // { type: 'day', value: '1' },
* // { type: 'literal', value: '.' },
* // { type: 'month', value: '2' },
* // { type: 'literal', value: '.' },
* // { type: 'year', value: '2000' },
* // ]
* formatDateTimeToParts(date, ['ban', 'id']);
* // [
* // { type: 'day', value: '1' },
* // { type: 'literal', value: '/' },
* // { type: 'month', value: '2' },
* // { type: 'literal', value: '/' },
* // { type: 'year', value: '2000' },
* // ]
* formatDateTimeToParts(date, 'en-GB', {
* year: 'numeric',
* month: 'short',
* day: 'numeric',
* });
* // [
* // ({ type: 'day', value: '1' },
* // { type: 'literal', value: ' ' },
* // { type: 'month', value: 'Feb' },
* // { type: 'literal', value: ' ' },
* // { type: 'year', value: '2000' })
* // ]
*
* @remarks
* In runtimes that don't support the `Intl.DateTimeFormat.formatToParts` API,
* the date is localized and returned as a single string literal part.
*
* @category Date & Time
*/
export const formatDateTimeToParts = formatDateTimeToPartsFactory();
function formatDateTimeToPartsFactory(): (
date: FormattableDateTime, // in UTC
locales?: Locale | Locale[],
options?: Intl.DateTimeFormatOptions,
) => (Intl.DateTimeFormatPart | { type: 'date'; value: string })[] {
if (!isDateTimeFormatToPartsSupported) {
return (date, locales, options) => {
// In runtimes that don't support formatting to parts, the date is
// localized and returned as a single string literal part.
const value = formatDateTime(date, locales, options);
return [{ type: 'literal', value }];
};
}
return (date, locales, options) => {
const fallbackOptions = getFallbackOptions(options);
const dateTimeFormat = getDateTimeFormat(locales, fallbackOptions);
return dateTimeFormat.formatToParts(date);
};
}
/**
* Resolves the locale and collation options that are used to format a `Date`.
*
* @example
* import { resolveDateTimeFormat } from '@sumup-oss/intl';
*
* resolveDateTimeFormat();
* // {
* // 'locale': 'en-DE',
* // 'calendar': 'gregory',
* // 'numberingSystem': 'latn',
* // 'timeZone': 'Europe/Berlin',
* // 'year': 'numeric',
* // 'month': '2-digit',
* // 'day': '2-digit'
* // }
* resolveDateTimeFormat(['ban', 'id']);
* // {
* // 'locale': 'id',
* // 'calendar': 'gregory',
* // 'numberingSystem': 'latn',
* // 'timeZone': 'Europe/Berlin',
* // 'year': 'numeric',
* // 'month': 'numeric',
* // 'day': 'numeric'
* // }
* resolveDateTimeFormat('en-GB', {
* year: 'numeric',
* month: 'short',
* day: 'numeric',
* });
* // {
* // 'locale': 'en-GB',
* // 'calendar': 'gregory',
* // 'numberingSystem': 'latn',
* // 'timeZone': 'Europe/Berlin',
* // 'year': 'numeric',
* // 'month': 'short',
* // 'day': 'numeric'
* // }
*
* @remarks
* In runtimes that don't support the `Intl.DateTimeFormat.resolvedOptions` API,
* `null` is returned.
*
* @category Date & Time
*/
export const resolveDateTimeFormat = resolveDateTimeFormatFactory();
function resolveDateTimeFormatFactory(): (
locales?: Locale | Locale[],
options?: Intl.DateTimeFormatOptions,
) => null | Intl.ResolvedDateTimeFormatOptions {
if (!isDateTimeFormatSupported) {
return () => null;
}
return (locales, options) => {
const fallbackOptions = getFallbackOptions(options);
const dateTimeFormat = getDateTimeFormat(locales, fallbackOptions);
return dateTimeFormat.resolvedOptions();
};
}
function hasOptions(
options: Intl.DateTimeFormatOptions | undefined,
keys: (keyof Intl.DateTimeFormatOptions)[],
) {
return options ? keys.some((key) => Boolean(options[key])) : false;
}
// The `dateStyle` and `timeStyle` options were added to the spec recently.
// In runtimes that don't support them, the styles are approximated using
// fallback options.
function getFallbackOptions(options?: Intl.DateTimeFormatOptions) {
if (
!options ||
isDateTimeStyleSupported ||
!hasOptions(options, ['dateStyle', 'timeStyle'])
) {
return options;
}
const { dateStyle, timeStyle, ...rest } = options;
let fallbackOptions = rest;
if (dateStyle) {
fallbackOptions = { ...fallbackOptions, ...DATE_STYLES[dateStyle] };
}
if (timeStyle) {
fallbackOptions = { ...fallbackOptions, ...TIME_STYLES[timeStyle] };
}
return fallbackOptions;
}