mirror of
https://github.com/karl0ss/homepage.git
synced 2025-04-29 12:03:41 +01:00
Enhancement: add remap, scale, and suffx to customapi fields (#1958)
This commit is contained in:
parent
2d8160512f
commit
eb2b37063f
@ -27,18 +27,60 @@ function getValue(field, data) {
|
|||||||
return value[lastField] ?? null;
|
return value[lastField] ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function formatValue(t, mapping, value) {
|
function formatValue(t, mapping, rawValue) {
|
||||||
|
let value = rawValue;
|
||||||
|
|
||||||
|
// Remap the value.
|
||||||
|
const remaps = mapping?.remap ?? [];
|
||||||
|
for (let i = 0; i < remaps.length; i += 1) {
|
||||||
|
const remap = remaps[i];
|
||||||
|
if (remap?.any || remap?.value === value) {
|
||||||
|
value = remap.to;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Scale the value. Accepts either a number to multiply by or a string
|
||||||
|
// like "12/345".
|
||||||
|
const scale = mapping?.scale;
|
||||||
|
if (typeof scale === 'number') {
|
||||||
|
value *= scale;
|
||||||
|
} else if (typeof scale === 'string') {
|
||||||
|
const parts = scale.split('/');
|
||||||
|
const numerator = parts[0] ? parseFloat(parts[0]) : 1;
|
||||||
|
const denominator = parts[1] ? parseFloat(parts[1]) : 1;
|
||||||
|
value = value * numerator / denominator;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Format the value using a known type.
|
||||||
switch (mapping?.format) {
|
switch (mapping?.format) {
|
||||||
case 'number':
|
case 'number':
|
||||||
return t("common.number", { value: parseInt(value, 10) });
|
value = t("common.number", { value: parseInt(value, 10) });
|
||||||
|
break;
|
||||||
case 'float':
|
case 'float':
|
||||||
return t("common.number", { value });
|
value = t("common.number", { value });
|
||||||
|
break;
|
||||||
case 'percent':
|
case 'percent':
|
||||||
return t("common.percent", { value });
|
value = t("common.percent", { value });
|
||||||
|
break;
|
||||||
|
case 'bytes':
|
||||||
|
value = t("common.bytes", { value });
|
||||||
|
break;
|
||||||
|
case 'bitrate':
|
||||||
|
value = t("common.bitrate", { value });
|
||||||
|
break;
|
||||||
case 'text':
|
case 'text':
|
||||||
default:
|
default:
|
||||||
return value;
|
// nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Apply fixed suffix.
|
||||||
|
const suffix = mapping?.suffix;
|
||||||
|
if (suffix) {
|
||||||
|
value = `${value} ${suffix}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Component({ service }) {
|
export default function Component({ service }) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user