Mobile-first PWA: groups via key-in-fragment links, encrypted outbox and sync over SSE, all split modes, currencies with ECB rate prefill, photos, comments, history and rewind, stats, recurring expenses, CSV and backup, EN/CS, dark/light theme, Web Push decrypted in the service worker. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013FYo6JSV1fDU1fm3LU77iF
87 lines
3.4 KiB
TypeScript
87 lines
3.4 KiB
TypeScript
import { exponent, toDecimalString } from '@oweme/core';
|
|
import { settings } from './settings.svelte.ts';
|
|
|
|
const cache = new Map<string, Intl.NumberFormat>();
|
|
|
|
/** 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<string, string> = {
|
|
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';
|
|
}
|