Download src/utils/formatting.ts from Jessedbrown/daisy-client: direct link, hf CLI and curl.
- Browser
- Download file 1.65 kB
-
https://huggingface.co/Jessedbrown/daisy-client/resolve/main/src/utils/formatting.ts
- Command line
-
hf download hf://Jessedbrown/daisy-client/src/utils/formatting.ts
-
curl -L -o formatting.ts https://huggingface.co/Jessedbrown/daisy-client/resolve/main/src/utils/formatting.ts
1.65 kB
| // Format file size for display | |
| export function formatFileSize(bytes: number): string { | |
| 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]}`; | |
| } | |
| // Format currency for display | |
| export function formatCurrency(amount: number, currency = 'USD'): string { | |
| return new Intl.NumberFormat('en-US', { | |
| style: 'currency', | |
| currency, | |
| }).format(amount); | |
| } | |
| // Truncate text with ellipsis | |
| export function truncateText(text: string, maxLength: number): string { | |
| if (text.length <= maxLength) return text; | |
| return `${text.slice(0, maxLength).trim()}...`; | |
| } | |
| // Format date for display | |
| export function formatDate(date: string | Date): string { | |
| const dateObj = typeof date === 'string' ? new Date(date) : date; | |
| return new Intl.DateTimeFormat('en-US', { | |
| year: 'numeric', | |
| month: 'short', | |
| day: 'numeric', | |
| }).format(dateObj); | |
| } | |
| // Format relative time (e.g., "2 hours ago") | |
| export function formatRelativeTime(date: string | Date): string { | |
| const dateObj = typeof date === 'string' ? new Date(date) : date; | |
| const now = new Date(); | |
| const diffInSeconds = Math.floor((now.getTime() - dateObj.getTime()) / 1000); | |
| if (diffInSeconds < 60) return 'just now'; | |
| if (diffInSeconds < 3600) | |
| return `${Math.floor(diffInSeconds / 60)} minutes ago`; | |
| if (diffInSeconds < 86400) | |
| return `${Math.floor(diffInSeconds / 3600)} hours ago`; | |
| if (diffInSeconds < 2592000) | |
| return `${Math.floor(diffInSeconds / 86400)} days ago`; | |
| return formatDate(dateObj); | |
| } | |