Skip to content

Commit

Permalink
ts format
Browse files Browse the repository at this point in the history
  • Loading branch information
Fil committed May 26, 2022
1 parent d94a6ae commit b87e8e3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 36 deletions.
36 changes: 0 additions & 36 deletions src/format.js

This file was deleted.

38 changes: 38 additions & 0 deletions src/format.ts
@@ -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();
4 changes: 4 additions & 0 deletions src/isoformat.d.ts
@@ -0,0 +1,4 @@
declare module 'isoformat' {
export function format(value: any, fallback: string): string;
export function format(value: any, fallback: any): any;
}

0 comments on commit b87e8e3

Please sign in to comment.