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.

156 lines (155 loc) • 6.82 kB
// src/internal/formatterCache.ts /** At most this many formatters (or checked argument sets) per kind; past it the kind's map clears. */ const MAX_ENTRIES_PER_KIND = 256; const formatters = /* @__PURE__ */ new Map(); const checked = /* @__PURE__ */ new Map(); /** The `Intl[kind]` constructor each kind's entries were built with. */ const builtWith = /* @__PURE__ */ new Map(); /** * Forget a kind's formatters and checked argument sets when `Intl[kind]` is no longer the * constructor they were built with (a polyfill loaded later, a test stub), so every answer comes * from the constructor installed now. */ function forgetIfReplaced(kind) { const current = Intl[kind]; if (builtWith.get(kind) !== current) { formatters.delete(kind); checked.delete(kind); builtWith.set(kind, current); } } function construct(kind, locale, options) { switch (kind) { case "DateTimeFormat": return new Intl.DateTimeFormat(locale, options); case "NumberFormat": return new Intl.NumberFormat(locale, options); case "ListFormat": return new Intl.ListFormat(locale, options); case "RelativeTimeFormat": return new Intl.RelativeTimeFormat(locale, options); } } /** * Whether a canonical locale list can be part of a cache key: a string, or a non-empty array. * `undefined` and `[]` answer from the process default locale, which can change. */ function isCacheableLocale(locale) { return typeof locale === "string" || Array.isArray(locale) && locale.length > 0; } /** Whether `Intl` can read this option value without running caller code. */ function isPlainOptionValue(value) { return typeof value === "string" || typeof value === "boolean" || typeof value === "number" && Number.isFinite(value); } /** * The own enumerable options as `[name, value]` pairs, read through property descriptors so a * getter is never run, or `null` when one is an accessor or holds a value `Intl` would coerce * by running caller code (anything but a string, a finite number or a boolean). An `undefined` * value is skipped, as `Intl` reads it as absent. */ function plainOptionEntries(options) { const entries = []; for (const name of Object.keys(options)) { const descriptor = Object.getOwnPropertyDescriptor(options, name); if (descriptor === void 0 || !("value" in descriptor)) return null; const value = descriptor.value; if (value === void 0) continue; if (!isPlainOptionValue(value)) return null; entries.push([name, value]); } return entries; } /** Whether these entries carry a `timeZone` string. */ function hasZoneString(entries) { return entries.some(([name, value]) => name === "timeZone" && typeof value === "string"); } /** * The cache key for these arguments, or `null` when they must be constructed fresh. The * locale is already canonical (see `canonicalLocale`). * * @param zoneRequired whether the options must carry a `timeZone` string: a `DateTimeFormat` * without one answers from the process default zone, which can change. */ function cacheKey(kind, locale, options, zoneRequired) { if (!isCacheableLocale(locale)) return null; const entries = plainOptionEntries(options); if (entries === null || zoneRequired && !hasZoneString(entries)) return null; return kind + JSON.stringify([locale, entries]); } /** * The locale list as `Intl` canonicalizes it (ECMA-402 CanonicalizeLocaleList): an array is read * once, here, through `Intl.getCanonicalLocales`, so the key and the constructor see the same * strings even when the array is a Proxy that answers each read differently, and a bad list * throws before the options are read, as in the constructors. */ function canonicalLocale(locale) { return Array.isArray(locale) ? Intl.getCanonicalLocales(locale) : locale; } /** * 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 */ function cachedFormatter(kind, locale, options) { locale = canonicalLocale(locale); const key = cacheKey(kind, locale, options, kind === "DateTimeFormat"); if (key === null) return construct(kind, locale, options); forgetIfReplaced(kind); let byKey = formatters.get(kind); const hit = byKey?.get(key); if (hit !== void 0) return hit; const built = construct(kind, locale, options); if (byKey === void 0) { byKey = /* @__PURE__ */ new Map(); formatters.set(kind, byKey); } else if (byKey.size >= MAX_ENTRIES_PER_KIND) byKey.clear(); byKey.set(key, built); return built; } /** * 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 */ function checkFormatterArguments(kind, locale, options) { locale = canonicalLocale(locale); const key = cacheKey(kind, locale, options, false); if (key === null) { construct(kind, locale, options); return; } forgetIfReplaced(kind); let seen = checked.get(kind); if (seen?.has(key)) return; construct(kind, locale, options); if (seen === void 0) { seen = /* @__PURE__ */ new Set(); checked.set(kind, seen); } else if (seen.size >= MAX_ENTRIES_PER_KIND) seen.clear(); seen.add(key); } /** Test seam: forget every cached formatter and every checked argument set. */ function clearFormatterCache() { formatters.clear(); checked.clear(); builtWith.clear(); } export { cachedFormatter, checkFormatterArguments, clearFormatterCache };