37 lines
1.1 KiB
TypeScript
37 lines
1.1 KiB
TypeScript
import { clsx, type ClassValue } from "clsx"
|
|
import { twMerge } from "tailwind-merge"
|
|
import { format as formatDateFns } from "date-fns"
|
|
|
|
export function cn(...inputs: ClassValue[]) {
|
|
return twMerge(clsx(inputs))
|
|
}
|
|
|
|
export function formatCurrency(amount: number, currency: string = 'SEK'): string {
|
|
return new Intl.NumberFormat('sv-SE', {
|
|
style: 'currency',
|
|
currency,
|
|
minimumFractionDigits: 0,
|
|
maximumFractionDigits: 2,
|
|
}).format(amount)
|
|
}
|
|
|
|
export function formatDate(date: Date | string): string {
|
|
const d = typeof date === 'string' ? new Date(date) : date
|
|
return formatDateFns(d, 'yyyy-MM-dd')
|
|
}
|
|
|
|
export function formatOrgNumber(orgNumber: string): string {
|
|
// Format Swedish org number: XXXXXX-XXXX
|
|
const cleaned = orgNumber.replace(/\D/g, '')
|
|
if (cleaned.length === 10) {
|
|
return `${cleaned.slice(0, 6)}-${cleaned.slice(6)}`
|
|
}
|
|
return orgNumber
|
|
}
|
|
|
|
export function generateInvoiceNumber(): string {
|
|
const year = new Date().getFullYear()
|
|
const random = Math.floor(Math.random() * 10000).toString().padStart(4, '0')
|
|
return `${year}-${random}`
|
|
}
|