feat: add formatDuration and formatSize utility functions for improved time and size formatting
This commit is contained in:
@@ -19,6 +19,7 @@ import {
|
|||||||
Minimize2,
|
Minimize2,
|
||||||
Minus
|
Minus
|
||||||
} from 'lucide-react'
|
} from 'lucide-react'
|
||||||
|
import { formatDuration } from '@/lib/format-duration'
|
||||||
|
|
||||||
export function MusicPlayer() {
|
export function MusicPlayer() {
|
||||||
const {
|
const {
|
||||||
@@ -179,8 +180,8 @@ export function MusicPlayer() {
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex justify-between text-xs text-muted-foreground">
|
<div className="flex justify-between text-xs text-muted-foreground">
|
||||||
<span>{formatTime(currentTime)}</span>
|
<span>{formatDuration(currentTime)}</span>
|
||||||
<span>{formatTime(duration)}</span>
|
<span>{formatDuration(duration)}</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -270,7 +271,7 @@ export function MusicPlayer() {
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
<span className="text-xs text-muted-foreground">
|
<span className="text-xs text-muted-foreground">
|
||||||
{formatTime(track.duration)}
|
{formatDuration(track.duration)}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
@@ -328,8 +329,8 @@ export function MusicPlayer() {
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex justify-between text-sm text-muted-foreground">
|
<div className="flex justify-between text-sm text-muted-foreground">
|
||||||
<span>{formatTime(currentTime)}</span>
|
<span>{formatDuration(currentTime)}</span>
|
||||||
<span>{formatTime(duration)}</span>
|
<span>{formatDuration(duration)}</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -408,7 +409,7 @@ export function MusicPlayer() {
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
<span className="text-sm text-muted-foreground">
|
<span className="text-sm text-muted-foreground">
|
||||||
{formatTime(track.duration)}
|
{formatDuration(track.duration)}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
))}
|
))}
|
||||||
|
|||||||
18
src/lib/format-duration.ts
Normal file
18
src/lib/format-duration.ts
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
export function formatDuration(ms: number): string {
|
||||||
|
if (isNaN(ms) || ms < 0) return '0:00'
|
||||||
|
|
||||||
|
// Convert milliseconds to seconds
|
||||||
|
const totalSeconds = Math.floor(ms / 1000)
|
||||||
|
|
||||||
|
// Calculate hours, minutes, and remaining seconds
|
||||||
|
const hours = Math.floor(totalSeconds / 3600)
|
||||||
|
const minutes = Math.floor((totalSeconds % 3600) / 60)
|
||||||
|
const seconds = totalSeconds % 60
|
||||||
|
|
||||||
|
// Format based on duration
|
||||||
|
if (hours > 0) {
|
||||||
|
return `${hours}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`
|
||||||
|
} else {
|
||||||
|
return `${minutes}:${seconds.toString().padStart(2, '0')}`
|
||||||
|
}
|
||||||
|
}
|
||||||
32
src/lib/format-size.ts
Normal file
32
src/lib/format-size.ts
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
/**
|
||||||
|
* Units for file sizes
|
||||||
|
*/
|
||||||
|
const FILE_SIZE_UNITS = ['B', 'KB', 'MB', 'GB', 'TB']
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a file size in bytes to a human-readable string
|
||||||
|
* @param bytes File size in bytes
|
||||||
|
* @returns Formatted file size string (e.g., "1.5 MB")
|
||||||
|
*/
|
||||||
|
export function formatSize(bytes: number, binary: boolean = false): string {
|
||||||
|
// Handle invalid input
|
||||||
|
if (bytes === null || bytes === undefined || isNaN(bytes) || bytes < 0) {
|
||||||
|
return `0 B`
|
||||||
|
}
|
||||||
|
|
||||||
|
// If the size is 0, return early
|
||||||
|
if (bytes === 0) {
|
||||||
|
return `0 B`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, determine the appropriate unit based on the size
|
||||||
|
const unit = binary ? 1024 : 1000
|
||||||
|
const unitIndex = Math.floor(Math.log(bytes) / Math.log(unit))
|
||||||
|
const unitSize = Math.pow(unit, unitIndex)
|
||||||
|
const value = bytes / unitSize
|
||||||
|
|
||||||
|
// Make sure we don't exceed our units array
|
||||||
|
const safeUnitIndex = Math.min(unitIndex, FILE_SIZE_UNITS.length - 1)
|
||||||
|
|
||||||
|
return `${value.toFixed(2)} ${FILE_SIZE_UNITS[safeUnitIndex]}`
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user