mirror of
https://github.com/navidrome/navidrome.git
synced 2026-03-04 06:35:52 +00:00
23 lines
685 B
JavaScript
23 lines
685 B
JavaScript
export const formatBytes = (bytes, decimals = 2) => {
|
|
if (bytes === 0) return '0 Bytes'
|
|
|
|
const k = 1024
|
|
const dm = decimals < 0 ? 0 : decimals
|
|
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
|
|
|
|
const i = Math.floor(Math.log(bytes) / Math.log(k))
|
|
|
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]
|
|
}
|
|
|
|
export const formatDuration = (d) => {
|
|
const hours = Math.floor(d / 3600)
|
|
const minutes = Math.floor(d / 60) % 60
|
|
const seconds = d % 60
|
|
return [hours, minutes, seconds]
|
|
.map((v) => Math.round(v).toString())
|
|
.map((v) => (v.length !== 2 ? '0' + v : v))
|
|
.filter((v, i) => v !== '00' || i > 0)
|
|
.join(':')
|
|
}
|