Skip to content

Commit 67905cb

Browse files
authored
Add per locale Intl memoizer (#504)
* Add per locale Intl memoizer * Switch from WeakMap to Map
1 parent 5f7ce0f commit 67905cb

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

fluent-bundle/src/bundle.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { FluentResource } from "./resource.js";
44
import { FluentValue, FluentNone, FluentFunction } from "./types.js";
55
import { Message, Term, Pattern } from "./ast.js";
66
import { NUMBER, DATETIME } from "./builtins.js";
7+
import { getMemoizerForLocale, IntlCache } from "./memoizer.js";
78

89
export type TextTransform = (text: string) => string;
910

@@ -22,12 +23,7 @@ export class FluentBundle {
2223
public _functions: Record<string, FluentFunction>;
2324
public _useIsolating: boolean;
2425
public _transform: TextTransform;
25-
public _intls = new WeakMap<
26-
| typeof Intl.NumberFormat
27-
| typeof Intl.DateTimeFormat
28-
| typeof Intl.PluralRules,
29-
Record<string, Intl.NumberFormat | Intl.DateTimeFormat | Intl.PluralRules>
30-
>();
26+
public _intls: IntlCache;
3127

3228
/**
3329
* Create an instance of `FluentBundle`.
@@ -78,6 +74,7 @@ export class FluentBundle {
7874
};
7975
this._useIsolating = useIsolating;
8076
this._transform = transform;
77+
this._intls = getMemoizerForLocale(locales);
8178
}
8279

8380
/**

fluent-bundle/src/memoizer.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export type IntlCache = Map<
2+
| typeof Intl.NumberFormat
3+
| typeof Intl.DateTimeFormat
4+
| typeof Intl.PluralRules,
5+
Record<string, Intl.NumberFormat | Intl.DateTimeFormat | Intl.PluralRules>
6+
>;
7+
8+
const cache = new Map<string, IntlCache>();
9+
10+
export function getMemoizerForLocale(locales: string | string[]): IntlCache {
11+
const stringLocale = Array.isArray(locales) ? locales.join(" ") : locales;
12+
let memoizer = cache.get(stringLocale);
13+
if (memoizer === undefined) {
14+
memoizer = new Map();
15+
cache.set(stringLocale, memoizer);
16+
}
17+
18+
return memoizer;
19+
}

fluent-bundle/test/memoizer_test.js

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
import assert from 'assert';
4+
import {getMemoizerForLocale} from '../esm/memoizer.js';
5+
6+
suite('Memoizer', function() {
7+
test('returns same instance for same locale', function() {
8+
const memoizer1 = getMemoizerForLocale('en')
9+
const memoizer2 = getMemoizerForLocale('en')
10+
assert.strictEqual(memoizer1, memoizer2)
11+
});
12+
13+
test('returns different instance for different locale', function() {
14+
const memoizer1 = getMemoizerForLocale('en')
15+
const memoizer2 = getMemoizerForLocale('uk')
16+
assert.notStrictEqual(memoizer1, memoizer2)
17+
});
18+
19+
test('works with array of locales', function() {
20+
const memoizer1 = getMemoizerForLocale(['en'])
21+
const memoizer2 = getMemoizerForLocale(['en'])
22+
assert.strictEqual(memoizer1, memoizer2)
23+
});
24+
25+
test('works with string and array of locales', function() {
26+
const memoizer1 = getMemoizerForLocale(['en'])
27+
const memoizer2 = getMemoizerForLocale('en')
28+
assert.strictEqual(memoizer1, memoizer2)
29+
})
30+
})

0 commit comments

Comments
 (0)