|
1 | 1 | /**
|
2 |
| - * Formats numbers to K/M notation. |
| 2 | + * Formats a number with commas and optional decimal places. |
3 | 3 | * @param num - The number to format
|
4 |
| - * @returns The formatted string with K/M suffix |
| 4 | + * @param decimals - Number of decimal places (optional) |
| 5 | + * @returns The formatted string |
5 | 6 | */
|
6 |
| -export const formatNumber = (num: number): string => { |
7 |
| - if (num >= 1000000) return (num / 1000000).toFixed(1) + "M"; |
8 |
| - if (num >= 1000) return (num / 1000).toFixed(1) + "K"; |
9 |
| - return num.toString(); |
| 7 | +const number = (num: number, decimals?: number): string => { |
| 8 | + return new Intl.NumberFormat("en-US", { |
| 9 | + minimumFractionDigits: decimals, |
| 10 | + maximumFractionDigits: decimals, |
| 11 | + }).format(num); |
| 12 | +}; |
| 13 | + |
| 14 | +/** |
| 15 | + * Formats a number as currency with optional currency code and locale. |
| 16 | + * @param num - The number to format |
| 17 | + * @param currency - Currency code (default: "USD") |
| 18 | + * @param locale - Locale string (default: "en-US") |
| 19 | + * @returns The formatted currency string |
| 20 | + */ |
| 21 | +const currency = ( |
| 22 | + num: number, |
| 23 | + currency: string = "USD", |
| 24 | + locale: string = "en-US", |
| 25 | +): string => { |
| 26 | + return new Intl.NumberFormat(locale, { |
| 27 | + style: "currency", |
| 28 | + currency, |
| 29 | + }).format(num); |
| 30 | +}; |
| 31 | + |
| 32 | +/** |
| 33 | + * Formats a date with optional pattern. |
| 34 | + * @param date - The date to format |
| 35 | + * @param pattern - Date pattern (optional) |
| 36 | + * @returns The formatted date string |
| 37 | + */ |
| 38 | +const date = (date: Date, pattern?: string): string => { |
| 39 | + if (pattern) { |
| 40 | + // Simple pattern implementation |
| 41 | + const year = date.getFullYear(); |
| 42 | + const month = String(date.getMonth() + 1).padStart(2, "0"); |
| 43 | + const day = String(date.getDate()).padStart(2, "0"); |
| 44 | + return pattern |
| 45 | + .replace("yyyy", String(year)) |
| 46 | + .replace("MM", month) |
| 47 | + .replace("dd", day); |
| 48 | + } |
| 49 | + return new Intl.DateTimeFormat("en-US", { |
| 50 | + year: "numeric", |
| 51 | + month: "short", |
| 52 | + day: "numeric", |
| 53 | + }).format(date); |
| 54 | +}; |
| 55 | + |
| 56 | +/** |
| 57 | + * Formats a time with optional pattern. |
| 58 | + * @param date - The date to format |
| 59 | + * @param pattern - Time pattern (optional) |
| 60 | + * @returns The formatted time string |
| 61 | + */ |
| 62 | +const time = (date: Date, pattern?: string): string => { |
| 63 | + if (pattern) { |
| 64 | + // Simple pattern implementation |
| 65 | + const hours24 = String(date.getHours()).padStart(2, "0"); |
| 66 | + const minutes = String(date.getMinutes()).padStart(2, "0"); |
| 67 | + return pattern.replace("HH", hours24).replace("mm", minutes); |
| 68 | + } |
| 69 | + return new Intl.DateTimeFormat("en-US", { |
| 70 | + hour: "numeric", |
| 71 | + minute: "numeric", |
| 72 | + hour12: true, |
| 73 | + }).format(date); |
| 74 | +}; |
| 75 | + |
| 76 | +export const format = { |
| 77 | + number, |
| 78 | + currency, |
| 79 | + date, |
| 80 | + time, |
10 | 81 | };
|
0 commit comments