flowstate / src /lib /utils.ts
muthuk1's picture
Upload project config and lib files
820c67c verified
raw
history blame
1.04 kB
import { clsx, type ClassValue } from 'clsx'
import { twMerge } from 'tailwind-merge'
export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)) }
export function formatNumber(n: number): string {
if (n >= 1_000_000_000) return `${(n / 1_000_000_000).toFixed(2)}B`
if (n >= 1_000_000) return `${(n / 1_000_000).toFixed(2)}M`
if (n >= 1_000) return `${(n / 1_000).toFixed(1)}K`
return n.toFixed(0)
}
export function formatCurrency(n: number): string {
return new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', maximumFractionDigits: 0 }).format(n)
}
export function shortenAddress(addr: string): string {
if (addr.length < 10) return addr
return `${addr.slice(0, 4)}...${addr.slice(-4)}`
}
export function getTimeAgo(date: Date): string {
const s = Math.floor((Date.now() - date.getTime()) / 1000)
if (s < 60) return `${s}s ago`
if (s < 3600) return `${Math.floor(s / 60)}m ago`
if (s < 86400) return `${Math.floor(s / 3600)}h ago`
return `${Math.floor(s / 86400)}d ago`
}