From 2a9a4b9eefaef513df5437a9d548e1941583fc1e Mon Sep 17 00:00:00 2001 From: whoswhip Date: Wed, 7 Jan 2026 20:18:42 -0500 Subject: [PATCH] fix: improve formatBytes function and display byte size in textarea --- SharpbinV3.Client/src/lib/utils/misc.ts | 18 ++++++---- SharpbinV3.Client/src/routes/+page.svelte | 41 +++++++++++++++++------ SharpbinV3.Client/src/routes/layout.css | 6 ++-- 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/SharpbinV3.Client/src/lib/utils/misc.ts b/SharpbinV3.Client/src/lib/utils/misc.ts index caac3ca..4f56eed 100644 --- a/SharpbinV3.Client/src/lib/utils/misc.ts +++ b/SharpbinV3.Client/src/lib/utils/misc.ts @@ -1,10 +1,16 @@ export function formatBytes(bytes: number, decimals = 2): string { - if (bytes === 0) return '0 Bytes'; - const k = 1024; - const dm = decimals < 0 ? 0 : decimals; - const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; - const i = Math.floor(Math.log(bytes) / Math.log(k)); - return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; + if (bytes === 0) return '0 Bytes'; + + const unit = 1024; + const units = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; + + const exponent = Math.floor(Math.log(bytes) / Math.log(unit)); + const value = bytes / Math.pow(unit, exponent); + + const unitLabel = value === 1 && exponent === 0 ? 'Byte' : units[exponent]; + const formattedValue = Number.isInteger(value) ? value : value.toFixed(decimals); + + return `${formattedValue} ${unitLabel}`; } export function formatNumber(num: number): string { diff --git a/SharpbinV3.Client/src/routes/+page.svelte b/SharpbinV3.Client/src/routes/+page.svelte index 3954e35..a3bbb80 100644 --- a/SharpbinV3.Client/src/routes/+page.svelte +++ b/SharpbinV3.Client/src/routes/+page.svelte @@ -5,6 +5,7 @@ import { getToken } from '$lib/utils/auth'; import { fly } from 'svelte/transition'; import { encryptAES } from '$lib/utils/encryption'; + import { formatBytes } from '$lib/utils/misc'; import Dropdown from '$lib/components/Dropdown.svelte'; export let data: PageData; @@ -118,16 +119,36 @@ bind:value={title} maxlength={data.options?.maxTitleLength ?? 500} /> - +
+ +
+ {#if data.options?.maxPasteSize} + {formatBytes(new TextEncoder().encode(content).length)} / {formatBytes( + data.options.maxPasteSize + )} + {:else} + {formatBytes(new TextEncoder().encode(content).length)} + {/if} +
+