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

chore: add TB to internal file size converter #1094

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/shared/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export const matchFileType = (
return Micro.succeed(type);
};

export const FILESIZE_UNITS = ["B", "KB", "MB", "GB"] as const;
export const FILESIZE_UNITS = ["B", "KB", "MB", "GB", "TB"] as const;
export type FileSizeUnit = (typeof FILESIZE_UNITS)[number];
export const fileSizeToBytes = (
fileSize: FileSize,
Expand All @@ -163,8 +163,8 @@ export const bytesToFileSize = (bytes: number) => {
return "0B";
}

const i = Math.floor(Math.log(bytes) / Math.log(1000));
return `${(bytes / Math.pow(1000, i)).toFixed(2)}${FILESIZE_UNITS[i]}`;
const i = Math.floor(Math.log(bytes) / Math.log(1024));
return `${(bytes / Math.pow(1024, i)).toFixed(2)}${FILESIZE_UNITS[i]}`;
Comment on lines +166 to +167
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider aligning size units with standard conventions

The change from base 1000 to 1024 for size calculations introduces a discrepancy with the displayed units. Currently:

  • Using 1024 as base (binary prefix standard)
  • But displaying KB, MB, etc. (decimal prefix standard)

This mixing of standards could be confusing as:

  • 1 KB should be 1000 bytes (SI standard)
  • 1 KiB should be 1024 bytes (Binary standard)

Consider one of these solutions:

  1. Keep 1024 but use binary prefixes:
-export const FILESIZE_UNITS = ["B", "KB", "MB", "GB", "TB"] as const;
+export const FILESIZE_UNITS = ["B", "KiB", "MiB", "GiB", "TiB"] as const;
  1. Or revert to 1000 to match current decimal prefixes:
-  const i = Math.floor(Math.log(bytes) / Math.log(1024));
-  return `${(bytes / Math.pow(1024, i)).toFixed(2)}${FILESIZE_UNITS[i]}`;
+  const i = Math.floor(Math.log(bytes) / Math.log(1000));
+  return `${(bytes / Math.pow(1000, i)).toFixed(2)}${FILESIZE_UNITS[i]}`;

Committable suggestion skipped: line range outside the PR's diff.

};

export async function safeParseJSON<T>(
Expand Down
Loading