import clsx, { ClassValue } from "clsx"; /** * Utility function to format numbers with locale-specific formatting */ export function formatNumber( value: number, options?: Intl.NumberFormatOptions, locale?: string ): string { return new Intl.NumberFormat(locale || 'en-US', options).format(value); } /** * Utility function to format dates with locale-specific formatting */ export function formatDate( date: Date | string | number, options?: Intl.DateTimeFormatOptions, locale?: string ): string { const dateObj = typeof date === 'string' || typeof date === 'number' ? new Date(date) : date; return new Intl.DateTimeFormat(locale || 'en-US', options).format(dateObj); } /** * Utility function to format currency values */ export function formatCurrency( value: number, currency: string = 'USD', locale?: string ): string { return formatNumber(value, { style: 'currency', currency, }, locale); } /** * Utility function to format percentages */ export function formatPercentage( value: number, decimals: number = 2, locale?: string ): string { return formatNumber(value / 100, { style: 'percent', minimumFractionDigits: decimals, maximumFractionDigits: decimals, }, locale); } /** * Utility function to truncate text with ellipsis */ export function truncateText(text: string, maxLength: number): string { if (text.length <= maxLength) return text; return text.slice(0, maxLength).trim() + '...'; } /** * Utility function to safely get nested object values */ export function getNestedValue(obj: any, path: string): any { return path.split('.').reduce((current, key) => current?.[key], obj); } /** * Utility function to create a debounced function */ export function debounce any>( func: T, wait: number ): (...args: Parameters) => void { let timeout: NodeJS.Timeout; return function executedFunction(...args: Parameters) { const later = () => { clearTimeout(timeout); func(...args); }; clearTimeout(timeout); timeout = setTimeout(later, wait); }; } /** * Utility function to check if a value is empty */ export function isEmpty(value: any): boolean { if (value == null) return true; if (typeof value === 'string') return value.trim().length === 0; if (Array.isArray(value)) return value.length === 0; if (typeof value === 'object') return Object.keys(value).length === 0; return false; } /** * Utility function to generate unique IDs */ export function generateId(prefix: string = 'id'): string { return `${prefix}-${Math.random().toString(36).substr(2, 9)}`; } /** * Utility function for type-safe object keys */ export function getObjectKeys(obj: T): (keyof T)[] { return Object.keys(obj) as (keyof T)[]; } /** * Capitalize first letter of a string */ export function capitalize(str: string): string { return str.charAt(0).toUpperCase() + str.slice(1); } /** * Convert camelCase to Title Case */ export function camelToTitle(str: string): string { return str .replace(/([A-Z])/g, ' $1') .replace(/^./, (str) => str.toUpperCase()) .trim(); } /** * Deep clone an object */ export function deepClone(obj: T): T { if (obj === null || typeof obj !== 'object') return obj; if (obj instanceof Date) return new Date(obj.getTime()) as any; if (obj instanceof Array) return obj.map(item => deepClone(item)) as any; if (typeof obj === 'object') { const clonedObj: any = {}; for (const key in obj) { if (obj.hasOwnProperty(key)) { clonedObj[key] = deepClone(obj[key]); } } return clonedObj; } return obj; }