Skip to content

Commit 3fa1ade

Browse files
author
Bogdan Tsechoev
committed
Merge branch 'bot_ui_pluralize_time' into 'master'
Bot UI: Refactor timeAgo function to use existing pluralize utility See merge request postgres-ai/database-lab!896
2 parents 2454f58 + 0c20d3b commit 3fa1ade

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

ui/packages/platform/src/utils/format.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
import moment from 'moment'
9+
import { pluralize } from "./time";
910

1011
const Format = {
1112
formatSeconds: function (seconds: number, decimal: number, separator = ' ') {
@@ -266,6 +267,7 @@ const Format = {
266267
timeAgo: function (date: string | Date): string | null {
267268
if (!date) return null
268269

270+
269271
const now = new Date();
270272
const past = new Date(date);
271273
const diff = Math.abs(now.getTime() - past.getTime());
@@ -275,13 +277,13 @@ const Format = {
275277
const days = Math.floor(hours / 24);
276278

277279
if (seconds < 60) {
278-
return `${seconds} seconds ago`;
280+
return `${seconds} ${pluralize('second', 'seconds')(seconds)} ago`;
279281
} else if (minutes < 60) {
280-
return `${minutes} minutes ago`;
282+
return `${minutes} ${pluralize('minute', 'minutes')(minutes)} ago`;
281283
} else if (hours < 24) {
282-
return `${hours} hours ago`;
284+
return `${hours} ${pluralize('hour', 'hours')(hours)} ago`;
283285
} else {
284-
return `${days} days ago`;
286+
return `${days} ${pluralize('day', 'days')(seconds)} ago`;
285287
}
286288
}
287289
}

ui/packages/platform/src/utils/time.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const pluralize = (single: string, multiple: string) => (val: number) =>
1+
export const pluralize = (single: string, multiple: string) => (val: number) =>
22
val === 1 ? single : multiple
33

44
const MS_IN_SECOND = 1000

0 commit comments

Comments
 (0)