@vobs/i18n 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +23 -25
  2. package/package.json +4 -4
package/dist/index.js CHANGED
@@ -298,42 +298,40 @@ function formatSelect(name, body, locale, params, resolveKey) {
298
298
  }
299
299
  // Intl constructor cache: PluralRules/NumberFormat/DateTimeFormat/RelativeTimeFormat are expensive to construct;
300
300
  // instances for the same locale are reusable (allowed by the Intl spec, results unchanged). Module-level caches are
301
- // shared across instances and grow with the number of locales (bounded).
301
+ // shared across instances and use an LRU limit so request-specific options cannot grow the process indefinitely.
302
+ const FORMATTER_CACHE_LIMIT = 100;
302
303
  const pluralRulesCache = new Map();
303
304
  const numberFormatCache = new Map();
304
305
  const dateTimeFormatCache = new Map();
305
306
  const relativeTimeFormatCache = new Map();
306
- function cachedPluralRules(locale) {
307
- let rules = pluralRulesCache.get(locale);
308
- if (rules === undefined) {
309
- rules = new Intl.PluralRules(locale);
310
- pluralRulesCache.set(locale, rules);
307
+ function readCachedFormatter(cache, key, create) {
308
+ const existing = cache.get(key);
309
+ if (existing !== undefined) {
310
+ cache.delete(key);
311
+ cache.set(key, existing);
312
+ return existing;
313
+ }
314
+ const formatter = create();
315
+ cache.set(key, formatter);
316
+ while (cache.size > FORMATTER_CACHE_LIMIT) {
317
+ const oldest = cache.keys().next().value;
318
+ if (oldest === undefined)
319
+ break;
320
+ cache.delete(oldest);
311
321
  }
312
- return rules;
322
+ return formatter;
323
+ }
324
+ function cachedPluralRules(locale) {
325
+ return readCachedFormatter(pluralRulesCache, locale, () => new Intl.PluralRules(locale));
313
326
  }
314
327
  function cachedNumberFormat(locale, options) {
315
328
  const key = options === undefined ? locale : `${locale}\u0000${JSON.stringify(options)}`;
316
- let format = numberFormatCache.get(key);
317
- if (format === undefined) {
318
- format = new Intl.NumberFormat(locale, options);
319
- numberFormatCache.set(key, format);
320
- }
321
- return format;
329
+ return readCachedFormatter(numberFormatCache, key, () => new Intl.NumberFormat(locale, options));
322
330
  }
323
331
  function cachedDateTimeFormat(locale, options) {
324
332
  const key = options === undefined ? locale : `${locale}\u0000${JSON.stringify(options)}`;
325
- let format = dateTimeFormatCache.get(key);
326
- if (format === undefined) {
327
- format = new Intl.DateTimeFormat(locale, options);
328
- dateTimeFormatCache.set(key, format);
329
- }
330
- return format;
333
+ return readCachedFormatter(dateTimeFormatCache, key, () => new Intl.DateTimeFormat(locale, options));
331
334
  }
332
335
  function cachedRelativeTimeFormat(locale) {
333
- let format = relativeTimeFormatCache.get(locale);
334
- if (format === undefined) {
335
- format = new Intl.RelativeTimeFormat(locale, { numeric: 'auto' });
336
- relativeTimeFormatCache.set(locale, format);
337
- }
338
- return format;
336
+ return readCachedFormatter(relativeTimeFormatCache, locale, () => new Intl.RelativeTimeFormat(locale, { numeric: 'auto' }));
339
337
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vobs/i18n",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "Instance-based translation and Intl formatting primitives for vobs.",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -18,8 +18,8 @@
18
18
  },
19
19
  "homepage": "https://github.com/vobsjs/vobs#readme",
20
20
  "dependencies": {
21
- "@vobs/reactivity": "0.1.0",
22
- "@vobs/runtime-core": "0.1.0"
21
+ "@vobs/reactivity": "0.3.0",
22
+ "@vobs/runtime-core": "0.3.0"
23
23
  },
24
24
  "files": [
25
25
  "dist"
@@ -36,6 +36,6 @@
36
36
  "main": "./dist/index.js",
37
37
  "sideEffects": false,
38
38
  "engines": {
39
- "node": ">=20.19.0"
39
+ "node": ">=22.12.0"
40
40
  }
41
41
  }