mirror of
https://github.com/maziggy/bambuddy.git
synced 2026-10-21 10:15:04 +02:00
Refactors date and file formatting utilities
refactor(utils): consolidate date and file size formatting Extracts formatting logic into dedicated utility functions to improve code maintainability and consistency across components. Changes: - Consolidate date formatting functions (formatRelativeTime, etc.) - Consolidate file size formatting into single formatFileSize utility - Fix file size calculation errors - Update UI components to use new utilities - Change "ASAP" to "Waiting" for unscheduled queue items Tests: - Add comprehensive unit tests for date utilities - Add unit tests for file size formatting
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
|
||||
/**
|
||||
* Formats a byte count into a human-readable string (e.g. `1.5 MB`).
|
||||
*
|
||||
* @param bytes - The number of bytes to format.
|
||||
* @returns A formatted string with the appropriate unit (B, KB, MB, GB, or TB).
|
||||
*/
|
||||
export function formatFileSize(bytes: number): string {
|
||||
if (bytes === 0) return '0 B';
|
||||
|
||||
const units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
||||
const k = 1024;
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||
|
||||
const size = bytes / Math.pow(k, i);
|
||||
|
||||
// No decimals for bytes, 1 decimal for larger units
|
||||
return i === 0
|
||||
? `${size} ${units[i]}`
|
||||
: `${size.toFixed(1)} ${units[i]}`;
|
||||
}
|
||||
Reference in New Issue
Block a user