mirror of
https://github.com/maziggy/bambuddy.git
synced 2026-10-22 03:49:18 +02:00
Consolidate duplicate utility functions into shared modules
Extract repeated helper functions from components into shared utils (colors.ts, date.ts, file.ts) to reduce duplication: - isLightColor, parseFilamentColor, hexToColorName → utils/colors.ts - formatMediaTime, formatDurationFromHours → utils/date.ts - formatFileSize guard hardened for NaN/negative → utils/file.ts - formatBytes (ToastContext), formatTime (Timelapse*) deduplicated Functions with subtly different behavior are intentionally kept local (formatFilament, formatStorageSize, formatBytes, formatUptime, formatDateTime, formatDate, formatTimeAgo) to preserve exact UI output.
This commit is contained in:
@@ -418,3 +418,28 @@ export function formatRelativeTime(
|
||||
// Older than 7 days
|
||||
return formatDateTime(dateStr, timeFormat);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format seconds as MM:SS for media/video player display.
|
||||
*
|
||||
* @param seconds - Total seconds
|
||||
* @returns Formatted string (e.g., "2:05", "0:30")
|
||||
*/
|
||||
export function formatMediaTime(seconds: number): string {
|
||||
const mins = Math.floor(seconds / 60);
|
||||
const secs = Math.floor(seconds % 60);
|
||||
return `${mins}:${secs.toString().padStart(2, '0')}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a duration given in hours to a human-readable string.
|
||||
*
|
||||
* @param hours - Duration in hours (e.g., 2.5)
|
||||
* @returns Formatted string (e.g., "2h 30m", "45m", "3h")
|
||||
*/
|
||||
export function formatDurationFromHours(hours: number): string {
|
||||
if (hours < 1) return `${Math.round(hours * 60)}m`;
|
||||
const h = Math.floor(hours);
|
||||
const m = Math.round((hours - h) * 60);
|
||||
return m > 0 ? `${h}h ${m}m` : `${h}h`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user