File size: 5,453 Bytes
c09f67c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | import { TZDate } from "@date-fns/tz";
import {
differenceInDays,
differenceInMonths,
format,
startOfDay,
} from "date-fns";
import { normalizeCurrencyCode } from "./currency";
export function formatSize(bytes: number): string {
const units = ["byte", "kilobyte", "megabyte", "gigabyte", "terabyte"];
const unitIndex = Math.max(
0,
Math.min(Math.floor(Math.log(bytes) / Math.log(1024)), units.length - 1),
);
return Intl.NumberFormat("en-US", {
style: "unit",
unit: units[unitIndex],
}).format(+Math.round(bytes / 1024 ** unitIndex));
}
type FormatAmountParams = {
currency: string;
amount: number;
locale?: string | null;
maximumFractionDigits?: number;
minimumFractionDigits?: number;
};
export function formatAmount({
currency,
amount,
locale = "en-US",
minimumFractionDigits,
maximumFractionDigits,
}: FormatAmountParams) {
if (!currency) {
return;
}
// Normalize currency code to ISO 4217 format
const normalizedCurrency = normalizeCurrencyCode(currency);
// Fix: locale can be null, but Intl.NumberFormat expects string | string[] | undefined
// So, if locale is null, pass undefined instead
const safeLocale = locale ?? undefined;
try {
return Intl.NumberFormat(safeLocale, {
style: "currency",
currency: normalizedCurrency,
minimumFractionDigits,
maximumFractionDigits,
}).format(amount);
} catch (error) {
// Fallback to USD if currency is invalid
console.warn(
`Invalid currency code: ${currency} (normalized to ${normalizedCurrency}), falling back to USD`,
error,
);
return Intl.NumberFormat(safeLocale, {
style: "currency",
currency: "USD",
minimumFractionDigits,
maximumFractionDigits,
}).format(amount);
}
}
export function secondsToHoursAndMinutes(seconds: number) {
const hours = Math.floor(seconds / 3600);
const minutes = Math.floor((seconds % 3600) / 60);
if (hours && minutes) {
return `${hours}h ${minutes}m`;
}
if (hours) {
return `${hours}h`;
}
if (minutes) {
return `${minutes}m`;
}
return "0m";
}
type BurnRateData = {
value: number;
date: string;
};
export function calculateAvgBurnRate(data: BurnRateData[] | null) {
if (!data) {
return 0;
}
return data?.reduce((acc, curr) => acc + curr.value, 0) / data?.length;
}
export function formatAccountName({
name = "",
currency,
}: {
name?: string;
currency?: string | null;
}) {
if (currency) {
return `${name} (${currency})`;
}
return name;
}
export function formatDateRange(dates: TZDate[]): string {
if (!dates.length) return "";
const formatFullDate = (date: TZDate) => format(date, "MMM d");
const formatDay = (date: TZDate) => format(date, "d");
const startDate = dates[0];
const endDate = dates[1];
if (!startDate) return "";
if (
dates.length === 1 ||
!endDate ||
startDate.getTime() === endDate.getTime()
) {
return formatFullDate(startDate);
}
if (startDate.getMonth() === endDate.getMonth()) {
// Same month
return `${format(startDate, "MMM")} ${formatDay(startDate)} - ${formatDay(endDate)}`;
}
// Different months
return `${formatFullDate(startDate)} - ${formatFullDate(endDate)}`;
}
export function getDueDateStatus(dueDate: string): string {
// Parse due date as UTC (it's stored as UTC midnight)
const due = new TZDate(dueDate, "UTC");
// Get current date in UTC for consistent comparison
const now = new Date();
const nowUTC = new TZDate(now.toISOString(), "UTC");
// Compare at the day level in UTC
const nowDay = startOfDay(nowUTC);
const dueDay = startOfDay(due);
const diffDays = differenceInDays(dueDay, nowDay);
const diffMonths = differenceInMonths(dueDay, nowDay);
if (diffDays === 0) return "Today";
if (diffDays === 1) return "Tomorrow";
if (diffDays === -1) return "Yesterday";
if (diffDays > 0) {
if (diffMonths < 1) return `in ${diffDays} days`;
return `in ${diffMonths} month${diffMonths === 1 ? "" : "s"}`;
}
if (diffMonths < 1)
return `${Math.abs(diffDays)} day${Math.abs(diffDays) === 1 ? "" : "s"} ago`;
return `${diffMonths} month${diffMonths === 1 ? "" : "s"} ago`;
}
export function formatRelativeTime(date: Date): string {
const now = new Date();
const diffInSeconds = Math.floor((now.getTime() - date.getTime()) / 1000);
if (diffInSeconds < 60) {
return "just now";
}
const intervals = [
{ label: "y", seconds: 31536000 },
{ label: "mo", seconds: 2592000 },
{ label: "d", seconds: 86400 },
{ label: "h", seconds: 3600 },
{ label: "m", seconds: 60 },
] as const;
for (const interval of intervals) {
const count = Math.floor(diffInSeconds / interval.seconds);
if (count > 0) {
return `${count}${interval.label} ago`;
}
}
return "just now";
}
export function formatCompactAmount(
amount: number,
locale?: string | null,
): string {
const absAmount = Math.abs(amount);
const safeLocale = locale ?? "en-US";
if (absAmount >= 1000000) {
const formatted = (absAmount / 1000000).toLocaleString(safeLocale, {
minimumFractionDigits: 1,
maximumFractionDigits: 1,
});
return `${formatted}m`;
}
// Always show in thousands notation
const formatted = (absAmount / 1000).toLocaleString(safeLocale, {
minimumFractionDigits: 1,
maximumFractionDigits: 1,
});
return `${formatted}k`;
}
|