-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add per locale Intl memoizer * Switch from WeakMap to Map
- Loading branch information
Showing
3 changed files
with
52 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |