Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add edge case checks and rework logic friendlysize #4666

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 13 additions & 6 deletions client/js/helpers/friendlysize.ts
@@ -1,8 +1,15 @@
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]}`;
};
export default function formatSize(size: number): string {
if (size <= 0) {
throw new Error("Size must be a positive number");
}

const i = Math.floor(Math.log(size) / Math.log(1024));

if (i >= sizes.length) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this must not error, simply emit a large number

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear you just want it to emit the KB value?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why KB?
You are guarding the index value, simply pick the highest available one namely sizes.length and do the dance same as you did in the other patch.

throw new Error("Size is out of range");
}

return `${(size / Math.pow(1024, i)).toFixed(1)} ${sizes[i]}`;
}