Time units shorthand (#3862)

This commit is contained in:
Pavel Laptev 2024-05-25 18:51:05 +02:00 committed by GitHub
parent 9a42f8b2eb
commit 198e72612c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -17,9 +17,9 @@ export function createTimeAgoStore(
if (seconds < 10) {
set('just now');
} else if (seconds < 60) {
set(`< 1 minute ${addSuffix ? ' ago' : ''}`);
set(`< 1 min ${addSuffix ? ' ago' : ''}`);
} else {
set(formatDistanceToNowStrict(date, { addSuffix }));
set(customFormatDistance(date, addSuffix));
}
timeoutId = window.setTimeout(() => {
updateStore();
@ -46,3 +46,27 @@ function getSecondsUntilUpdate(seconds: number) {
return 3600;
}
}
// SHORTHAND WORDS
const unitShorthandMap: Record<string, string> = {
second: 'sec',
seconds: 'sec',
minute: 'min',
minutes: 'min',
hour: 'hr',
hours: 'hr',
day: 'day',
days: 'days',
month: 'mo',
months: 'mo',
year: 'yr',
years: 'yr'
};
function customFormatDistance(date: Date, addSuffix: boolean): string {
const distance = formatDistanceToNowStrict(date, { addSuffix });
return distance.replace(
/\b(seconds?|minutes?|hours?|days?|months?|years?)\b/g,
(match) => unitShorthandMap[match]
);
}