Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.
JavaScript aianthropiccross-platformgeminigroqlinuxmacosnvidiaopen-sourceopenaiparakeetspeech-to-texttranscribewhisperwindows
fix(utils): format sub-1, non-finite, and negative byte values safely (#1448)
Henry Sue3047151f5d8
diff --git a/src/utils/formatBytes.ts b/src/utils/formatBytes.tsindex b01a22811..7b84c26db 100644--- a/src/utils/formatBytes.ts+++ b/src/utils/formatBytes.ts@@ -1,11 +1,11 @@export function formatBytes(bytes: number, decimals = 2): string {- if (bytes === 0) return "0 Bytes";+ if (!Number.isFinite(bytes) || bytes <= 0) return "0 Bytes";const k = 1024;const dm = decimals < 0 ? 0 : decimals;const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];- const i = Math.floor(Math.log(bytes) / Math.log(k));+ const i = Math.min(sizes.length - 1, Math.max(0, Math.floor(Math.log(bytes) / Math.log(k))));return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];}diff --git a/test/helpers/formatBytes.test.js b/test/helpers/formatBytes.test.jsindex 13fe94166..a6c23351c 100644--- a/test/helpers/formatBytes.test.js+++ b/test/helpers/formatBytes.test.js@@ -33,3 +33,14 @@ test("fractional values respect the decimals parameter, with negatives clamped tassert.equal(formatBytes(1536, 3), "1.5 KB");assert.equal(formatBytes(1536, -1), "2 KB");});++test("negative, non-finite, and sub-1 byte values format safely without undefined units", async () => {+ const { formatBytes } = await load();++ assert.equal(formatBytes(-100), "0 Bytes");+ assert.equal(formatBytes(-1), "0 Bytes");+ assert.equal(formatBytes(NaN), "0 Bytes");+ assert.equal(formatBytes(Infinity), "0 Bytes");+ assert.equal(formatBytes(0.5), "0.5 Bytes");+ assert.equal(formatBytes(0.1, 1), "0.1 Bytes");+});