import { exponent, toDecimalString } from '@oweme/core'; import { settings } from './settings.svelte.ts'; const cache = new Map(); function formatter(currency: string, opts: { sign?: boolean }): Intl.NumberFormat { 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 units as money in the current locale ("1 234,50 Kฤ"). */ export function money(minor: number, currency: string, opts: { sign?: boolean } = {}): string { return formatter(currency, opts).format(minor / 10 ** exponent(currency)); } /** * The same text as money(), split so the whole number can be emphasised: * `before` (sign, currency), `whole` ("1 234"), `frac` (",50"), `after` (" Kฤ"). */ export function moneyParts(minor: number, currency: string, opts: { sign?: boolean } = {}) { const out = { before: '', whole: '', frac: '', after: '' }; let seenNumber = false; for (const p of formatter(currency, opts).formatToParts(minor / 10 ** exponent(currency))) { if (p.type === 'integer' || p.type === 'group') { out.whole += p.value; seenNumber = true; } else if (p.type === 'decimal' || p.type === 'fraction') out.frac += p.value; else if (seenNumber) out.after += p.value; else out.before += p.value; } return out; } /** 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; } } /** A hue per category, used to tint its round icon background. */ export const CATEGORY_COLORS: Record = { food: '#ff8f6b', groceries: '#3ddc97', transport: '#ffc85a', accommodation: '#7aa7ff', entertainment: '#e879a6', shopping: '#c084fc', utilities: '#facc15', rent: '#4fd1c5', travel: '#60a5fa', gifts: '#f472b6', health: '#f87171', other: '#9ca3af', transfer: '#3ddc97', }; 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'; }