From 01c6952ec9b19b18418ef8abcc87cc743d9a2216 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Wed, 29 Nov 2023 19:42:00 -0500 Subject: [PATCH] fix: Cannot read properties of undefined --- shared/utils/files.test.ts | 3 +++ shared/utils/files.ts | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/shared/utils/files.test.ts b/shared/utils/files.test.ts index f0b2295e7..fb4faa9fe 100644 --- a/shared/utils/files.test.ts +++ b/shared/utils/files.test.ts @@ -3,12 +3,15 @@ import { bytesToHumanReadable } from "./files"; describe("bytesToHumanReadable", () => { test("Outputs readable string", () => { expect(bytesToHumanReadable(0)).toBe("0 Bytes"); + expect(bytesToHumanReadable(0.0)).toBe("0 Bytes"); + expect(bytesToHumanReadable(33)).toBe("33 Bytes"); expect(bytesToHumanReadable(500)).toBe("500 Bytes"); expect(bytesToHumanReadable(1000)).toBe("1 kB"); expect(bytesToHumanReadable(15000)).toBe("15 kB"); expect(bytesToHumanReadable(12345)).toBe("12.34 kB"); expect(bytesToHumanReadable(123456)).toBe("123.45 kB"); expect(bytesToHumanReadable(1234567)).toBe("1.23 MB"); + expect(bytesToHumanReadable(1234567890)).toBe("1.23 GB"); expect(bytesToHumanReadable(undefined)).toBe("0 Bytes"); }); }); diff --git a/shared/utils/files.ts b/shared/utils/files.ts index 2484110e5..63833944f 100644 --- a/shared/utils/files.ts +++ b/shared/utils/files.ts @@ -17,7 +17,7 @@ export function bytesToHumanReadable(bytes: number | undefined) { return bytes + " Bytes"; } - const f = out[1].substring(0, 2); + const f = (out[1] ?? "").substring(0, 2); return `${Number(out[0])}${f === "00" ? "" : `.${f}`} ${ " kMGTPEZY"[out.length]