Skip to content

Commit 133b81e

Browse files
authored
refactor: Usage of @std/fmt instead of manual size calculation (#1004)
Showcase of @std/fmt usage: ![image](https://github.com/user-attachments/assets/ee22ab56-8885-4c67-a222-678508789456)
1 parent 8b804f8 commit 133b81e

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

frontend/deno.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
},
1515
"imports": {
1616
"@preact-icons/tb": "jsr:@preact-icons/tb@^1.0.12",
17+
"@std/fmt": "jsr:@std/fmt@^1.0.6",
1718
"fresh": "jsr:@fresh/core@^2.0.0-alpha.25",
1819
"@fresh/plugin-tailwind": "jsr:@fresh/plugin-tailwind@^0.0.1-alpha.7",
1920

frontend/deno.lock

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/routes/package/source.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { PackageHeader } from "./(_components)/PackageHeader.tsx";
88
import { TbFolder, TbSourceCode } from "@preact-icons/tb";
99
import { ListDisplay } from "../../components/List.tsx";
1010
import { scopeIAM } from "../../utils/iam.ts";
11+
import { format as formatBytes } from "@std/fmt/bytes";
1112

1213
export default define.page<typeof handler>(function PackagePage(
1314
{ data, params, state },
@@ -136,19 +137,12 @@ function DirEntry({ entry }: { entry: SourceDirEntry }) {
136137
</div>
137138
</div>
138139
<div class="text-sm text-jsr-gray-600">
139-
{bytesToSize(entry.size)}
140+
{formatBytes(entry.size, { maximumFractionDigits: 0 }).toUpperCase()}
140141
</div>
141142
</div>
142143
);
143144
}
144145

145-
function bytesToSize(bytes: number) {
146-
const sizes = ["B", "KB", "MB", "GB", "TB"];
147-
if (bytes == 0) return "0 B";
148-
const i = Math.floor(Math.log(bytes) / Math.log(1024));
149-
return (bytes / Math.pow(1024, i)).toFixed(0) + " " + sizes[i];
150-
}
151-
152146
const LINE_COL_REGEX = /(.*):(\d+):(\d+)$/;
153147

154148
export const handler = define.handlers({

0 commit comments

Comments
 (0)