fix: Handle missing size on attachment

This commit is contained in:
Tom Moor
2023-01-25 22:30:21 -05:00
parent 6ab428a498
commit 784631baf4
2 changed files with 6 additions and 1 deletions

View File

@@ -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");
});
});

View File

@@ -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
);