-
-
Notifications
You must be signed in to change notification settings - Fork 378
/
utils.ts
235 lines (199 loc) · 6.35 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//Adapted from https://stackoverflow.com/a/34270811/10951033
import {ValetudoDataPoint} from "./api";
import {useCallback, useLayoutEffect, useRef} from "react";
export function convertSecondsToHumans(seconds: number, showSeconds = true, showDays = true): string {
let levels;
if (showDays) {
levels = [
{
value: Math.floor(seconds / 86400),
label: "d"
},
{
value: Math.floor((seconds % 86400) / 3600).toString().padStart(2, "0"),
label: "h"
},
{
value: Math.floor(((seconds % 86400) % 3600) / 60).toString().padStart(2, "0"),
label: "m"
}
];
if (showSeconds) {
levels.push(
{
value: (((seconds % 86400) % 3600) % 60).toString().padStart(2, "0"),
label: "s"
}
);
}
} else {
levels = [
{
value: Math.floor(seconds / 3600).toString().padStart(2, "0"),
label: "h"
},
{
value: Math.floor((seconds % 3600) / 60).toString().padStart(2, "0"),
label: "m"
}
];
if (showSeconds) {
levels.push(
{
value: ((seconds % 3600) % 60).toString().padStart(2, "0"),
label: "s"
}
);
}
}
let humanReadableTimespan = "";
levels.forEach((lvl) => {
humanReadableTimespan += lvl.value + lvl.label + " ";
});
return humanReadableTimespan.trim();
}
export function convertBytesToHumans(bytes: number): string {
if (bytes >= 1024*1024*1024) {
return `${(((bytes/1024)/1024)/1024).toFixed(2)} GiB`;
} else if (bytes >= 1024*1024) {
return `${((bytes/1024)/1024).toFixed(2)} MiB`;
} else if (bytes >= 1024) {
return `${(bytes/1024).toFixed(2)} KiB`;
} else {
return `${bytes} bytes`;
}
}
//Adapted from https://stackoverflow.com/a/41358305
export function convertNumberToRoman(num: number): string {
const symbols: Record<string, number> = {
XC: 90,
L: 50,
XL: 40,
X: 10,
IX: 9,
V: 5,
IV: 4,
I: 1
};
let str = "";
for (const i of Object.keys(symbols)) {
const quantity = Math.floor(num / symbols[i]);
num -= quantity * symbols[i];
str += i.repeat(quantity);
}
return str;
}
// Adapted from https://gist.github.com/erikvullings/ada7af09925082cbb89f40ed962d475e
export const deepCopy = <T>(target: T): T => {
if (target === null) {
return target;
}
if (target instanceof Date) {
return new Date(target.getTime()) as any;
}
if (target instanceof Array) {
const cp = [] as any[];
(target as any[]).forEach((v) => {
cp.push(v);
});
return cp.map((n: any) => {
return deepCopy<any>(n);
}) as any;
}
if (typeof target === "object" && Object.keys(target).length !== 0) {
const cp = {...(target as { [key: string]: any })} as { [key: string]: any };
Object.keys(cp).forEach(k => {
cp[k] = deepCopy<any>(cp[k]);
});
return cp as T;
}
return target;
};
const consumableTypeMapping: Record<string, string> = {
"brush": "Brush",
"filter": "Filter",
"sensor": "Sensor cleaning",
"mop": "Mop",
"detergent": "Detergent"
};
const consumableSubtypeMapping: Record<string, string> = {
"main": "Main",
"secondary": "Secondary",
"side_right": "Right",
"side_left": "Left",
"all": "",
"none": ""
};
export const getConsumableName = (type: string, subType?: string): string => {
let ret = "";
if (subType && subType in consumableSubtypeMapping) {
ret += consumableSubtypeMapping[subType] + " ";
}
if (type in consumableTypeMapping) {
ret += consumableTypeMapping[type];
}
return ret.trim() || "Unknown consumable: " + type + ", " + subType;
};
// Adapted from https://stackoverflow.com/a/53660837
export const median = (numbers: Array<number>): number => { //Note that this will modify the input array
numbers.sort((a, b) => {
return a - b;
});
const middle = Math.floor(numbers.length / 2);
if (numbers.length % 2 === 0) {
return (numbers[middle - 1] + numbers[middle]) / 2;
}
return numbers[middle];
};
export function getFriendlyStatName(stat: ValetudoDataPoint) : string {
switch (stat.type) {
case "area":
return "Area";
case "time":
return "Time";
case "count":
return "Count";
}
}
export function getHumanReadableStatValue(stat: ValetudoDataPoint): string {
switch (stat.type) {
case "area":
return (stat.value / 10000).toFixed(2).padStart(6, "0") + " m²";
case "time":
return convertSecondsToHumans(stat.value, true, false);
case "count":
return stat.value.toString();
}
}
//adapted from https://stackoverflow.com/a/60880664
export function adjustColorBrightness(hexInput: string, percent: number) : string {
let hex = hexInput;
// strip the leading # if it's there
hex = hex.trim().replace("#","");
// convert 3 char codes --> 6, e.g. `E0F` --> `EE00FF`
if (hex.length === 3) {
hex = hex.replace(/(.)/g, "$1$1");
}
let r = parseInt(hex.slice(0, 2), 16);
let g = parseInt(hex.slice(2, 4), 16);
let b = parseInt(hex.slice(4, 6), 16);
const calculatedPercent = (100 + percent) / 100;
r = Math.round(Math.min(255, Math.max(0, r * calculatedPercent)));
g = Math.round(Math.min(255, Math.max(0, g * calculatedPercent)));
b = Math.round(Math.min(255, Math.max(0, b * calculatedPercent)));
let result = "#";
result += r.toString(16).toUpperCase().padStart(2, "0");
result += g.toString(16).toUpperCase().padStart(2, "0");
result += b.toString(16).toUpperCase().padStart(2, "0");
return result;
}
//adapted from https://stackoverflow.com/a/69331524
export const useGetter = <S>(value: S): (() => S) => {
const ref = useRef(value);
useLayoutEffect(() => {
ref.current = value;
});
return useCallback(() => {
return ref.current;
}, [ref]);
};