Skip to content

Commit

Permalink
Add per locale Intl memoizer (#504)
Browse files Browse the repository at this point in the history
* Add per locale Intl memoizer

* Switch from WeakMap to Map
  • Loading branch information
Demivan committed Oct 25, 2021
1 parent 5f7ce0f commit 67905cb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
9 changes: 3 additions & 6 deletions fluent-bundle/src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { FluentResource } from "./resource.js";
import { FluentValue, FluentNone, FluentFunction } from "./types.js";
import { Message, Term, Pattern } from "./ast.js";
import { NUMBER, DATETIME } from "./builtins.js";
import { getMemoizerForLocale, IntlCache } from "./memoizer.js";

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

Expand All @@ -22,12 +23,7 @@ export class FluentBundle {
public _functions: Record<string, FluentFunction>;
public _useIsolating: boolean;
public _transform: TextTransform;
public _intls = new WeakMap<
| typeof Intl.NumberFormat
| typeof Intl.DateTimeFormat
| typeof Intl.PluralRules,
Record<string, Intl.NumberFormat | Intl.DateTimeFormat | Intl.PluralRules>
>();
public _intls: IntlCache;

/**
* Create an instance of `FluentBundle`.
Expand Down Expand Up @@ -78,6 +74,7 @@ export class FluentBundle {
};
this._useIsolating = useIsolating;
this._transform = transform;
this._intls = getMemoizerForLocale(locales);
}

/**
Expand Down
19 changes: 19 additions & 0 deletions fluent-bundle/src/memoizer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export type IntlCache = Map<
| typeof Intl.NumberFormat
| typeof Intl.DateTimeFormat
| typeof Intl.PluralRules,
Record<string, Intl.NumberFormat | Intl.DateTimeFormat | Intl.PluralRules>
>;

const cache = new Map<string, IntlCache>();

export function getMemoizerForLocale(locales: string | string[]): IntlCache {
const stringLocale = Array.isArray(locales) ? locales.join(" ") : locales;
let memoizer = cache.get(stringLocale);
if (memoizer === undefined) {
memoizer = new Map();
cache.set(stringLocale, memoizer);
}

return memoizer;
}
30 changes: 30 additions & 0 deletions fluent-bundle/test/memoizer_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

import assert from 'assert';
import {getMemoizerForLocale} from '../esm/memoizer.js';

suite('Memoizer', function() {
test('returns same instance for same locale', function() {
const memoizer1 = getMemoizerForLocale('en')
const memoizer2 = getMemoizerForLocale('en')
assert.strictEqual(memoizer1, memoizer2)
});

test('returns different instance for different locale', function() {
const memoizer1 = getMemoizerForLocale('en')
const memoizer2 = getMemoizerForLocale('uk')
assert.notStrictEqual(memoizer1, memoizer2)
});

test('works with array of locales', function() {
const memoizer1 = getMemoizerForLocale(['en'])
const memoizer2 = getMemoizerForLocale(['en'])
assert.strictEqual(memoizer1, memoizer2)
});

test('works with string and array of locales', function() {
const memoizer1 = getMemoizerForLocale(['en'])
const memoizer2 = getMemoizerForLocale('en')
assert.strictEqual(memoizer1, memoizer2)
})
})

0 comments on commit 67905cb

Please sign in to comment.