From ccd1c348e6ba07e34937aeb54102da825cab217f Mon Sep 17 00:00:00 2001 From: Azalea Gui Date: Thu, 9 Feb 2023 20:24:00 -0500 Subject: [PATCH] [O] Split util function --- src/App.tsx | 6 +----- src/utils.ts | 10 ++++++++++ 2 files changed, 11 insertions(+), 5 deletions(-) create mode 100644 src/utils.ts diff --git a/src/App.tsx b/src/App.tsx index a3ec488..686afad 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -10,6 +10,7 @@ import 'tippy.js/animations/shift-away.css'; import './app.sass'; import { Icon } from '@iconify-icon/solid'; +import { sizeFmt } from './utils'; interface File { name: string @@ -25,11 +26,6 @@ const host = "https://daisy-ddns.hydev.org/data/api" const path = window.location.pathname const fetchApi = async () => await (await fetch(urlJoin(host, path))).json() as File[] -function sizeFmt(size: number) { - var i = size == 0 ? 0 : Math.floor(Math.log(size) / Math.log(1024)); - return (size / Math.pow(1024, i)).toFixed(1) + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i]; -} - function getIcon(f: File) { if (f.type == "directory") return "/mime/folder.svg" diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..5deab9c --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,10 @@ +/** + * Format bytes to human-readable size + * + * @param size Size in bytes + * @returns Human readable size in string + */ +export function sizeFmt(size: number): string { + var i = size == 0 ? 0 : Math.floor(Math.log(size) / Math.log(1024)); + return (size / Math.pow(1024, i)).toFixed(1) + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i]; +} \ No newline at end of file