import { exponent, toDecimalString } from '@oweme/core'; import { settings } from './settings.svelte.ts'; const cache = new Map(); /** Format minor units as money in the current locale ("1 234,50 Kฤ"). */ export function money(minor: number, currency: string, opts: { sign?: boolean } = {}): string { const key = `${settings.locale}|${currency}|${opts.sign ? 1 : 0}`; let f = cache.get(key); if (!f) { const e = exponent(currency); try { f = new Intl.NumberFormat(settings.locale, { style: 'currency', currency, minimumFractionDigits: e, maximumFractionDigits: e, signDisplay: opts.sign ? 'exceptZero' : 'auto', }); } catch { f = new Intl.NumberFormat(settings.locale, { minimumFractionDigits: e, maximumFractionDigits: e }); } cache.set(key, f); } return f.format(minor / 10 ** exponent(currency)); } /** Editable amount text for an input, in the user's decimal style. */ export function amountInput(minor: number, currency: string): string { const s = toDecimalString(minor, currency); return settings.lang === 'cs' ? s.replace('.', ',') : s; } export function fmtDate(d: string, style: 'short' | 'long' = 'short'): string { const date = new Date(`${d}T12:00:00`); return date.toLocaleDateString(settings.locale, style === 'long' ? { weekday: 'short', day: 'numeric', month: 'long', year: 'numeric' } : { day: 'numeric', month: 'short', year: date.getFullYear() === new Date().getFullYear() ? undefined : 'numeric' }); } export function fmtDateTime(iso: string): string { return new Date(iso).toLocaleString(settings.locale, { day: 'numeric', month: 'short', hour: '2-digit', minute: '2-digit' }); } export function fmtMonth(ym: string): string { return new Date(`${ym}-15T12:00:00`).toLocaleDateString(settings.locale, { month: 'short', year: '2-digit' }); } /** Today as YYYY-MM-DD in local time. */ export function today(): string { const d = new Date(); return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`; } export function addDays(d: string, n: number): string { const x = new Date(`${d}T12:00:00`); x.setDate(x.getDate() + n); return `${x.getFullYear()}-${String(x.getMonth() + 1).padStart(2, '0')}-${String(x.getDate()).padStart(2, '0')}`; } export function currencyName(code: string): string { try { return new Intl.DisplayNames(settings.locale, { type: 'currency' }).of(code) ?? code; } catch { return code; } } export const CATEGORY_ICONS: Record = { food: '๐Ÿฝ๏ธ', groceries: '๐Ÿ›’', transport: '๐Ÿš•', accommodation: '๐Ÿจ', entertainment: '๐ŸŽŸ๏ธ', shopping: '๐Ÿ›๏ธ', utilities: '๐Ÿ’ก', rent: '๐Ÿ ', travel: 'โœˆ๏ธ', gifts: '๐ŸŽ', health: '๐Ÿ’Š', other: '๐Ÿงพ', transfer: '๐Ÿ’ธ', }; export const MEMBER_COLORS = ['#3DDC97', '#F2A65A', '#6CB4EE', '#E879A6', '#C9A227', '#9B8CF2', '#4FD1C5', '#F87171', '#A3E635', '#FB923C']; export function download(filename: string, content: BlobPart, type: string) { const url = URL.createObjectURL(new Blob([content], { type })); const a = document.createElement('a'); a.href = url; a.download = filename; document.body.append(a); a.click(); a.remove(); setTimeout(() => URL.revokeObjectURL(url), 10_000); } export function slug(s: string): string { return s.normalize('NFKD').replace(/[ฬ€-อฏ]/g, '').replace(/[^A-Za-z0-9]+/g, '-').replace(/^-|-$/g, '').toLowerCase() || 'group'; }