simple-react-ui-kits
Version:
A lightweight, customizable React UI component library built with TypeScript and Tailwind CSS
41 lines (38 loc) • 1.07 kB
text/typescript
/**
* Format a number with commas
* @param num - Number to format
* @returns Formatted number string
*/
export function formatNumber(num: number): string {
return num.toLocaleString();
}
/**
* Format a date to a readable string
* @param date - Date to format
* @returns Formatted date string
*/
export function formatDate(date: Date): string {
return date.toLocaleDateString();
}
/**
* Format currency
* @param amount - Amount to format
* @param currency - Currency code (default: USD)
* @returns Formatted currency string
*/
export function formatCurrency(amount: number, currency = 'USD'): string {
return new Intl.NumberFormat('en-US', {
style: 'currency',
currency,
}).format(amount);
}
/**
* Truncate text to a specified length
* @param text - Text to truncate
* @param maxLength - Maximum length
* @returns Truncated text
*/
export function truncateText(text: string, maxLength: number): string {
if (text.length <= maxLength) return text;
return text.slice(0, maxLength) + '...';
}