UNPKG

@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.

62 lines • 3.29 kB
/** * A locale argument: one BCP 47 tag, or a preference list of tags (ECMA-402 `locales`). */ export type LocalesArgument = string | string[]; /** * The canonical tags of a `locale` argument, read as the types say: an omitted locale, a string, * or an array whose every entry is a primitive string. `Intl.getCanonicalLocales` would convert a * boxed `String` or an object with a `toString` into a tag, and read a hole as `"undefined"`, so * each entry is checked first. The array is read once, so the check and the canonical list see * the same strings even for a Proxy. * * @param locales raw locale argument from caller input * @returns the canonical tags (`[]` for an omitted locale or an empty list) * @throws TypeError for a value that is not `undefined`, a string or an array of strings * * @example canonicalLocaleTags("EN-gb") // ["en-GB"] * @example canonicalLocaleTags(["de-DE", "en"]) // ["de-DE", "en"] * @example canonicalLocaleTags([new String("de-DE")]) // throws TypeError */ export declare function canonicalLocaleTags(locales: unknown): string[]; /** * Resolve a `locale` argument to the single BCP 47 tag a locale-aware function reads. * * - Follows ECMA-402 CanonicalizeLocaleList: a string is a one-tag list, every tag is * canonicalized (`"EN-gb"` → `"en-GB"`) and duplicates are dropped. * - The first tag with available locale data wins (`Intl.DateTimeFormat.supportedLocalesOf`), as * ECMA-402's locale negotiation picks; a list with no available tag keeps its first tag, exactly * as that tag passed alone would be read. * - `undefined` and an empty list resolve to the default locale. * - A malformed tag anywhere in the list, a non-string element, or any other type is invalid and * resolves to `null`, which callers map to their sentinel (ECMA-402 throws a RangeError/TypeError). * * @param locales raw locale argument from caller input * @returns the resolved BCP 47 tag, or `null` when the argument is invalid */ export declare function resolveLocale(locales: unknown): string | null; /** * Resolve the `locale` argument of a function whose locale is required, to the single BCP 47 tag * it reads, or `null` (the caller's sentinel). * * - An omitted locale (`undefined`) and an empty list both name no locale, so both are `null`. * ECMA-402 would resolve an empty list to the host default locale; a required locale exists to * keep the result independent of the host, so GMT treats `[]` as omitted (house rule). * - Anything else resolves as `resolveLocale` does. * * @param locales raw locale argument from caller input * @returns the resolved BCP 47 tag, or `null` when the argument is omitted, empty or invalid * * @example resolveRequiredLocale("EN-gb") // "en-GB" * @example resolveRequiredLocale(undefined) // null * @example resolveRequiredLocale([]) // null */ export declare function resolveRequiredLocale(locales: unknown): string | null; /** * The host's default time zone identifier, read fresh on every call because it depends on a * process default and is never remembered. * * @returns the IANA name or offset the host reports, or `""` when it reports none or an invalid one * * @example systemTimeZone() // "Europe/Berlin" */ export declare function systemTimeZone(): string;