[O] Split util function

This commit is contained in:
Azalea Gui
2023-02-09 20:24:00 -05:00
parent e95f338d13
commit ccd1c348e6
2 changed files with 11 additions and 5 deletions
+1 -5
View File
@@ -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"
+10
View File
@@ -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];
}