From 4baa58838fd46860902463a754998ccf48f5072a Mon Sep 17 00:00:00 2001 From: Neele Barthel Date: Wed, 19 Feb 2020 13:27:32 -0500 Subject: [PATCH] Memoize NumberFormat as well --- packages/react-i18n/src/i18n.ts | 2 +- .../react-i18n/src/temporary-format-date.ts | 57 ------------------- .../react-i18n/src/utilities/translate.tsx | 21 ++++--- 3 files changed, 14 insertions(+), 66 deletions(-) delete mode 100644 packages/react-i18n/src/temporary-format-date.ts diff --git a/packages/react-i18n/src/i18n.ts b/packages/react-i18n/src/i18n.ts index b93bd359e1..b4940ace80 100644 --- a/packages/react-i18n/src/i18n.ts +++ b/packages/react-i18n/src/i18n.ts @@ -1,4 +1,5 @@ import { + formatDate, isLessThanOneHourAgo, isLessThanOneMinuteAgo, isLessThanOneWeekAgo, @@ -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'; diff --git a/packages/react-i18n/src/temporary-format-date.ts b/packages/react-i18n/src/temporary-format-date.ts deleted file mode 100644 index 5a365a5c64..0000000000 --- a/packages/react-i18n/src/temporary-format-date.ts +++ /dev/null @@ -1,57 +0,0 @@ -const intl = new Map(); -const memoizedGetDateTimeFormat = function(locale, options) { - const key = dateTimeFormatCacheKey(locale, options); - if (intl.has(key)) { - return intl.get(key); - } - const i = new Intl.DateTimeFormat(locale, options); - intl.set(key, i); - return i; -}; - -const browserFeatureDetectionDate = Intl.DateTimeFormat('en', { - hour: 'numeric', -}); - -interface FormatDateOptions extends Intl.DateTimeFormatOptions { - hourCycle?: string; -} - -const resolvedOptions: FormatDateOptions | undefined = - typeof browserFeatureDetectionDate.resolvedOptions === 'undefined' - ? undefined - : browserFeatureDetectionDate.resolvedOptions(); - -export function formatDate( - date: Date, - locales: string | string[], - options: FormatDateOptions = {}, -) { - const hourCycleRequired = - resolvedOptions != null && - options.hour12 != null && - resolvedOptions.hourCycle != null; - - if (hourCycleRequired) { - options.hour12 = undefined; - options.hourCycle = 'h23'; - } - - // Etc/GMT+12 is not supported in most browsers and there is no equivalent fallback - if (options.timeZone != null && options.timeZone === 'Etc/GMT+12') { - const adjustedDate = new Date(date.valueOf() - 12 * 60 * 60 * 1000); - return memoizedGetDateTimeFormat(locales, { - ...options, - timeZone: 'UTC', - }).format(adjustedDate); - } - - return memoizedGetDateTimeFormat(locales, options).format(date); -} - -function dateTimeFormatCacheKey( - locales: string | string[], - options?: Intl.DateTimeFormatOptions, -) { - return `${locales}-${JSON.stringify(options)}`; -} diff --git a/packages/react-i18n/src/utilities/translate.tsx b/packages/react-i18n/src/utilities/translate.tsx index 464f866a12..66d3d23ee9 100644 --- a/packages/react-i18n/src/utilities/translate.tsx +++ b/packages/react-i18n/src/utilities/translate.tsx @@ -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: '}', @@ -30,19 +41,13 @@ export interface TranslateOptions { 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); }