diff options
| author | Mohamed Bassem <me@mbassem.com> | 2025-02-23 22:50:12 +0000 |
|---|---|---|
| committer | Mohamed Bassem <me@mbassem.com> | 2025-02-23 23:16:52 +0000 |
| commit | 14e4fed321634dc014ad2f15cafef3ed0123855e (patch) | |
| tree | 0b7b1f1157bacea13b93161c07ab48561544fd28 /apps/web/lib/utils.ts | |
| parent | e5cb9aa848009ea22c1385e4d33b7edf372979fb (diff) | |
| download | karakeep-14e4fed321634dc014ad2f15cafef3ed0123855e.tar.zst | |
feat: Add a setting page to manage assets. Fixes #730
Diffstat (limited to 'apps/web/lib/utils.ts')
| -rw-r--r-- | apps/web/lib/utils.ts | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/apps/web/lib/utils.ts b/apps/web/lib/utils.ts index 230c9eef..292a6d62 100644 --- a/apps/web/lib/utils.ts +++ b/apps/web/lib/utils.ts @@ -8,6 +8,18 @@ export function cn(...inputs: ClassValue[]) { export type OS = "macos" | "ios" | "windows" | "android" | "linux" | null; +export function formatBytes(bytes: number, decimals = 2) { + 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]; +} + export function getOS() { if (typeof window === "undefined") return; const userAgent = window.navigator.userAgent.toLowerCase(); |
