-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.ts
62 lines (58 loc) · 1.99 KB
/
format.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
declare namespace Intl {
class ListFormat {
constructor(locales?: string | string[], options?: Intl.ListFormatOptions);
public format: (items: string[]) => string;
}
class ListFormatOptions {
public localeMatcher?: "lookup" | "best fit";
public type?: "conjunction" | "disjunction" | "unit";
public style?: "long" | "short" | "narrow";
}
}
const wrap =
(formatter) =>
(...args) =>
formatter.format(...args);
export const pc = wrap(
new Intl.NumberFormat("en-US", { style: "percent", notation: "compact", compactDisplay: "short" }),
);
export const pc0 = wrap(new Intl.NumberFormat("en-US", { style: "percent", maximumFractionDigits: 0 }));
export const pc1 = wrap(
new Intl.NumberFormat("en-US", {
style: "percent",
maximumFractionDigits: 1,
minimumFractionDigits: 1,
}),
);
export const num = wrap(
new Intl.NumberFormat("en-US", {
style: "decimal",
notation: "compact",
compactDisplay: "short",
}),
);
export const num0 = wrap(
new Intl.NumberFormat("en-US", { style: "decimal", grouping: "always", maximumFractionDigits: 0 }),
);
export const num2 = wrap(
new Intl.NumberFormat("en-US", {
style: "decimal",
grouping: "always",
maximumFractionDigits: 2,
minimumFractionDigits: 2,
}),
);
export const dmy = wrap(new Intl.DateTimeFormat("en-GB", { day: "numeric", month: "short", year: "numeric" }));
export const mdy = wrap(new Intl.DateTimeFormat("en-US", { day: "numeric", month: "short", year: "numeric" }));
export const dm = wrap(new Intl.DateTimeFormat("en-GB", { day: "numeric", month: "short" }));
export const md = wrap(new Intl.DateTimeFormat("en-US", { day: "numeric", month: "short" }));
export const wdmy = wrap(
new Intl.DateTimeFormat("en-GB", {
weekday: "short",
day: "numeric",
month: "short",
year: "numeric",
}),
);
export const and = wrap(new Intl.ListFormat("en-US", { style: "long", type: "conjunction" }));
export const or = wrap(new Intl.ListFormat("en-US", { style: "long", type: "disjunction" }));