(rawRows, vendorsMap)
| 15 | * user and re-running normalize with an updated map). |
| 16 | */ |
| 17 | export function normalize(rawRows, vendorsMap) { |
| 18 | const canonical = []; |
| 19 | const unknownSet = new Set(); |
| 20 | const stats = new Map(); |
| 21 | for (const r of rawRows) { |
| 22 | const key = r.counterparty; |
| 23 | const entry = vendorsMap[key]; |
| 24 | if (!entry) { |
| 25 | unknownSet.add(key); |
| 26 | const s = stats.get(key) ?? { count: 0, sumEur: 0 }; |
| 27 | s.count += 1; |
| 28 | s.sumEur += r.amount_eur ?? 0; |
| 29 | stats.set(key, s); |
| 30 | continue; |
| 31 | } |
| 32 | canonical.push({ |
| 33 | date: r.date, |
| 34 | month: r.date.slice(0, 7), |
| 35 | vendor: entry.canonical, |
| 36 | category: entry.category, |
| 37 | amount: r.amount_eur, |
| 38 | }); |
| 39 | } |
| 40 | const unknownStats = [...stats.entries()] |
| 41 | .map(([counterparty, s]) => ({ |
| 42 | counterparty, |
| 43 | count: s.count, |
| 44 | sumEur: Number(s.sumEur.toFixed(2)), |
| 45 | })) |
| 46 | .sort((a, b) => Math.abs(b.sumEur) - Math.abs(a.sumEur)); |
| 47 | return { canonical, unknown: [...unknownSet], unknownStats }; |
| 48 | } |
no test coverage detected