Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
42 additions
and
36 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
declare module 'isoformat' { | ||
export function format(value: any, fallback: string): string; | ||
export function format(value: any, fallback: any): any; | ||
} |