]> git.r.bdr.sh - rbdr/forum/blame_incremental - src/lib/utils/readable_time.ts
Update eslint
[rbdr/forum] / src / lib / utils / readable_time.ts
... / ...
CommitLineData
1type DateMagnitude = 'day' | 'hour' | 'minute' | 'second';
2
3type ReadableTime = {
4 count: number;
5 label: string;
6};
7
8const internals = {
9 magnitudes: {
10 day: 86400000,
11 hour: 3600000,
12 minute: 60000,
13 second: 1000
14 },
15 labels: {
16 day: 'time.days',
17 hour: 'time.hours',
18 minute: 'time.minutes',
19 second: 'time.seconds'
20 },
21
22 makeTimeReadable(time: number, magnitude: DateMagnitude): ReadableTime {
23 return {
24 count: Math.floor(time / internals.magnitudes[magnitude]),
25 label: internals.labels[magnitude]
26 };
27 }
28};
29
30export const readableTime = function readableTime(time: number): ReadableTime {
31 switch (true) {
32 case time >= internals.magnitudes.day:
33 return internals.makeTimeReadable(time, 'day');
34 case time >= internals.magnitudes.hour:
35 return internals.makeTimeReadable(time, 'hour');
36 case time >= internals.magnitudes.minute:
37 return internals.makeTimeReadable(time, 'minute');
38 case time < 0:
39 return internals.makeTimeReadable(0, 'second');
40 default:
41 return internals.makeTimeReadable(time, 'second');
42 }
43};