mirror of
https://github.com/thelounge/thelounge
synced 2024-11-22 20:13:07 +00:00
dd05ee3a65
Co-authored-by: Eric Nemchik <eric@nemchik.com> Co-authored-by: Pavel Djundik <xPaw@users.noreply.github.com>
8 lines
347 B
TypeScript
8 lines
347 B
TypeScript
const sizes = ["Bytes", "KiB", "MiB", "GiB", "TiB", "PiB"];
|
|
|
|
export default (size: number) => {
|
|
// 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]}`;
|
|
};
|