hardlounge/client/js/helpers/friendlysize.js

11 lines
367 B
JavaScript
Raw Normal View History

2017-12-28 21:31:19 +00:00
"use strict";
const sizes = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
2019-11-16 17:24:03 +00:00
export default (size) => {
2017-12-28 21:31:19 +00:00
// Loosely inspired from https://stackoverflow.com/a/18650828/1935861
const i = size > 0 ? Math.floor(Math.log(size) / Math.log(1024)) : 0;
const fixedSize = parseFloat((size / Math.pow(1024, i)).toFixed(1));
return `${fixedSize} ${sizes[i]}`;
};