@northguild/gmt
Version:
Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.
40 lines • 2.43 kB
TypeScript
/** The `Intl` formatter classes this module builds. */
type FormatterKind = "DateTimeFormat" | "NumberFormat" | "ListFormat" | "RelativeTimeFormat";
type FormatterLocale = string | string[] | undefined;
/**
* A shared, read-only formatter for these arguments; built on the first call with them.
*
* A formatter's output is fixed by its constructor arguments and the host's locale data. A
* `DateTimeFormat` also reads the default locale and default time zone, so it is cached only
* with a `locale` string (or array of strings) and a `timeZone` string; any other argument
* set is constructed fresh, exactly as `new Intl[kind](locale, options)` would. A construction
* failure throws and is never cached. A formatter is kept only while `Intl[kind]` is the
* constructor that built it.
*
* @param kind which `Intl` formatter class to build
* @param locale BCP 47 locale or locales, or `undefined` for the runtime default
* @param options the formatter's options, built by GMT code
* @returns the formatter
*
* @example cachedFormatter("DateTimeFormat", "en-GB", { timeZone: "UTC" }).format(0) // "01/01/1970"
* @example cachedFormatter("ListFormat", "en", { type: "conjunction" }).format(["a", "b"]) // "a and b"
* @example cachedFormatter("DateTimeFormat", "not a locale", { timeZone: "UTC" }) // throws RangeError
*/
export declare function cachedFormatter<K extends FormatterKind>(kind: K, locale: FormatterLocale, options: object): InstanceType<(typeof Intl)[K]>;
/**
* Construct once to raise the error `Intl` raises for these arguments; a repeat is a no-op.
*
* Whether construction throws does not depend on the default zone, so the arguments need no
* `timeZone`. Only a success is remembered; an invalid call throws every time.
*
* @param kind which `Intl` formatter class to build
* @param locale BCP 47 locale or locales, or `undefined` for the runtime default
* @param options the formatter's options, built by GMT code
*
* @example checkFormatterArguments("DateTimeFormat", "en-GB", { dateStyle: "long" }) // returns
* @example checkFormatterArguments("DateTimeFormat", "en-GB", { dateStyle: "huge" }) // throws RangeError
*/
export declare function checkFormatterArguments(kind: FormatterKind, locale: FormatterLocale, options: object): void;
/** Test seam: forget every cached formatter and every checked argument set. */
export declare function clearFormatterCache(): void;
export {};