Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
Memoize NumberFormat as well
Browse files Browse the repository at this point in the history
  • Loading branch information
Neele Barthel committed Feb 19, 2020
1 parent 6d01cf0 commit 4baa588
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 66 deletions.
2 changes: 1 addition & 1 deletion packages/react-i18n/src/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
formatDate,
isLessThanOneHourAgo,
isLessThanOneMinuteAgo,
isLessThanOneWeekAgo,
Expand Down Expand Up @@ -41,7 +42,6 @@ import {
memoizedNumberFormatter,
memoizedPluralRules,
} from './utilities';
import {formatDate} from './temporary-format-date';

export interface NumberFormatOptions extends Intl.NumberFormatOptions {
as?: 'number' | 'currency' | 'percent';
Expand Down
57 changes: 0 additions & 57 deletions packages/react-i18n/src/temporary-format-date.ts

This file was deleted.

21 changes: 13 additions & 8 deletions packages/react-i18n/src/utilities/translate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ const MISSING_TRANSLATION = Symbol('Missing translation');
const PLURALIZATION_KEY_NAME = 'count';
const SEPARATOR = '.';

const numberFormats = new Map();
export const memoizedNumberFormatter = function(locale, options = {}) {
const key = numberFormatCacheKey(locale, options);
if (numberFormats.has(key)) {
return numberFormats.get(key);
}
const i = new Intl.NumberFormat(locale, options);
numberFormats.set(key, i);
return i;
};

export const PSEUDOTRANSLATE_OPTIONS: PseudotranslateOptions = {
startDelimiter: '{',
endDelimiter: '}',
Expand All @@ -30,19 +41,13 @@ export interface TranslateOptions<Replacements = {}> {
pseudotranslate?: boolean | string;
}

function numberFormatter(
function numberFormatCacheKey(
locale: string,
options: Intl.NumberFormatOptions = {},
) {
return new Intl.NumberFormat(locale, options);
return `${locale}${JSON.stringify(options)}`;
}

export const memoizedNumberFormatter = memoizeFn(
numberFormatter,
(locale: string, options: Intl.NumberFormatOptions = {}) =>
`${locale}${JSON.stringify(options)}`,
);

function pluralRules(locale: string, options: Intl.PluralRulesOptions = {}) {
return new Intl.PluralRules(locale, options);
}
Expand Down

0 comments on commit 4baa588

Please sign in to comment.