add web
This commit is contained in:
24
web/src/lib/utils.ts
Normal file
24
web/src/lib/utils.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { clsx, type ClassValue } from 'clsx';
|
||||
import { twMerge } from 'tailwind-merge';
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs));
|
||||
}
|
||||
|
||||
export function formatBytes(
|
||||
bytes: number,
|
||||
opts: {
|
||||
decimals?: number;
|
||||
sizeType?: 'accurate' | 'normal';
|
||||
} = {},
|
||||
) {
|
||||
const { decimals = 0, sizeType = 'normal' } = opts;
|
||||
|
||||
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
|
||||
const accurateSizes = ['Bytes', 'KiB', 'MiB', 'GiB', 'TiB'];
|
||||
if (bytes === 0) return '0 Byte';
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(1024));
|
||||
return `${(bytes / Math.pow(1024, i)).toFixed(decimals)} ${
|
||||
sizeType === 'accurate' ? accurateSizes[i] ?? 'Bytes' : sizes[i] ?? 'Bytes'
|
||||
}`;
|
||||
}
|
||||
Reference in New Issue
Block a user