UNPKG

besper-frontend-site-dev-0935

Version:

Professional B-esper Frontend Site - Site-wide integration toolkit for full website bot deployment

71 lines (54 loc) 2.21 kB
// Formatting utility functions 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]; } export function formatDate(date: Date | string): string { const d = typeof date === 'string' ? new Date(date) : date; const now = new Date(); const diffTime = Math.abs(now.getTime() - d.getTime()); const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); if (diffDays === 1) return 'Yesterday'; if (diffDays < 7) return `${diffDays} days ago`; if (diffDays < 30) return `${Math.ceil(diffDays / 7)} weeks ago`; if (diffDays < 365) return `${Math.ceil(diffDays / 30)} months ago`; return d.toLocaleDateString(); } export function formatTime(date: Date | string): string { const d = typeof date === 'string' ? new Date(date) : date; return d.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' }); } export function formatNumber(num: number): string { if (num >= 1000000) return (num / 1000000).toFixed(1) + 'M'; if (num >= 1000) return (num / 1000).toFixed(1) + 'K'; return num.toString(); } export function formatDuration(milliseconds: number): string { if (milliseconds < 1000) return `${milliseconds}ms`; const seconds = milliseconds / 1000; if (seconds < 60) return `${seconds.toFixed(1)}s`; const minutes = seconds / 60; if (minutes < 60) return `${minutes.toFixed(1)}m`; const hours = minutes / 60; return `${hours.toFixed(1)}h`; } export function formatPercentage(value: number, total: number): string { if (total === 0) return '0%'; return Math.round((value / total) * 100) + '%'; } export function capitalizeFirstLetter(str: string): string { return str.charAt(0).toUpperCase() + str.slice(1); } export function truncateText(text: string, maxLength: number): string { if (text.length <= maxLength) return text; return text.substring(0, maxLength - 3) + '...'; } export function kebabToTitle(str: string): string { return str .split('-') .map(word => capitalizeFirstLetter(word)) .join(' '); }