| import { |
| MS_PER_SECOND, |
| SECONDS_PER_MINUTE, |
| SECONDS_PER_HOUR, |
| SHORT_DURATION_THRESHOLD, |
| MEDIUM_DURATION_THRESHOLD |
| } from '$lib/constants'; |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function formatFileSize(bytes: number | unknown): string { |
| if (typeof bytes !== 'number') return 'Unknown'; |
| if (bytes === 0) return '0 Bytes'; |
|
|
| const k = 1024; |
| const sizes = ['Bytes', 'KB', 'MB', 'GB']; |
| const i = Math.floor(Math.log(bytes) / Math.log(k)); |
|
|
| return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export function formatParameters(params: number | unknown): string { |
| if (typeof params !== 'number') return 'Unknown'; |
|
|
| if (params >= 1e9) { |
| return `${(params / 1e9).toFixed(2)}B`; |
| } |
|
|
| if (params >= 1e6) { |
| return `${(params / 1e6).toFixed(2)}M`; |
| } |
|
|
| if (params >= 1e3) { |
| return `${(params / 1e3).toFixed(2)}K`; |
| } |
|
|
| return params.toString(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export function formatNumber(num: number | unknown): string { |
| if (typeof num !== 'number') return 'Unknown'; |
|
|
| return num.toLocaleString(); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function formatJsonPretty(jsonString: string): string { |
| try { |
| const parsed = JSON.parse(jsonString); |
| return JSON.stringify(parsed, null, 2); |
| } catch { |
| return jsonString; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| export function formatTime(date: Date): string { |
| return date.toLocaleTimeString('en-US', { |
| hour12: false, |
| hour: '2-digit', |
| minute: '2-digit', |
| second: '2-digit' |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function formatPerformanceTime(ms: number): string { |
| if (ms < 0) return '0s'; |
|
|
| const totalSeconds = ms / MS_PER_SECOND; |
|
|
| if (totalSeconds < SHORT_DURATION_THRESHOLD) { |
| return `${totalSeconds.toFixed(1)}s`; |
| } |
|
|
| if (totalSeconds < MEDIUM_DURATION_THRESHOLD) { |
| return `${totalSeconds.toFixed(1)}s`; |
| } |
|
|
| const hours = Math.floor(totalSeconds / SECONDS_PER_HOUR); |
| const minutes = Math.floor((totalSeconds % SECONDS_PER_HOUR) / SECONDS_PER_MINUTE); |
| const seconds = Math.floor(totalSeconds % SECONDS_PER_MINUTE); |
|
|
| const parts: string[] = []; |
|
|
| if (hours > 0) { |
| parts.push(`${hours}h`); |
| } |
|
|
| if (minutes > 0) { |
| parts.push(`${minutes}min`); |
| } |
|
|
| if (seconds > 0 || parts.length === 0) { |
| parts.push(`${seconds}s`); |
| } |
|
|
| return parts.join(' '); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export function formatAttachmentText( |
| label: string, |
| name: string, |
| content: string, |
| extra?: string |
| ): string { |
| const header = extra ? `${name} (${extra})` : name; |
| return `\n\n--- ${label}: ${header} ---\n${content}`; |
| } |
|
|