Skip to content

Commit 9732e37

Browse files
committed
Display file size in kibibytes in statistics. Fix webpack#5769
1 parent b8f181f commit 9732e37

2 files changed

Lines changed: 44 additions & 3 deletions

File tree

lib/SizeFormatHelpers.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ SizeFormatHelpers.formatSize = size => {
1111
return "0 bytes";
1212
}
1313

14-
const abbreviations = ["bytes", "kB", "MB", "GB"];
15-
const index = Math.floor(Math.log(size) / Math.log(1000));
14+
const abbreviations = ["bytes", "KiB", "MiB", "GiB"];
15+
const index = Math.floor(Math.log(size) / Math.log(1024));
1616

17-
return `${+(size / Math.pow(1000, index)).toPrecision(3)} ${abbreviations[index]}`;
17+
return `${+(size / Math.pow(1024, index)).toPrecision(3)} ${abbreviations[index]}`;
1818
};

test/SizeFormatHelpers.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* globals describe, it, beforeEach */
2+
"use strict";
3+
4+
const should = require("should");
5+
const SizeFormatHelpers = require("../lib/SizeFormatHelpers");
6+
7+
describe("SizeFormatHelpers", () => {
8+
describe("formatSize", () => {
9+
it("should handle zero size", () => {
10+
should(SizeFormatHelpers.formatSize(0)).be.eql("0 bytes");
11+
});
12+
13+
it("should handle bytes", () => {
14+
should(SizeFormatHelpers.formatSize(1000)).be.eql("1000 bytes");
15+
});
16+
17+
it("should handle integer kibibytes", () => {
18+
should(SizeFormatHelpers.formatSize(2048)).be.eql("2 KiB");
19+
});
20+
21+
it("should handle float kibibytes", () => {
22+
should(SizeFormatHelpers.formatSize(2560)).be.eql("2.5 KiB");
23+
});
24+
25+
it("should handle integer mebibytes", () => {
26+
should(SizeFormatHelpers.formatSize(10 * 1024 * 1024)).be.eql("10 MiB");
27+
});
28+
29+
it("should handle float mebibytes", () => {
30+
should(SizeFormatHelpers.formatSize(12.5 * 1024 * 1024)).be.eql("12.5 MiB");
31+
});
32+
33+
it("should handle integer gibibytes", () => {
34+
should(SizeFormatHelpers.formatSize(3 * 1024 * 1024 * 1024)).be.eql("3 GiB");
35+
});
36+
37+
it("should handle float gibibytes", () => {
38+
should(SizeFormatHelpers.formatSize(1.2 * 1024 * 1024 * 1024)).be.eql("1.2 GiB");
39+
});
40+
});
41+
});

0 commit comments

Comments
 (0)