forked from observablehq/plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.ts
More file actions
50 lines (43 loc) · 2.11 KB
/
format.ts
File metadata and controls
50 lines (43 loc) · 2.11 KB
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
/* eslint-disable @typescript-eslint/no-explicit-any */
import {format as isoFormat} from "isoformat";
import {string} from "./options.js";
import {memoize1} from "./memoize.js";
const numberFormat = memoize1<Intl.NumberFormat>(
(locale: string | string[] | undefined) => new Intl.NumberFormat(locale)
);
const monthFormat = memoize1<Intl.DateTimeFormat>(
(locale: string | string[] | undefined, month: "numeric" | "2-digit" | "long" | "short" | "narrow" | undefined) =>
new Intl.DateTimeFormat(locale, {timeZone: "UTC", month})
);
const weekdayFormat = memoize1<Intl.DateTimeFormat>(
(locale: string | string[] | undefined, weekday: "long" | "short" | "narrow" | undefined) =>
new Intl.DateTimeFormat(locale, {timeZone: "UTC", weekday})
);
export function formatNumber(locale = "en-US"): (value: any) => string | undefined {
const format = numberFormat(locale);
return (i: any) => (i != null && !isNaN(i) ? format.format(i) : undefined);
}
export function formatMonth(
locale = "en-US",
month: "numeric" | "2-digit" | "long" | "short" | "narrow" | undefined = "short"
) {
const format = monthFormat(locale, month);
return (i: Date | number | null | undefined) =>
i != null && !isNaN((i = +new Date(Date.UTC(2000, +i)))) ? format.format(i) : undefined;
}
export function formatWeekday(locale = "en-US", weekday: "long" | "short" | "narrow" | undefined = "short") {
const format = weekdayFormat(locale, weekday);
return (i: Date | number | null | undefined) =>
i != null && !isNaN((i = +new Date(Date.UTC(2001, 0, +i)))) ? format.format(i) : undefined;
}
export function formatIsoDate(date: Date): string {
return isoFormat(date, "Invalid Date");
}
export function formatAuto(locale = "en-US"): (value: any) => string | number | undefined {
const number = formatNumber(locale);
return (v: any) => (v instanceof Date ? formatIsoDate : typeof v === "number" ? number : string)(v);
}
// TODO When Plot supports a top-level locale option, this should be removed
// because it lacks context to know which locale to use; formatAuto should be
// used instead whenever possible.
export const formatDefault = formatAuto();