dashboard: BaseWidget - Improve _formatBytes function, remove nested if statements, always return size+unit, implement additional null check. (#7729)

This commit is contained in:
Monviech 2024-08-06 09:07:02 +02:00 committed by GitHub
parent 4182f19938
commit b76b69aacd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -239,17 +239,18 @@ export default class BaseWidget {
}
_formatBytes(value, decimals = 2) {
if (!isNaN(value) && value > 0) {
let fileSizeTypes = ["", "K", "M", "G", "T", "P", "E", "Z", "Y"];
let ndx = Math.floor(Math.log(value) / Math.log(1000) );
if (ndx > 0) {
return (value / Math.pow(1000, ndx)).toFixed(2) + ' ' + fileSizeTypes[ndx];
} else {
return value.toFixed(2);
}
} else {
if (isNaN(value) || value === null || value < 0) {
return "";
}
const fileSizeTypes = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
const ndx = Math.floor(Math.log(value) / Math.log(1000));
if (ndx === 0) {
return value.toFixed(decimals) + ' ' + fileSizeTypes[0];
}
return (value / Math.pow(1000, ndx)).toFixed(decimals) + ' ' + fileSizeTypes[ndx];
}
sanitizeSelector(selector) {