mirror of
https://github.com/lordmathis/lemma.git
synced 2025-11-06 07:54:22 +00:00
Migrate utils to ts
This commit is contained in:
24
app/src/utils/formatBytes.ts
Normal file
24
app/src/utils/formatBytes.ts
Normal 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]}`;
|
||||
};
|
||||
Reference in New Issue
Block a user