Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / drive-store / utils / intl / dateFormatter.ts
blobe81f8bf352e9a0b55d8a213a16410a4501e27007
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);
15     }
17     return cache.formatter;
20 /**
21  * A cached Intl.DateTimeFormat object that ouptuts month names
22  *
23  * e.g. `January`
24  */
25 export const getMonthFormatter = () => getCachedFormatter(monthFormatterCache, { month: 'long' });
27 /**
28  * A cached Intl.DateTimeFormat object that outputs month names with the year attached
29  *
30  * e.g. `January 1970`
31  */
32 export const getMonthYearFormatter = () =>
33     getCachedFormatter(monthYearFormatterCache, { month: 'long', year: 'numeric' });