UNPKG

@intlayer/core

Version:

Includes core Intlayer functions like translation, dictionary, and utility functions shared across multiple packages.

1 lines 7.5 kB
{"version":3,"file":"intl.cjs","names":["Locales"],"sources":["../../../src/utils/intl.ts"],"sourcesContent":["// Cached Intl helper – drop‑in replacement for the global `Intl` object.\n// ‑‑‑\n// • Uses a `Proxy` to lazily wrap every *constructor* hanging off `Intl` (NumberFormat, DateTimeFormat, …).\n// • Each wrapped constructor keeps an in‑memory cache keyed by `[locales, options]` so that identical requests\n// reuse the same heavy instance instead of reparsing CLDR data every time.\n// • A polyfill warning for `Intl.DisplayNames` is emitted only once and only in dev.\n// • The public API is fully type‑safe and mirrors the native `Intl` surface exactly –\n// you can treat `CachedIntl` just like the built‑in `Intl`.\n//\n// Usage examples:\n// ---------------\n// import { CachedIntl } from \"./cached-intl\";\n//\n// const nf = CachedIntl.NumberFormat(\"en-US\", { style: \"currency\", currency: \"USD\" });\n// console.log(nf.format(1234));\n//\n// const dn = CachedIntl.DisplayNames([\"fr\"], { type: \"language\" });\n// console.log(dn.of(\"en\")); // → \"anglais\"\n//\n// You can also spin up an isolated instance with its own caches (handy in test suites):\n// const TestIntl = createCachedIntl();\n//\n// ---------------------------------------------------------------------\n\nimport { Locales, type LocalesValues } from '@intlayer/types';\n\n// Helper type that picks just the constructor members off `typeof Intl`.\n// The \"capital‑letter\" heuristic is 100 % accurate today and keeps the\n// mapping short‑lived, so we don't have to manually list every constructor.\ntype IntlConstructors = {\n [K in keyof typeof Intl as (typeof Intl)[K] extends new (\n ...args: any\n ) => any\n ? K\n : never]: (typeof Intl)[K];\n};\n\n// Type wrapper to replace locale arguments with LocalesValues\ntype ReplaceLocaleWithLocalesValues<T> = T extends new (\n locales: any,\n options?: infer Options\n) => infer Instance\n ? new (\n locales?: LocalesValues,\n options?: Options\n ) => Instance\n : T extends new (\n locales: any\n ) => infer Instance\n ? new (\n locales?: LocalesValues\n ) => Instance\n : T;\n\n// Wrapped Intl type with LocalesValues\ntype WrappedIntl = {\n [K in keyof typeof Intl]: K extends keyof IntlConstructors\n ? ReplaceLocaleWithLocalesValues<(typeof Intl)[K]>\n : (typeof Intl)[K];\n};\n\n// ... (Keep your Type Helper definitions here: IntlConstructors, ReplaceLocaleWithLocalesValues, WrappedIntl) ...\n\n/**\n * Optimized Cache Key Generator\n * 1. Fast path: If no options, just use the locale string.\n * 2. Normal path: JSON.stringify for deterministic object comparison.\n */\nconst getCacheKey = (\n locales: LocalesValues | undefined,\n options: unknown\n): string => {\n const localeKey = locales ? String(locales) : Locales.ENGLISH;\n\n if (!options) return localeKey;\n\n // JSON.stringify is the most robust way to handle nested options objects\n // without a heavy custom hashing function.\n return `${localeKey}|${JSON.stringify(options)}`;\n};\n\n/**\n * Generic wrapper for any `new Intl.*()` constructor.\n */\nconst createCachedConstructor = <T extends new (...args: any[]) => any>(\n Ctor: T\n) => {\n // The cache lives here, inside the closure of the wrapped constructor.\n const cache = new Map<string, InstanceType<T>>();\n const MAX_CACHE_SIZE = 50;\n\n function Wrapped(locales?: LocalesValues, options?: any) {\n // 1. Handle DisplayNames Polyfill warning (Keep your existing logic here)\n if (\n Ctor.name === 'DisplayNames' &&\n typeof (Intl as any)?.DisplayNames !== 'function'\n ) {\n // ... (Your existing polyfill warning logic) ...\n return locales as any;\n }\n\n // 2. Generate Key\n const key = getCacheKey(locales, options);\n\n // 3. Check Cache\n let instance = cache.get(key);\n if (instance) return instance;\n\n // 4. Create New Instance\n instance = new Ctor(locales as never, options as never);\n\n // 5. Smart Eviction (LRU-ish)\n // Map iterates in insertion order. Deleting the first key removes the \"oldest\".\n if (cache.size >= MAX_CACHE_SIZE) {\n const oldestKey = cache.keys().next().value;\n if (oldestKey) cache.delete(oldestKey);\n }\n\n cache.set(key, instance as InstanceType<T>);\n return instance as InstanceType<T>;\n }\n\n // Preserve prototype for `instanceof` checks\n (Wrapped as any).prototype = (Ctor as any).prototype;\n\n return Wrapped as unknown as ReplaceLocaleWithLocalesValues<T>;\n};\n\n/**\n * Factory that turns the global `Intl` into a cached clone.\n */\nexport const createCachedIntl = (): WrappedIntl => {\n // 🔥 CRITICAL OPTIMIZATION:\n // We must cache the *wrapped constructors* themselves.\n // Otherwise, the Proxy creates a new `Wrapped` function (and a new empty Map)\n // on every single property access.\n const constructorCache = new Map<string | symbol, any>();\n\n return new Proxy(Intl as IntlConstructors, {\n get: (target, prop, receiver) => {\n // 1. Fast return if we already wrapped this constructor\n if (constructorCache.has(prop)) {\n return constructorCache.get(prop);\n }\n\n const value = Reflect.get(target, prop, receiver);\n\n // 2. Wrap only Constructors (Heuristic: Function + starts with Uppercase)\n // This prevents wrapping static methods like `Intl.getCanonicalLocales`\n if (\n typeof value === 'function' &&\n typeof prop === 'string' &&\n /^[A-Z]/.test(prop)\n ) {\n const wrapped = createCachedConstructor(value);\n constructorCache.set(prop, wrapped);\n return wrapped;\n }\n\n // 3. Pass through everything else (static methods, constants)\n return value;\n },\n }) as unknown as WrappedIntl;\n};\n\nexport const CachedIntl = createCachedIntl();\n\n// new CachedIntl.DisplayNames(Locales.FRENCH, { type: 'language' });\n// new CachedIntl.DisplayNames('fr', { type: 'language' });\n// new CachedIntl.DateTimeFormat('fr', {\n// year: 'numeric',\n// month: 'long',\n// day: 'numeric',\n// });\n// new CachedIntl.NumberFormat('fr', {\n// style: 'currency',\n// currency: 'EUR',\n// });\n// new CachedIntl.Collator('fr', { sensitivity: 'base' });\n// new CachedIntl.PluralRules('fr');\n// new CachedIntl.RelativeTimeFormat('fr', { numeric: 'auto' });\n// new CachedIntl.ListFormat('fr', { type: 'conjunction' });\nexport { CachedIntl as Intl };\n"],"mappings":";;;;;;;;;AAoEA,MAAM,eACJ,SACA,YACW;CACX,MAAM,YAAY,UAAU,OAAO,QAAQ,GAAGA,wBAAQ;AAEtD,KAAI,CAAC,QAAS,QAAO;AAIrB,QAAO,GAAG,UAAU,GAAG,KAAK,UAAU,QAAQ;;;;;AAMhD,MAAM,2BACJ,SACG;CAEH,MAAM,wBAAQ,IAAI,KAA8B;CACh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