-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.ts
214 lines (198 loc) · 6.7 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
/**
* Copyright 2024, 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 { Locale } from '../../types/index.js';
import { formatNumber, formatNumberToParts } from '../number-format/index.js';
import {
getRelativeTimeFormat,
isRelativeTimeFormatSupported,
} from './intl.js';
export { isRelativeTimeFormatSupported };
/**
* Formats a relative time with support for various [styles](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/RelativeTimeFormat#style).
*
* @example
* import { formatRelativeTime } from '@sumup-oss/intl';
*
* formatRelativeTime(1, 'day', 'de-DE'); // 'in 1 Tag'
* formatRelativeTime(7, 'years', ['pt-BR', 'pt']); // 'em 7 anos'
* formatRelativeTime(-5, 'months', 'en-GB', {
* style: 'narrow',
* }); // '5 mo ago'
*
* @remarks
* In runtimes that don't support the `Intl.RelativeTimeFormat` API,
* the relative time is formatted using the `Intl.NumberFormat` API instead.
*
* @category Date & Time
*/
export const formatRelativeTime = formatRelativeTimeFactory();
function formatRelativeTimeFactory(): (
value: number,
unit: Intl.RelativeTimeFormatUnit,
locales?: Locale | Locale[],
options?: Intl.RelativeTimeFormatOptions,
) => string {
if (!isRelativeTimeFormatSupported) {
return (value, unit, locales, options) => {
const numberFormat = convertToNumberFormat(value, unit, options);
return formatNumber(numberFormat.value, locales, numberFormat.options);
};
}
return (value, unit, locales, options) => {
const relativeTimeFormat = getRelativeTimeFormat(locales, options);
return relativeTimeFormat.format(value, unit);
};
}
/**
* Formats a relative time to parts with support for various [styles](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/RelativeTimeFormat#style).
*
* @example
* import { formatRelativeTimeToParts } from '@sumup-oss/intl';
*
* formatRelativeTimeToParts(1, 'day', 'de-DE');
* // [
* // { "type": "literal", "value": "in " },
* // { "type": "integer", "unit": "day", "value": "1" },
* // { "type": "literal", "value": " Tag" }
* // ]
* formatRelativeTimeToParts(7, 'years', ['pt-BR', 'pt']);
* // [
* // { "type": "literal", "value": "em " },
* // { "type": "integer", "unit": "year", "value": "7" },
* // { "type": "literal", "value": " anos" }
* // ]
* formatRelativeTimeToParts(-5, 'months', 'en-GB', {
* style: 'narrow',
* });
* // [
* // { "type": "integer", "unit": "month", "value": "5" },
* // { "type": "literal", "value": " mo ago" }
* // ]
*
* @remarks
* In runtimes that don't support the `Intl.RelativeTimeFormat` API,
* the relative time is formatted using the `Intl.NumberFormat` API instead.
*
* @category Date & Time
*/
export const formatRelativeTimeToParts = formatRelativeTimeToPartsFactory();
function formatRelativeTimeToPartsFactory(): (
value: number,
unit: Intl.RelativeTimeFormatUnit,
locales?: Locale | Locale[],
options?: Intl.RelativeTimeFormatOptions,
) => Intl.RelativeTimeFormatPart[] {
if (!isRelativeTimeFormatSupported) {
return (value, unit, locales, options) => {
// In runtimes that don't support formatting to parts, the relative time
// is formatted using the `Intl.NumberFormat` API instead.
const numberFormat = convertToNumberFormat(value, unit, options);
const numberFormatParts = formatNumberToParts(
numberFormat.value,
locales,
numberFormat.options,
);
// Smooth over the subtle differences between NumberFormatPart and RelativeTimeFormatPart
return numberFormatParts.map((part) => {
if (part.type === 'integer') {
return { ...part, unit: numberFormat.options.unit };
}
if (part.type === 'unit') {
return { ...part, type: 'literal' };
}
return part;
}) as Intl.RelativeTimeFormatPart[];
};
}
return (value, unit, locales, options) => {
const relativeTimeFormat = getRelativeTimeFormat(locales, options);
return relativeTimeFormat.formatToParts(value, unit);
};
}
/**
* Resolves the locale and collation options that are used to format a relative time.
*
* @example
* import { resolveRelativeTimeFormat } from '@sumup-oss/intl';
*
* resolveRelativeTimeFormat('de-DE');
* // {
* // "locale": "de-DE",
* // "numberingSystem": "latn",
* // "numeric": "always",
* // "style": "long",
* // }
* resolveRelativeTimeFormat(['pt-BR', 'pt']);
* // {
* // "locale": "pt-BR",
* // "numberingSystem": "latn",
* // "numeric": "always",
* // "style": "long",
* // }
* resolveRelativeTimeFormat('en-GB', {
* style: 'narrow',
* });
* // {
* // "locale": "en-GB",
* // "numberingSystem": "latn",
* // "numeric": "always",
* // "style": "narrow",
* // }
*
* @remarks
* In runtimes that don't support the `Intl.RelativeTimeFormat.resolvedOptions` API,
* `null` is returned.
*
* @category Date & Time
*/
export const resolveRelativeTimeFormat = resolveRelativeTimeFormatFactory();
function resolveRelativeTimeFormatFactory(): (
locales?: Locale | Locale[],
options?: Intl.RelativeTimeFormatOptions,
) => null | Intl.ResolvedRelativeTimeFormatOptions {
if (!isRelativeTimeFormatSupported) {
return () => null;
}
return (locales, options) => {
const relativeTimeFormat = getRelativeTimeFormat(locales, options);
return relativeTimeFormat.resolvedOptions();
};
}
function convertToNumberFormat(
value: number,
unit: Intl.RelativeTimeFormatUnit,
options: Intl.RelativeTimeFormatOptions | undefined,
): { value: number; options: Intl.NumberFormatOptions } {
const style = 'unit';
const unitDisplay = options?.style || 'long';
// Intl.NumberFormat doesn't support the 'quarter' unit
if (unit.startsWith('quarter')) {
return { value: value * 3, options: { unit: 'month', style, unitDisplay } };
}
const numberFormatUnits = [
'year',
'month',
'week',
'day',
'hour',
'minute',
'second',
];
// Intl.NumberFormat only supports singular unit names
const singularUnit = numberFormatUnits.find((supportedUnit) =>
unit.startsWith(supportedUnit),
);
return { value, options: { unit: singularUnit, style, unitDisplay } };
}