diff --git a/shared/utils/files.test.ts b/shared/utils/files.test.ts index 4555f0689..f0b2295e7 100644 --- a/shared/utils/files.test.ts +++ b/shared/utils/files.test.ts @@ -9,5 +9,6 @@ describe("bytesToHumanReadable", () => { expect(bytesToHumanReadable(12345)).toBe("12.34 kB"); expect(bytesToHumanReadable(123456)).toBe("123.45 kB"); expect(bytesToHumanReadable(1234567)).toBe("1.23 MB"); + expect(bytesToHumanReadable(undefined)).toBe("0 Bytes"); }); }); diff --git a/shared/utils/files.ts b/shared/utils/files.ts index efcb544f6..2484110e5 100644 --- a/shared/utils/files.ts +++ b/shared/utils/files.ts @@ -4,7 +4,11 @@ * @param bytes filesize in bytes * @returns Human readable filesize as a string */ -export function bytesToHumanReadable(bytes: number) { +export function bytesToHumanReadable(bytes: number | undefined) { + if (!bytes) { + return "0 Bytes"; + } + const out = ("0".repeat((bytes.toString().length * 2) % 3) + bytes).match( /.{3}/g );