fix: Cannot read properties of undefined
This commit is contained in:
@@ -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");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user