1 import { dateLocale } from '@proton/shared/lib/i18n';
3 type FormatterCache = { code?: string; formatter?: Intl.DateTimeFormat };
5 // Creating Intl.DateTimeFormat objects is expensive, so we have a local cache
6 // Since dateLocale can mutate, we update it when the code updates.
8 let monthFormatterCache: FormatterCache = {};
9 let monthYearFormatterCache: FormatterCache = {};
11 const getCachedFormatter = (cache: FormatterCache, options: Intl.DateTimeFormatOptions) => {
12 if (cache.code !== dateLocale.code || !cache.formatter) {
13 cache.code = dateLocale.code;
14 cache.formatter = new Intl.DateTimeFormat(dateLocale.code, options);
17 return cache.formatter;
21 * A cached Intl.DateTimeFormat object that ouptuts month names
25 export const getMonthFormatter = () => getCachedFormatter(monthFormatterCache, { month: 'long' });
28 * A cached Intl.DateTimeFormat object that outputs month names with the year attached
32 export const getMonthYearFormatter = () =>
33 getCachedFormatter(monthYearFormatterCache, { month: 'long', year: 'numeric' });