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