]> git.r.bdr.sh - rbdr/forum/blame - src/lib/utils/readable_time.ts
Update eslint
[rbdr/forum] / src / lib / utils / readable_time.ts
CommitLineData
a7cf03c1
RBR
1type DateMagnitude = 'day' | 'hour' | 'minute' | 'second';
2
3type ReadableTime = {
cac85db0
RBR
4 count: number;
5 label: string;
a7cf03c1
RBR
6};
7
c1bc5993 8const internals = {
cac85db0
RBR
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 },
c1bc5993 21
cac85db0
RBR
22 makeTimeReadable(time: number, magnitude: DateMagnitude): ReadableTime {
23 return {
24 count: Math.floor(time / internals.magnitudes[magnitude]),
25 label: internals.labels[magnitude]
26 };
27 }
c1bc5993
RBR
28};
29
a7cf03c1 30export const readableTime = function readableTime(time: number): ReadableTime {
cac85db0
RBR
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 }
c1bc5993 43};