Merge pull request #3596 from thelounge/xpaw/friendly-size

Use `friendlysize` helper consistently
This commit is contained in:
Pavel Djundik 2019-12-17 15:43:58 +02:00 committed by GitHub
commit 86341f063c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 24 deletions

View file

@ -3,7 +3,7 @@
</template>
<script>
import constants from "../js/constants";
import friendlysize from "../js/helpers/friendlysize";
export default {
name: "LinkPreviewFileSize",
@ -12,22 +12,7 @@ export default {
},
computed: {
previewSize() {
let size = 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}`;
return friendlysize(this.size);
},
},
};

View file

@ -26,14 +26,11 @@ const timeFormats = {
msgWithSeconds: "HH:mm:ss",
};
const sizeUnits = ["B", "KiB", "MiB", "GiB", "TiB"];
export default {
colorCodeMap,
commands: [],
condensedTypes,
timeFormats,
sizeUnits,
// Same value as media query in CSS that forces sidebars to become overlays
mobileViewportPixels: 768,
};

View file

@ -1,6 +1,6 @@
"use strict";
const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
const sizes = ["Bytes", "KiB", "MiB", "GiB", "TiB", "PiB"];
export default (size) => {
// Loosely inspired from https://stackoverflow.com/a/18650828/1935861

View file

@ -4,15 +4,26 @@ const expect = require("chai").expect;
const friendlysize = require("../../../../client/js/helpers/friendlysize").default;
describe("friendlysize helper", function() {
it("should render big values in human-readable version", function() {
expect(friendlysize(51200)).to.equal("50 KB");
it("should render human-readable version", function() {
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() {
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() {
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");
});
});