Skip to content

Commit c8310e5

Browse files
authored
refactor: replaced custom byte size calculation with @std/fmt (#1010)
Showcase of @std/fmt usage: ![image](https://github.com/user-attachments/assets/396634ef-ad6e-4bec-a433-d0c54498f80f)
1 parent 96139df commit c8310e5

File tree

1 file changed

+2
-8
lines changed

1 file changed

+2
-8
lines changed

frontend/routes/package/(_islands)/DependencyGraph.tsx

+2-8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type {
1919
DependencyGraphKindNpm,
2020
DependencyGraphKindRoot,
2121
} from "../../../utils/api_types.ts";
22+
import { format as formatBytes } from "@std/fmt/bytes";
2223

2324
export interface DependencyGraphProps {
2425
dependencies: DependencyGraphItem[];
@@ -216,13 +217,6 @@ function createDigraph(dependencies: DependencyGraphItem[]) {
216217
}`;
217218
}
218219

219-
function bytesToSize(bytes: number) {
220-
const sizes = ["B", "KB", "MB", "GB", "TB"];
221-
if (bytes == 0) return "0 B";
222-
const i = Math.floor(Math.log(bytes) / Math.log(1024));
223-
return (bytes / Math.pow(1024, i)).toFixed(0) + " " + sizes[i];
224-
}
225-
226220
function renderDependency(
227221
dependency: GroupedDependencyGraphKind,
228222
size?: number,
@@ -244,7 +238,7 @@ function renderDependency(
244238
return entrypoint;
245239
}
246240
}).join("\n")
247-
}\n${bytesToSize(size ?? 0)}`;
241+
}\n${formatBytes(size ?? 0, { maximumFractionDigits: 0 }).toUpperCase()}`;
248242
color = "#faee4a";
249243
break;
250244
}

0 commit comments

Comments
 (0)