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