mirror of
https://github.com/Buriburizaem0n/nezha-dash-v1.git
synced 2026-09-19 09:40:14 +00:00
fix: update formatBytes function to return "0 KiB" for zero bytes and streamline size array
This commit is contained in:
+5
-14
@@ -1,21 +1,12 @@
|
|||||||
export function formatBytes(bytes: number, decimals: number = 2) {
|
export function formatBytes(bytes: number, decimals: number = 2) {
|
||||||
if (!+bytes) return "0 Bytes";
|
if (!+bytes) return "0 KiB";
|
||||||
|
|
||||||
const k = 1024;
|
const k = 1024;
|
||||||
const dm = decimals < 0 ? 0 : decimals;
|
const dm = decimals < 0 ? 0 : decimals;
|
||||||
const sizes = [
|
const sizes = ["KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"];
|
||||||
"Bytes",
|
|
||||||
"KiB",
|
|
||||||
"MiB",
|
|
||||||
"GiB",
|
|
||||||
"TiB",
|
|
||||||
"PiB",
|
|
||||||
"EiB",
|
|
||||||
"ZiB",
|
|
||||||
"YiB",
|
|
||||||
];
|
|
||||||
|
|
||||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
const i = Math.max(0, Math.floor(Math.log(bytes) / Math.log(k)) - 1);
|
||||||
|
const value = (bytes / k ** (i + 1)).toFixed(dm);
|
||||||
|
|
||||||
return `${parseFloat((bytes / k ** i).toFixed(dm))} ${sizes[i]}`;
|
return `${value} ${sizes[i]}`;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user