Memoize getShortDateStr
parent
7723f551c8
commit
beb4219b1b
|
@ -1,17 +1,32 @@
|
||||||
import { getCanonicalLocale } from "@nextcloud/l10n";
|
import { getCanonicalLocale } from "@nextcloud/l10n";
|
||||||
|
|
||||||
|
// Memoize the result of short date conversions
|
||||||
|
// These operations are surprisingly expensive
|
||||||
|
// and we do them a lot because of scroller hover
|
||||||
|
const shortDateStrMemo = new Map<number, string>();
|
||||||
|
|
||||||
/** Get JS date object from dayId */
|
/** Get JS date object from dayId */
|
||||||
export function dayIdToDate(dayId: number){
|
export function dayIdToDate(dayId: number){
|
||||||
return new Date(dayId*86400*1000);
|
return new Date(dayId*86400*1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Get Day ID from JS date */
|
||||||
|
export function dateToDayId(date: Date){
|
||||||
|
return Math.floor(date.getTime() / (86400*1000));
|
||||||
|
}
|
||||||
|
|
||||||
/** Get month name from number */
|
/** Get month name from number */
|
||||||
export function getShortDateStr(date: Date) {
|
export function getShortDateStr(date: Date) {
|
||||||
return date.toLocaleDateString(getCanonicalLocale(), {
|
const dayId = dateToDayId(date);
|
||||||
month: 'short',
|
if (!shortDateStrMemo.has(dayId)) {
|
||||||
year: 'numeric',
|
shortDateStrMemo.set(dayId,
|
||||||
timeZone: 'UTC',
|
date.toLocaleDateString(getCanonicalLocale(), {
|
||||||
});
|
month: 'short',
|
||||||
|
year: 'numeric',
|
||||||
|
timeZone: 'UTC',
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
return shortDateStrMemo.get(dayId);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Get long date string with optional year if same as current */
|
/** Get long date string with optional year if same as current */
|
||||||
|
|
Loading…
Reference in New Issue