32 lines
1.1 KiB
JavaScript
32 lines
1.1 KiB
JavaScript
|
|
import { formatCentsUSD } from '@/lib/money';
|
||
|
|
|
||
|
|
// Display + matching helpers for bank transactions in the bill modal. Shared by
|
||
|
|
// the linked-transaction list, the unmatch dialogs, and the bulk-unmatch payee
|
||
|
|
// grouping.
|
||
|
|
|
||
|
|
export function fmtTransactionAmount(amount, currency = 'USD') {
|
||
|
|
return formatCentsUSD(amount, { signed: true, currency });
|
||
|
|
}
|
||
|
|
|
||
|
|
export function transactionDate(tx) {
|
||
|
|
return tx?.posted_date || String(tx?.transacted_at || '').slice(0, 10) || null;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function transactionTitle(tx) {
|
||
|
|
return tx?.payee || tx?.description || tx?.memo || 'Transaction';
|
||
|
|
}
|
||
|
|
|
||
|
|
export function normalizePayee(s) {
|
||
|
|
return (s || '').toLowerCase().replace(/[^a-z0-9]/g, '');
|
||
|
|
}
|
||
|
|
|
||
|
|
// Two payees are "similar" when one normalized name is a prefix of the other
|
||
|
|
// (min 3 chars) — the grouping used to find related matches to unmatch together.
|
||
|
|
export function isSimilarPayee(a, b) {
|
||
|
|
const na = normalizePayee(a);
|
||
|
|
const nb = normalizePayee(b);
|
||
|
|
const minLen = Math.min(na.length, nb.length);
|
||
|
|
if (minLen < 3) return false;
|
||
|
|
return na.startsWith(nb) || nb.startsWith(na);
|
||
|
|
}
|