UNPKG

datezone

Version:

A lightweight and comprehensive date and timeZone utility library for JavaScript.

38 lines 1.2 kB
export const dtfCache = new Map(); export function getOptionsKey(opts) { // remove timeZone, sort remaining keys const { timeZone: _, ...rest } = opts; const ks = Object.keys(rest).sort(); return ks.map((k) => `${k}:${rest[k]}`).join("|"); } export function getCachedFormatter(timeZone, opts) { let zoneMap = dtfCache.get(timeZone); if (!zoneMap) { zoneMap = new Map(); dtfCache.set(timeZone, zoneMap); } const key = getOptionsKey(opts); let fmt = zoneMap.get(key); if (!fmt) { fmt = new Intl.DateTimeFormat("en-US", { timeZone, ...opts }); zoneMap.set(key, fmt); } return fmt; } // Cache for Intl.DateTimeFormat instances per locale and options const dtfCacheLocale = new Map(); export function getCachedFormatterLocale(locale, opts) { const key = JSON.stringify(opts); let localeMap = dtfCacheLocale.get(locale); if (!localeMap) { localeMap = new Map(); dtfCacheLocale.set(locale, localeMap); } let fmt = localeMap.get(key); if (!fmt) { fmt = new Intl.DateTimeFormat(locale, opts); localeMap.set(key, fmt); } return fmt; } //# sourceMappingURL=cache.js.map