Migrate utils to ts

This commit is contained in:
2025-04-04 19:23:31 +02:00
parent 49ecaac720
commit e4fb276cf7
12 changed files with 287 additions and 111 deletions

View File

@@ -0,0 +1,24 @@
/**
* Units for file size display.
*/
type ByteUnit = 'B' | 'KB' | 'MB' | 'GB';
/**
* An array of size units in ascending order.
*/
const UNITS: readonly ByteUnit[] = ['B', 'KB', 'MB', 'GB'] as const;
/**
* Formats a number of bytes into a human-readable string.
* @param bytes - The number of bytes to format.
* @returns A string representing the formatted file size.
*/
export const formatBytes = (bytes: number): string => {
let size: number = bytes;
let unitIndex: number = 0;
while (size >= 1024 && unitIndex < UNITS.length - 1) {
size /= 1024;
unitIndex++;
}
return `${size.toFixed(1)} ${UNITS[unitIndex]}`;
};