Files
property-management-network/lib/utils.ts
T

65 lines
1.6 KiB
TypeScript
Raw Normal View History

import { clsx, type ClassValue } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
export function formatCurrency(amount: number, currency = "USD"): string {
return new Intl.NumberFormat("en-US", {
style: "currency",
currency,
minimumFractionDigits: 0,
maximumFractionDigits: 2,
}).format(amount)
}
export function formatDate(date: string | Date): string {
return new Intl.DateTimeFormat("en-US", {
year: "numeric",
month: "short",
day: "numeric",
}).format(new Date(date))
}
export function formatDateShort(date: string | Date): string {
return new Intl.DateTimeFormat("en-US", {
month: "short",
day: "numeric",
}).format(new Date(date))
}
export function daysUntil(date: string | Date): number {
const target = new Date(date)
const today = new Date()
today.setHours(0, 0, 0, 0)
const diff = target.getTime() - today.getTime()
return Math.ceil(diff / (1000 * 60 * 60 * 24))
}
export function daysAgo(date: string | Date): number {
return -daysUntil(date)
}
export function slugify(str: string): string {
return str.toLowerCase().replace(/\s+/g, "-").replace(/[^\w-]/g, "")
}
export function initials(name: string): string {
return name
.split(" ")
.map((n) => n[0])
.join("")
.toUpperCase()
.slice(0, 2)
}
export function truncate(str: string, length = 50): string {
return str.length > length ? str.slice(0, length) + "…" : str
}
export function getOccupancyRate(occupied: number, total: number): number {
if (total === 0) return 0
return Math.round((occupied / total) * 100)
}