Use friendlysize helper consistently
Co-Authored-By: fnutt <fnutt@users.noreply.github.com>
This commit is contained in:
parent
f0b0c53536
commit
f2bf1fa90a
@ -3,7 +3,7 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import constants from "../js/constants";
|
import friendlysize from "../js/helpers/friendlysize";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "LinkPreviewFileSize",
|
name: "LinkPreviewFileSize",
|
||||||
@ -12,22 +12,7 @@ export default {
|
|||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
previewSize() {
|
previewSize() {
|
||||||
let size = this.size;
|
return friendlysize(this.size);
|
||||||
|
|
||||||
// Threshold for SI units
|
|
||||||
const thresh = 1024;
|
|
||||||
|
|
||||||
let u = 0;
|
|
||||||
|
|
||||||
do {
|
|
||||||
size /= thresh;
|
|
||||||
++u;
|
|
||||||
} while (size >= thresh && u < constants.sizeUnits.length - 1);
|
|
||||||
|
|
||||||
const displaySize = size.toFixed(1);
|
|
||||||
const unit = constants.sizeUnits[u];
|
|
||||||
|
|
||||||
return `${displaySize} ${unit}`;
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -27,15 +27,12 @@ const timeFormats = {
|
|||||||
msgWithSeconds: "HH:mm:ss",
|
msgWithSeconds: "HH:mm:ss",
|
||||||
};
|
};
|
||||||
|
|
||||||
const sizeUnits = ["B", "KiB", "MiB", "GiB", "TiB"];
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
colorCodeMap,
|
colorCodeMap,
|
||||||
commands: [],
|
commands: [],
|
||||||
condensedTypes,
|
condensedTypes,
|
||||||
condensedTypesQuery,
|
condensedTypesQuery,
|
||||||
timeFormats,
|
timeFormats,
|
||||||
sizeUnits,
|
|
||||||
// Same value as media query in CSS that forces sidebars to become overlays
|
// Same value as media query in CSS that forces sidebars to become overlays
|
||||||
mobileViewportPixels: 768,
|
mobileViewportPixels: 768,
|
||||||
};
|
};
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
|
const sizes = ["Bytes", "KiB", "MiB", "GiB", "TiB", "PiB"];
|
||||||
|
|
||||||
export default (size) => {
|
export default (size) => {
|
||||||
// Loosely inspired from https://stackoverflow.com/a/18650828/1935861
|
// Loosely inspired from https://stackoverflow.com/a/18650828/1935861
|
||||||
|
@ -4,15 +4,26 @@ const expect = require("chai").expect;
|
|||||||
const friendlysize = require("../../../../client/js/helpers/friendlysize").default;
|
const friendlysize = require("../../../../client/js/helpers/friendlysize").default;
|
||||||
|
|
||||||
describe("friendlysize helper", function() {
|
describe("friendlysize helper", function() {
|
||||||
it("should render big values in human-readable version", function() {
|
it("should render human-readable version", function() {
|
||||||
expect(friendlysize(51200)).to.equal("50 KB");
|
expect(friendlysize(51200)).to.equal("50 KiB");
|
||||||
|
expect(friendlysize(2)).to.equal("2 Bytes");
|
||||||
|
expect(friendlysize(1023)).to.equal("1023 Bytes");
|
||||||
|
expect(friendlysize(1024)).to.equal("1 KiB");
|
||||||
|
expect(friendlysize(Math.pow(1024, 2))).to.equal("1 MiB");
|
||||||
|
expect(friendlysize(Math.pow(1024, 3))).to.equal("1 GiB");
|
||||||
|
expect(friendlysize(Math.pow(1024, 4))).to.equal("1 TiB");
|
||||||
|
expect(friendlysize(Math.pow(1024, 5))).to.equal("1 PiB");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should round with 1 digit", function() {
|
it("should round with 1 digit", function() {
|
||||||
expect(friendlysize(1234567)).to.equal("1.2 MB");
|
expect(friendlysize(1234567)).to.equal("1.2 MiB");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should render special case 0 as 0 Bytes", function() {
|
it("should render special case 0 as 0 Bytes", function() {
|
||||||
expect(friendlysize(0)).to.equal("0 Bytes");
|
expect(friendlysize(0)).to.equal("0 Bytes");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should render max safe integer as petabytes", function() {
|
||||||
|
expect(friendlysize(Number.MAX_SAFE_INTEGER)).to.equal("8 PiB");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user