/** * Utility Functions Example * * This file contains utility functions that demonstrate different * change scenarios in the diff viewer. */ /** * Format a number as currency */ export function formatCurrency(amount: number, currency: string = 'USD'): string { return new Intl.NumberFormat('en-US', { style: 'currency', currency: currency, }).format(amount); } /** * Truncate text with ellipsis */ export function truncate(text: string, maxLength: number): string { if (text.length <= maxLength) { return text; } return text.slice(0, maxLength - 3) + '...'; } /** * Deep clone an object */ export function deepClone(obj: T): T { return JSON.parse(JSON.stringify(obj)); } /** * Debounce a function */ export function debounce any>( fn: T, delay: number ): (...args: Parameters) => void { let timeoutId: ReturnType; return function (...args: Parameters) { clearTimeout(timeoutId); timeoutId = setTimeout(() => fn(...args), delay); }; } /** * Generate a random ID */ export function generateId(length: number = 8): string { const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; let result = ''; for (let i = 0; i < length; i++) { result += chars.charAt(Math.floor(Math.random() * chars.length)); } return result; } /** * Parse a date string safely */ export function parseDate(dateString: string): Date | null { const date = new Date(dateString); if (isNaN(date.getTime())) { return null; } return date; }