From 198e72612cfe06c2a6084b5b0f24bf30147db819 Mon Sep 17 00:00:00 2001 From: Pavel Laptev Date: Sat, 25 May 2024 18:51:05 +0200 Subject: [PATCH] Time units shorthand (#3862) --- app/src/lib/utils/timeAgo.ts | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/app/src/lib/utils/timeAgo.ts b/app/src/lib/utils/timeAgo.ts index f18121798..8cb8deeec 100644 --- a/app/src/lib/utils/timeAgo.ts +++ b/app/src/lib/utils/timeAgo.ts @@ -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 = { + 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] + ); +}