-
Notifications
You must be signed in to change notification settings - Fork 1
/
intl.ts
135 lines (121 loc) · 3.29 KB
/
intl.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
import { locale as defaultLocale } from "./locale.ts";
export function plural(
amount: number,
singular: string,
options: {
locale?: string;
plural?: string;
hideCount?: boolean;
} = {},
) {
const {
locale = defaultLocale,
plural = singular + "s",
hideCount,
} = options;
const pluralRules = new Intl.PluralRules(locale);
const pluralForm = pluralRules.select(amount);
return [!hideCount && amount, pluralForm === "one" ? singular : plural]
.filter(Boolean)
.join(" ");
}
export function list(
items: string[],
options: Intl.ListFormatOptions & {
locale?: string;
} = {},
) {
const { locale = defaultLocale, ...formatOptions } = options;
const listFormatter = new Intl.ListFormat(locale, formatOptions);
return listFormatter.format(items);
}
export function collate<T>(
items: T[] | ReadonlyArray<T>,
options: Intl.CollatorOptions & {
locale?: string;
get?: (item: T) => string;
} = {},
): T[] {
const {
locale = defaultLocale,
get = (item) => "" + item,
...collatorOptions
} = options;
const collator = new Intl.Collator(locale, collatorOptions);
return [...items].sort((a, b) => collator.compare(get(a), get(b)));
}
export function relativeTime(
date: Date,
options: {
style?: Intl.RelativeTimeFormatOptions["style"];
locale?: string;
} = {},
): string {
const { style: length = "long", locale = defaultLocale } = options;
const rtf = new Intl.RelativeTimeFormat(locale, {
style: length,
numeric: "auto",
});
let delta = (date.getTime() - Date.now()) / 1000;
for (let i = 0; i <= units.length; i++) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const unit = units[i]!;
delta = Math.round(delta);
if (Math.abs(delta) < unit.amount) {
return rtf.format(delta, unit.name);
}
delta /= unit.amount;
}
return "";
}
export function relativeTimeFormat(
value: number,
unit: Intl.RelativeTimeFormatUnit,
options: Intl.RelativeTimeFormatOptions & {
locale?: string;
} = {},
): string {
const { locale = defaultLocale, ...formatOptions } = options;
return new Intl.RelativeTimeFormat(locale, formatOptions).format(value, unit);
}
export function currency(
value: number,
options: Omit<Intl.NumberFormatOptions, "currency"> & {
currency?: string;
locale?: string;
} = {},
): string {
const { locale, ...formatOptions } = options;
return new Intl.NumberFormat(locale, {
style: "currency",
...formatOptions,
}).format(value);
}
export function date(
date: Date,
options: Intl.DateTimeFormatOptions & {
locale?: string;
} = {},
): string {
const { locale, ...formatOptions } = options;
return new Intl.DateTimeFormat(locale, formatOptions).format(date);
}
export function range(
start: number,
end: number,
options: Intl.NumberFormatOptions & {
locale?: string;
} = {},
): string {
const { locale, ...formatOptions } = options;
return new Intl.NumberFormat(locale, formatOptions).formatRange(start, end);
}
const units = [
{ amount: 60, name: "second" },
{ amount: 60, name: "minute" },
{ amount: 24, name: "hour" },
{ amount: 7, name: "day" },
{ amount: Math.floor(52 / 12), name: "week" },
{ amount: 12, name: "month" },
{ amount: Number.POSITIVE_INFINITY, name: "year" },
] as const;