feat: handle incorrect sizes

This commit is contained in:
Sven SAULEAU 2018-03-29 15:28:03 +02:00
parent 2bd495cec1
commit 7b6f56ef61
No known key found for this signature in database
GPG Key ID: F5464AC83B687AD1
2 changed files with 9 additions and 0 deletions

View File

@ -7,6 +7,10 @@
const SizeFormatHelpers = exports;
SizeFormatHelpers.formatSize = size => {
if (typeof size !== "number" || Number.isNaN(size) === true) {
return "unknown size";
}
if (size <= 0) {
return "0 bytes";
}

View File

@ -43,5 +43,10 @@ describe("SizeFormatHelpers", () => {
"1.2 GiB"
);
});
it("should handle undefined/NaN", () => {
should(SizeFormatHelpers.formatSize(undefined)).be.eql("unknown size");
should(SizeFormatHelpers.formatSize(NaN)).be.eql("unknown size");
});
});
});