From 78c412a51700b28f158b8238b1e1954ec43e7f29 Mon Sep 17 00:00:00 2001 From: hamster1963 <1410514192@qq.com> Date: Wed, 10 Jun 2026 15:04:05 +0800 Subject: [PATCH] fix: update formatBytes function to return "0 KiB" for zero bytes and streamline size array --- src/lib/format.ts | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/lib/format.ts b/src/lib/format.ts index e255566..0db5ed3 100644 --- a/src/lib/format.ts +++ b/src/lib/format.ts @@ -1,21 +1,12 @@ export function formatBytes(bytes: number, decimals: number = 2) { - if (!+bytes) return "0 Bytes"; + if (!+bytes) return "0 KiB"; const k = 1024; const dm = decimals < 0 ? 0 : decimals; - const sizes = [ - "Bytes", - "KiB", - "MiB", - "GiB", - "TiB", - "PiB", - "EiB", - "ZiB", - "YiB", - ]; + const sizes = ["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]}`; }