this repo has no description
plcbundle-watch.pages.dev
1import numeral from 'numeral';
2
3export function formatNumber(n: number) {
4 return numeral(n).format()
5}
6
7export function formatUptime(totalSeconds: number): string {
8 if (totalSeconds < 0) {
9 return "Invalid input: seconds cannot be negative";
10 }
11 if (totalSeconds === 0) {
12 return "0s";
13 }
14
15 let parts: string[] = [];
16 let displayUnits: [string, number][];
17
18 const SECONDS_IN_MINUTE = 60;
19 const SECONDS_IN_HOUR = 3600; // 60 minutes * 60 seconds/minute
20 const SECONDS_IN_DAY = 24 * SECONDS_IN_HOUR;
21
22 if (totalSeconds > SECONDS_IN_DAY) { // If uptime is bigger than 1 day
23 displayUnits = [
24 ['d', SECONDS_IN_DAY],
25 ['h', SECONDS_IN_HOUR],
26 ];
27 } else if (totalSeconds > SECONDS_IN_MINUTE) { // If uptime is bigger than 60 seconds but not bigger than 60 minutes
28 displayUnits = [
29 ['d', SECONDS_IN_DAY],
30 ['h', SECONDS_IN_HOUR],
31 ['m', SECONDS_IN_MINUTE],
32 ];
33 } else { // If uptime is 60 seconds or less
34 displayUnits = [
35 ['d', SECONDS_IN_DAY],
36 ['h', SECONDS_IN_HOUR],
37 ['m', SECONDS_IN_MINUTE],
38 ['s', 1],
39 ];
40 }
41
42 let remainingSeconds = totalSeconds;
43
44 for (const [unitChar, unitSeconds] of displayUnits) {
45 if (remainingSeconds >= unitSeconds) {
46 const value = Math.floor(remainingSeconds / unitSeconds);
47 parts.push(`${value}${unitChar}`);
48 remainingSeconds %= unitSeconds;
49 }
50 }
51
52 return parts.join("");
53 }