Files
frontend/app/utils/format.ts
2026-08-21 15:29:01 +02:00

51 lines
1.7 KiB
TypeScript

const UNITS = ['B', 'KiB', 'MiB', 'GiB', 'TiB']
export function formatBytes(bytes: number): string {
let value = bytes
let unitIndex = 0
while (value >= 1024 && unitIndex < UNITS.length - 1) {
value /= 1024
unitIndex += 1
}
return `${value.toFixed(unitIndex === 0 ? 0 : 1)} ${UNITS[unitIndex]}`
}
export function formatTime(iso: string): string {
return new Date(iso).toLocaleTimeString('de-DE', { hour: '2-digit', minute: '2-digit', second: '2-digit' })
}
export function formatDate(iso: string): string {
return new Date(iso).toLocaleDateString('de-DE', { day: '2-digit', month: '2-digit', year: 'numeric' })
}
export function isToday(iso: string): boolean {
const date = new Date(iso)
const now = new Date()
return date.getFullYear() === now.getFullYear() && date.getMonth() === now.getMonth() && date.getDate() === now.getDate()
}
export function formatDateTime(iso: string): string {
return new Date(iso).toLocaleString('de-DE', { dateStyle: 'medium', timeStyle: 'medium' })
}
export function partCount(etag: string): number {
const dashIndex = etag.indexOf('-')
if (dashIndex === -1) {
return 1
}
const parsed = Number(etag.slice(dashIndex + 1))
return Number.isFinite(parsed) && parsed > 0 ? parsed : 1
}
export function shortEtag(etag: string): string {
const dashIndex = etag.indexOf('-')
const hash = dashIndex === -1 ? etag : etag.slice(0, dashIndex)
const suffix = dashIndex === -1 ? '' : etag.slice(dashIndex)
return `${hash.slice(0, 8)}…${hash.slice(-4)}${suffix}`
}
export function baseName(key: string): string {
const slashIndex = key.lastIndexOf('/')
return slashIndex === -1 ? key : key.slice(slashIndex + 1)
}