2019-12-16 06:26:10 -05:00
|
|
|
const sizes = ["Bytes", "KiB", "MiB", "GiB", "TiB", "PiB"];
|
2017-12-28 16:31:19 -05:00
|
|
|
|
2022-06-18 20:25:21 -04:00
|
|
|
export default (size: number) => {
|
2017-12-28 16:31:19 -05: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]}`;
|
|
|
|
};
|