zero-debt 0.0.0-stage → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +249 -3
- package/lib/claude.js +148 -0
- package/package.json +18 -6
- package/public/css/zero.css +341 -0
- package/public/fonts/OFL-hanken-grotesk.txt +93 -0
- package/public/fonts/OFL-jetbrains-mono.txt +93 -0
- package/public/fonts/hanken-grotesk.woff2 +0 -0
- package/public/fonts/jetbrains-mono.woff2 +0 -0
- package/public/index.html +245 -0
- package/public/js/analysis.js +62 -0
- package/public/js/api.js +88 -0
- package/public/js/categorize.js +69 -0
- package/public/js/charts.js +173 -0
- package/public/js/demo.js +84 -0
- package/public/js/format.js +38 -0
- package/public/js/importers.js +251 -0
- package/public/js/insights.js +137 -0
- package/public/js/keys.js +50 -0
- package/public/js/main.js +946 -0
- package/public/js/palette.js +67 -0
- package/public/js/payoff.js +143 -0
- package/public/js/progress.js +91 -0
- package/public/js/settings.js +112 -0
- package/public/js/state.js +130 -0
- package/public/og.png +0 -0
- package/server.js +314 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// demo.js — made-up sample numbers, shown until you import your own.
|
|
2
|
+
// Safe to publish: no real accounts or people.
|
|
3
|
+
|
|
4
|
+
import { makeBaseline, plannedAt } from './progress.js';
|
|
5
|
+
|
|
6
|
+
const tx = [
|
|
7
|
+
['2026-11-20', 'Checking', 'ACME COFFEE PAYROLL', 742.18],
|
|
8
|
+
['2026-11-06', 'Checking', 'ACME COFFEE PAYROLL', 701.55],
|
|
9
|
+
['2026-10-23', 'Checking', 'ACME COFFEE PAYROLL', 768.90],
|
|
10
|
+
['2026-10-09', 'Checking', 'ACME COFFEE PAYROLL', 690.12],
|
|
11
|
+
['2026-11-15', 'Checking', 'NORTHLINE CARD PAYMENT', -300.00],
|
|
12
|
+
['2026-10-15', 'Checking', 'NORTHLINE CARD PAYMENT', -300.00],
|
|
13
|
+
['2026-11-03', 'Checking', 'RENT — SHARED HOUSE', -450.00],
|
|
14
|
+
['2026-10-03', 'Checking', 'RENT — SHARED HOUSE', -450.00],
|
|
15
|
+
['2026-11-12', 'Checking', 'DEPT EDUCATION STUDENT LN', -45.00],
|
|
16
|
+
['2026-10-12', 'Checking', 'DEPT EDUCATION STUDENT LN', -45.00],
|
|
17
|
+
['2026-11-19', 'Northline card', 'SAFEWAY 1123', -38.42],
|
|
18
|
+
['2026-11-17', 'Northline card', 'SHELL OIL 5521', -41.10],
|
|
19
|
+
['2026-11-16', 'Northline card', 'TACO BELL 0094', -9.87],
|
|
20
|
+
['2026-11-14', 'Northline card', 'STEAM PURCHASE', -24.99],
|
|
21
|
+
['2026-11-12', 'Northline card', 'SPOTIFY USA', -11.99],
|
|
22
|
+
['2026-11-11', 'Northline card', 'CHIPOTLE 2210', -13.45],
|
|
23
|
+
['2026-11-09', 'Northline card', 'TARGET 0441', -52.30],
|
|
24
|
+
['2026-11-08', 'Northline card', 'STARBUCKS 8812', -6.85],
|
|
25
|
+
['2026-11-06', 'Northline card', 'NETFLIX.COM', -15.49],
|
|
26
|
+
['2026-11-05', 'Northline card', 'SHELL OIL 5521', -38.75],
|
|
27
|
+
['2026-11-03', 'Northline card', 'AMAZON MKTPL', -64.20],
|
|
28
|
+
['2026-11-02', 'Northline card', 'TACO BELL 0094', -8.12],
|
|
29
|
+
['2026-11-01', 'Northline card', 'INTEREST CHARGE', -28.40],
|
|
30
|
+
['2026-10-28', 'Northline card', 'SAFEWAY 1123', -44.18],
|
|
31
|
+
['2026-10-25', 'Northline card', 'XBOX GAME PASS', -16.99],
|
|
32
|
+
['2026-10-22', 'Northline card', 'CHIPOTLE 2210', -12.95],
|
|
33
|
+
['2026-10-19', 'Northline card', 'SHELL OIL 5521', -36.20],
|
|
34
|
+
['2026-10-14', 'Northline card', 'ULTA BEAUTY', -48.60],
|
|
35
|
+
['2026-10-12', 'Northline card', 'SPOTIFY USA', -11.99],
|
|
36
|
+
['2026-10-10', 'Northline card', 'TACO BELL 0094', -11.02],
|
|
37
|
+
['2026-10-06', 'Northline card', 'NETFLIX.COM', -15.49],
|
|
38
|
+
['2026-10-04', 'Northline card', 'TARGET 0441', -71.88],
|
|
39
|
+
['2026-10-01', 'Northline card', 'INTEREST CHARGE', -30.12],
|
|
40
|
+
];
|
|
41
|
+
|
|
42
|
+
const CATS = {
|
|
43
|
+
'ACME COFFEE PAYROLL': 'income', 'NORTHLINE CARD PAYMENT': 'card payment', 'RENT — SHARED HOUSE': 'bills',
|
|
44
|
+
'DEPT EDUCATION STUDENT LN': 'loan payment', 'SAFEWAY 1123': 'groceries', 'TARGET 0441': 'groceries',
|
|
45
|
+
'SHELL OIL 5521': 'gas & convenience', 'TACO BELL 0094': 'fast food & dining', 'CHIPOTLE 2210': 'fast food & dining',
|
|
46
|
+
'STARBUCKS 8812': 'fast food & dining', 'STEAM PURCHASE': 'gaming', 'XBOX GAME PASS': 'gaming',
|
|
47
|
+
'SPOTIFY USA': 'subscriptions', 'NETFLIX.COM': 'subscriptions', 'AMAZON MKTPL': 'shopping',
|
|
48
|
+
'ULTA BEAUTY': 'personal care & style', 'INTEREST CHARGE': 'interest & fees',
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export const DEMO = {
|
|
52
|
+
version: 1,
|
|
53
|
+
sample: true,
|
|
54
|
+
settings: {
|
|
55
|
+
takeHome: 1560, netHourly: 15.4, fixed: 495, living: 620, studentLiving: 390,
|
|
56
|
+
studentMode: false, extraHours: 0, extraMonthly: 0,
|
|
57
|
+
strategy: 'avalanche', order: [], lump: { amount: 0, month: '' },
|
|
58
|
+
},
|
|
59
|
+
debts: [
|
|
60
|
+
{ id: 'd1', name: 'Northline card', balance: 1284.5, apr: 27.49, min: 35, promoApr: '', promoEnd: '' },
|
|
61
|
+
{ id: 'd2', name: 'Harbor card', balance: 1890, apr: 24.99, promoApr: 0, promoEnd: '2027-06-15', min: 38 },
|
|
62
|
+
{ id: 'd3', name: 'Phone financing', balance: 420, apr: 0, min: 35, promoApr: '', promoEnd: '' },
|
|
63
|
+
],
|
|
64
|
+
transactions: tx.map(([date, account, description, amount], i) => ({
|
|
65
|
+
id: `demo${i}`, date, account, description, amount, category: CATS[description] || 'other',
|
|
66
|
+
})),
|
|
67
|
+
learned: {},
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
// the sample plan was set on oct 1 with higher balances; two check-ins since then
|
|
71
|
+
const startDebts = [
|
|
72
|
+
{ id: 'd1', balance: 1690, apr: 27.49, min: 35 },
|
|
73
|
+
{ id: 'd2', balance: 1890, apr: 24.99, promoApr: 0, promoEnd: '2027-06-15', min: 38 },
|
|
74
|
+
{ id: 'd3', balance: 510, apr: 0, min: 35 },
|
|
75
|
+
];
|
|
76
|
+
const baseline = makeBaseline(startDebts, { budget: 445, strategy: 'avalanche', order: [], lump: { amount: 0, month: '' }, start: '2026-10' }, '2026-10-01');
|
|
77
|
+
const octPlanned = plannedAt(baseline, '2026-10-31');
|
|
78
|
+
DEMO.progress = {
|
|
79
|
+
baseline,
|
|
80
|
+
checkins: [
|
|
81
|
+
{ date: '2026-10-31', total: Math.round(octPlanned + 12), balances: {} },
|
|
82
|
+
{ date: '2026-11-28', total: 3594.5, balances: { d1: 1284.5, d2: 1890, d3: 420 } },
|
|
83
|
+
],
|
|
84
|
+
};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// format.js — how numbers and dates read on screen. configure() is called whenever
|
|
2
|
+
// settings change, so locale, currency and date style follow settings.json.
|
|
3
|
+
|
|
4
|
+
const MONTHS = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december'];
|
|
5
|
+
let cfg = { locale: 'en-US', currency: 'USD', dates: 'iso' };
|
|
6
|
+
let whole, cents;
|
|
7
|
+
|
|
8
|
+
export function configure({ locale = 'en-US', currency = 'USD', dates = 'iso' } = {}) {
|
|
9
|
+
cfg = { locale, currency, dates };
|
|
10
|
+
whole = new Intl.NumberFormat(locale, { style: 'currency', currency, maximumFractionDigits: 0 });
|
|
11
|
+
cents = new Intl.NumberFormat(locale, { style: 'currency', currency, minimumFractionDigits: 2, maximumFractionDigits: 2 });
|
|
12
|
+
}
|
|
13
|
+
configure();
|
|
14
|
+
|
|
15
|
+
/** $1,234 by default; $1,234.56 with cents. */
|
|
16
|
+
export const money = (n, withCents = false) => (withCents ? cents : whole).format(Number(n) || 0);
|
|
17
|
+
|
|
18
|
+
/** '2027-03' → '2027-03' (iso) or 'march 2027' (words). short → '2027-03' / 'mar ’27'. */
|
|
19
|
+
export function monthName(ym, short = false) {
|
|
20
|
+
if (!ym) return '';
|
|
21
|
+
if (cfg.dates === 'iso') return ym;
|
|
22
|
+
const [y, m] = ym.split('-').map(Number);
|
|
23
|
+
return short ? `${MONTHS[m - 1].slice(0, 3)} ’${String(y).slice(2)}` : `${MONTHS[m - 1]} ${y}`;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const plural = (n, word, many = `${word}s`) => `${n} ${n === 1 ? word : many}`;
|
|
27
|
+
|
|
28
|
+
/** Months between two 'YYYY-MM' strings (b − a). */
|
|
29
|
+
export function monthsBetween(a, b) {
|
|
30
|
+
const [ay, am] = a.split('-').map(Number);
|
|
31
|
+
const [by, bm] = b.split('-').map(Number);
|
|
32
|
+
return (by - ay) * 12 + (bm - am);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Escapes text for safe use inside innerHTML templates. Use it on anything from imports. */
|
|
36
|
+
export function esc(s) {
|
|
37
|
+
return String(s ?? '').replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]));
|
|
38
|
+
}
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
// importers.js — turns bank / card exports into transactions.
|
|
2
|
+
//
|
|
3
|
+
// Every importer returns rows shaped like:
|
|
4
|
+
// { date: 'YYYY-MM-DD', description, amount, account }
|
|
5
|
+
// amount is signed from YOUR point of view: + money in, − money out.
|
|
6
|
+
//
|
|
7
|
+
// CSV presets are matched on the header row. To support a new bank, add a preset:
|
|
8
|
+
// list the header columns it must have, and a `row` function that maps one parsed
|
|
9
|
+
// line to the shape above. Anything unrecognized falls back to the generic mapper,
|
|
10
|
+
// which the import screen lets you adjust by hand.
|
|
11
|
+
|
|
12
|
+
/** Comma, semicolon (European Excel) or tab: whichever the header line uses most. */
|
|
13
|
+
export function detectDelimiter(text) {
|
|
14
|
+
const first = String(text).replace(/^\uFEFF/, '').split(/\r?\n/, 1)[0] || '';
|
|
15
|
+
const count = (ch) => first.split(ch).length - 1;
|
|
16
|
+
return [',', ';', '\t'].sort((a, b) => count(b) - count(a))[0];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** RFC 4180 CSV parser: quoted fields, escaped quotes, delimiters and newlines inside quotes. */
|
|
20
|
+
export function parseCSV(text, delimiter = detectDelimiter(text)) {
|
|
21
|
+
const rows = [];
|
|
22
|
+
let row = [], field = '', quoted = false;
|
|
23
|
+
const s = String(text).replace(/^\uFEFF/, ''); // strip a byte-order mark
|
|
24
|
+
for (let i = 0; i < s.length; i++) {
|
|
25
|
+
const c = s[i];
|
|
26
|
+
if (quoted) {
|
|
27
|
+
if (c === '"' && s[i + 1] === '"') { field += '"'; i++; }
|
|
28
|
+
else if (c === '"') quoted = false;
|
|
29
|
+
else field += c;
|
|
30
|
+
} else if (c === '"') quoted = true;
|
|
31
|
+
else if (c === delimiter) { row.push(field); field = ''; }
|
|
32
|
+
else if (c === '\n' || c === '\r') {
|
|
33
|
+
if (c === '\r' && s[i + 1] === '\n') i++;
|
|
34
|
+
row.push(field); field = '';
|
|
35
|
+
if (row.some((f) => f.trim() !== '')) rows.push(row);
|
|
36
|
+
row = [];
|
|
37
|
+
} else field += c;
|
|
38
|
+
}
|
|
39
|
+
row.push(field);
|
|
40
|
+
if (row.some((f) => f.trim() !== '')) rows.push(row);
|
|
41
|
+
return rows;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const pad = (n) => String(n).padStart(2, '0');
|
|
45
|
+
function valid(y, m, d) {
|
|
46
|
+
const date = new Date(Date.UTC(y, m - 1, d));
|
|
47
|
+
return date.getUTCFullYear() === y && date.getUTCMonth() === m - 1 && date.getUTCDate() === d;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Accepts 2026-11-02, 11/02/2026, 11/2/26, and day-first 02/11/2026 or 02.11.2026 when
|
|
52
|
+
* dayFirst is set. Returns 'YYYY-MM-DD', or null for anything that isn't a real date.
|
|
53
|
+
*/
|
|
54
|
+
export function parseDate(value, dayFirst = false) {
|
|
55
|
+
const v = String(value || '').trim();
|
|
56
|
+
let m = v.match(/^(\d{4})-(\d{1,2})-(\d{1,2})/);
|
|
57
|
+
if (m) {
|
|
58
|
+
const [y, mo, d] = [Number(m[1]), Number(m[2]), Number(m[3])];
|
|
59
|
+
return valid(y, mo, d) ? `${y}-${pad(mo)}-${pad(d)}` : null;
|
|
60
|
+
}
|
|
61
|
+
m = v.match(/^(\d{1,2})[/.-](\d{1,2})[/.-](\d{2}|\d{4})$/);
|
|
62
|
+
if (!m) return null;
|
|
63
|
+
const y = m[3].length === 2 ? 2000 + Number(m[3]) : Number(m[3]);
|
|
64
|
+
const [mo, d] = dayFirst ? [Number(m[2]), Number(m[1])] : [Number(m[1]), Number(m[2])];
|
|
65
|
+
return valid(y, mo, d) ? `${y}-${pad(mo)}-${pad(d)}` : null;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** True when a column of a/b/yyyy dates can only be day-first (some first number is over 12). */
|
|
69
|
+
export function looksDayFirst(values) {
|
|
70
|
+
return values.some((v) => {
|
|
71
|
+
const m = String(v ?? '').trim().match(/^(\d{1,2})[/.-]\d{1,2}[/.-]\d{2,4}$/);
|
|
72
|
+
return m && Number(m[1]) > 12;
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* "$1,234.56", "(12.00)", "-5", "$-5.00", "5.00-", and European "1.234,56" → number.
|
|
78
|
+
* Returns NaN for blanks.
|
|
79
|
+
*/
|
|
80
|
+
export function parseAmount(value) {
|
|
81
|
+
let v = String(value ?? '').trim();
|
|
82
|
+
if (!v) return NaN;
|
|
83
|
+
const negative = /^\(.*\)$/.test(v) || v.includes('-');
|
|
84
|
+
v = v.replace(/[^0-9.,]/g, '');
|
|
85
|
+
if (/,\d{1,2}$/.test(v)) v = v.replace(/\./g, '').replace(',', '.'); // 1.234,56 → 1234.56
|
|
86
|
+
else v = v.replace(/,/g, '');
|
|
87
|
+
const n = Number(v);
|
|
88
|
+
if (!v || !Number.isFinite(n)) return NaN;
|
|
89
|
+
return negative ? -n : n;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const has = (header, cols) => cols.every((c) => header.includes(c.toLowerCase()));
|
|
93
|
+
|
|
94
|
+
export const PRESETS = [
|
|
95
|
+
{
|
|
96
|
+
id: 'capitalone-checking', label: 'Capital One checking', account: 'Capital One checking',
|
|
97
|
+
columns: ['Transaction Date', 'Transaction Description', 'Transaction Type', 'Transaction Amount'],
|
|
98
|
+
row: (r) => ({
|
|
99
|
+
date: r['transaction date'], description: r['transaction description'],
|
|
100
|
+
amount: Math.abs(parseAmount(r['transaction amount'])) * (/debit/i.test(r['transaction type']) ? -1 : 1),
|
|
101
|
+
}),
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
id: 'capitalone-card', label: 'Capital One card', account: 'Capital One card',
|
|
105
|
+
columns: ['Transaction Date', 'Description', 'Debit', 'Credit'],
|
|
106
|
+
row: (r) => ({
|
|
107
|
+
date: r['transaction date'], description: r.description,
|
|
108
|
+
amount: (parseAmount(r.credit) || 0) - (parseAmount(r.debit) || 0),
|
|
109
|
+
}),
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
id: 'apple-card', label: 'Apple Card', account: 'Apple Card',
|
|
113
|
+
columns: ['Transaction Date', 'Description', 'Amount (USD)'],
|
|
114
|
+
// Apple Card exports purchases as positive numbers, payments as negative
|
|
115
|
+
row: (r) => ({ date: r['transaction date'], description: r.merchant || r.description, amount: -parseAmount(r['amount (usd)']) }),
|
|
116
|
+
},
|
|
117
|
+
{
|
|
118
|
+
id: 'chase-card', label: 'Chase card', account: 'Chase card',
|
|
119
|
+
columns: ['Transaction Date', 'Post Date', 'Description', 'Amount'],
|
|
120
|
+
// Chase card exports purchases as negative already
|
|
121
|
+
row: (r) => ({ date: r['transaction date'], description: r.description, amount: parseAmount(r.amount) }),
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
id: 'chase-checking', label: 'Chase checking', account: 'Chase checking',
|
|
125
|
+
columns: ['Posting Date', 'Description', 'Amount'],
|
|
126
|
+
row: (r) => ({ date: r['posting date'], description: r.description, amount: parseAmount(r.amount) }),
|
|
127
|
+
},
|
|
128
|
+
];
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Best-guess column picks for an unknown CSV, from header names first and the first
|
|
132
|
+
* data row second. The import screen shows these as editable selects.
|
|
133
|
+
*/
|
|
134
|
+
export function guessMapping(header, sample = []) {
|
|
135
|
+
const find = (re) => header.findIndex((h) => re.test(h));
|
|
136
|
+
const byValue = (test) => sample.findIndex((v) => test(String(v || '').trim()));
|
|
137
|
+
const map = {
|
|
138
|
+
date: find(/date|posted|when/i),
|
|
139
|
+
description: find(/desc|merchant|payee|name|memo|details/i),
|
|
140
|
+
amount: find(/amount|value|total/i),
|
|
141
|
+
debit: find(/debit|withdraw/i),
|
|
142
|
+
credit: find(/credit|deposit/i),
|
|
143
|
+
};
|
|
144
|
+
const isDate = (v) => parseDate(v) !== null || parseDate(v, true) !== null;
|
|
145
|
+
if (map.date < 0) map.date = byValue(isDate);
|
|
146
|
+
if (map.amount < 0 && map.debit < 0) map.amount = byValue((v) => !isDate(v) && /^[-+(]?\D{0,3}[\d.,]*\d[.,]\d{2}\)?-?$/.test(v));
|
|
147
|
+
if (map.description < 0) map.description = byValue((v) => /[a-z]{3,}/i.test(v) && !isDate(v));
|
|
148
|
+
return map;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Parse a CSV export.
|
|
153
|
+
* @returns {{ preset, header, rows, mapping, errors }}
|
|
154
|
+
* preset is null when the generic mapper was used.
|
|
155
|
+
*/
|
|
156
|
+
export function importCSV(text, { mapping, flip = false } = {}) {
|
|
157
|
+
const table = parseCSV(text);
|
|
158
|
+
if (table.length < 2) return { preset: null, header: [], rows: [], mapping: null, errors: ['that file has no rows'] };
|
|
159
|
+
const header = table[0].map((h) => h.trim());
|
|
160
|
+
const lower = header.map((h) => h.toLowerCase());
|
|
161
|
+
const preset = mapping ? null : PRESETS.find((p) => has(lower, p.columns)) || null;
|
|
162
|
+
const map = mapping || guessMapping(header, table[1]);
|
|
163
|
+
const rows = [];
|
|
164
|
+
const errors = [];
|
|
165
|
+
const dateCol = preset ? lower.indexOf(preset.columns[0].toLowerCase()) : map.date;
|
|
166
|
+
const dayFirst = dateCol >= 0 && looksDayFirst(table.slice(1).map((r) => r[dateCol]));
|
|
167
|
+
|
|
168
|
+
table.slice(1).forEach((cells, i) => {
|
|
169
|
+
const r = Object.fromEntries(lower.map((h, j) => [h, (cells[j] ?? '').trim()]));
|
|
170
|
+
let out;
|
|
171
|
+
if (preset) out = preset.row(r);
|
|
172
|
+
else {
|
|
173
|
+
const amount = map.amount >= 0
|
|
174
|
+
? parseAmount(cells[map.amount])
|
|
175
|
+
: (parseAmount(cells[map.credit]) || 0) - (parseAmount(cells[map.debit]) || 0);
|
|
176
|
+
out = { date: cells[map.date], description: cells[map.description], amount };
|
|
177
|
+
}
|
|
178
|
+
const date = parseDate(out.date, dayFirst);
|
|
179
|
+
if (!date || !Number.isFinite(out.amount)) { errors.push(`line ${i + 2} skipped`); return; }
|
|
180
|
+
rows.push({ date, description: String(out.description || '').trim() || '(no description)', amount: flip ? -out.amount : out.amount });
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
return { preset, header, rows, mapping: preset ? null : map, errors, dayFirst };
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** Stable id for de-duplicating imports: same account, day, amount and description = same transaction. */
|
|
187
|
+
export function txId(t) {
|
|
188
|
+
const s = `${t.account}|${t.date}|${Number(t.amount).toFixed(2)}|${String(t.description).toLowerCase()}`;
|
|
189
|
+
let h = 5381;
|
|
190
|
+
for (let i = 0; i < s.length; i++) h = ((h << 5) + h + s.charCodeAt(i)) >>> 0;
|
|
191
|
+
return `t${h.toString(36)}`;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Reads a File as base64 (no data: prefix), for statement uploads. */
|
|
195
|
+
export function fileToBase64(file) {
|
|
196
|
+
return new Promise((resolve, reject) => {
|
|
197
|
+
const reader = new FileReader();
|
|
198
|
+
reader.onload = () => resolve(String(reader.result).split(',')[1]);
|
|
199
|
+
reader.onerror = () => reject(reader.error);
|
|
200
|
+
reader.readAsDataURL(file);
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/* ---------- OFX / QFX (Quicken, Money, most US banks' "download for Quicken") ---------- */
|
|
205
|
+
|
|
206
|
+
/** True for OFX 1.x (SGML) and OFX 2.x (XML) files, including QFX. */
|
|
207
|
+
export function isOFX(text) {
|
|
208
|
+
return /OFXHEADER|<OFX>/i.test(String(text).slice(0, 4000));
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const decode = (s) => s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'|'/g, "'");
|
|
212
|
+
|
|
213
|
+
/** Value of the first <NAME>value in a block. Works with and without closing tags. */
|
|
214
|
+
function ofxTag(block, name) {
|
|
215
|
+
const m = block.match(new RegExp(`<${name}>([^<\\r\\n]*)`, 'i'));
|
|
216
|
+
return m ? decode(m[1].trim()) : '';
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Parses an OFX/QFX statement.
|
|
221
|
+
* @returns {{ account, isCard, balance, rows: [{ date, description, amount, fitid }], errors }}
|
|
222
|
+
* amount is already signed from the account holder's point of view (OFX does that).
|
|
223
|
+
* balance is the statement's ledger balance; on a card, a negative balance means you owe it.
|
|
224
|
+
*/
|
|
225
|
+
export function parseOFX(text) {
|
|
226
|
+
const body = String(text);
|
|
227
|
+
const isCard = /<CCSTMTRS>/i.test(body);
|
|
228
|
+
const org = ofxTag(body, 'ORG');
|
|
229
|
+
const acct = ofxTag(body, 'ACCTID');
|
|
230
|
+
const account = [org || (isCard ? 'card' : 'bank'), acct ? `…${acct.slice(-4)}` : ''].filter(Boolean).join(' ');
|
|
231
|
+
const ledger = body.match(/<LEDGERBAL>([\s\S]*?)(<\/LEDGERBAL>|<AVAILBAL>|<\/STMTRS>|<\/CCSTMTRS>)/i);
|
|
232
|
+
const balance = ledger ? parseAmount(ofxTag(ledger[1], 'BALAMT')) : NaN;
|
|
233
|
+
|
|
234
|
+
const rows = [], errors = [];
|
|
235
|
+
const blocks = body.split(/<STMTTRN>/i).slice(1).map((b) => b.split(/<\/STMTTRN>|<\/BANKTRANLIST>/i)[0]);
|
|
236
|
+
blocks.forEach((b, i) => {
|
|
237
|
+
const d = ofxTag(b, 'DTPOSTED').match(/^(\d{4})(\d{2})(\d{2})/);
|
|
238
|
+
const date = d ? parseDate(`${d[1]}-${d[2]}-${d[3]}`) : null;
|
|
239
|
+
const amount = parseAmount(ofxTag(b, 'TRNAMT'));
|
|
240
|
+
const name = ofxTag(b, 'NAME'), memo = ofxTag(b, 'MEMO');
|
|
241
|
+
const description = (name && memo && !memo.startsWith(name) ? `${name} ${memo}` : name || memo || '(no description)').slice(0, 120);
|
|
242
|
+
if (!date || !Number.isFinite(amount)) { errors.push(`transaction ${i + 1} skipped`); return; }
|
|
243
|
+
rows.push({ date, description, amount, fitid: ofxTag(b, 'FITID') || undefined });
|
|
244
|
+
});
|
|
245
|
+
return { account, isCard, balance, rows, errors };
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** Stable id from a bank's own transaction id (FITID), so re-downloads never duplicate. */
|
|
249
|
+
export function fitId(account, fitid) {
|
|
250
|
+
return txId({ account, date: 'fitid', amount: 0, description: fitid });
|
|
251
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
// insights.js — plain-language notes worked out on this device, no AI needed.
|
|
2
|
+
// Each note is { tone: 'warn' | 'good' | 'info', text }. Keep them short and specific:
|
|
3
|
+
// a number, what it means, what to do.
|
|
4
|
+
|
|
5
|
+
import { simulate, minimumsOnly, rateFor, nextMonth } from './payoff.js';
|
|
6
|
+
import { since, byCategory, topMerchants, chargesOn } from './analysis.js';
|
|
7
|
+
import { monthlyBudget } from './state.js';
|
|
8
|
+
import { progressOf } from './progress.js';
|
|
9
|
+
import { money, monthName, plural, monthsBetween } from './format.js';
|
|
10
|
+
|
|
11
|
+
const CUTTABLE = ['fast food & dining', 'gaming', 'subscriptions', 'shopping', 'personal care & style'];
|
|
12
|
+
|
|
13
|
+
/** Smallest monthly budget that clears `debt` before its promo ends, by binary search. */
|
|
14
|
+
function budgetToBeatPromo(debts, plan, debt) {
|
|
15
|
+
const promoYm = debt.promoEnd.slice(0, 7);
|
|
16
|
+
const clears = (budget) => {
|
|
17
|
+
const r = simulate(debts, { ...plan, budget });
|
|
18
|
+
const before = r.months.filter((m) => m.ym < promoYm).pop();
|
|
19
|
+
return before && before.balances[debt.id] <= 0.005;
|
|
20
|
+
};
|
|
21
|
+
let lo = plan.budget, hi = plan.budget + 20000;
|
|
22
|
+
if (!clears(hi)) return null;
|
|
23
|
+
for (let i = 0; i < 30; i++) { const mid = (lo + hi) / 2; if (clears(mid)) hi = mid; else lo = mid; }
|
|
24
|
+
return Math.ceil(hi);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function localInsights(state) {
|
|
28
|
+
const { settings, debts, transactions } = state;
|
|
29
|
+
const notes = [];
|
|
30
|
+
const open = debts.filter((d) => Number(d.balance) > 0);
|
|
31
|
+
if (!open.length) return [{ tone: 'good', text: 'no debts entered. add one in the ledger to see a plan.' }];
|
|
32
|
+
|
|
33
|
+
const start = nextMonth();
|
|
34
|
+
const plan = { budget: monthlyBudget(settings), strategy: settings.strategy, order: settings.order, lump: settings.lump, start };
|
|
35
|
+
const result = simulate(open, plan);
|
|
36
|
+
|
|
37
|
+
if (result.shortfall) {
|
|
38
|
+
const mins = open.reduce((s, d) => s + Number(d.min || 0), 0);
|
|
39
|
+
notes.push({ tone: 'warn', text: `this budget (${money(plan.budget)}/mo) doesn’t cover the minimums (${money(mins)}/mo). missed minimums mean late fees and penalty rates — find the gap first.` });
|
|
40
|
+
}
|
|
41
|
+
if (result.stalled) {
|
|
42
|
+
notes.push({ tone: 'warn', text: 'at this budget the interest grows faster than the payments, so this never reaches $0.' });
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// interest leaking right now
|
|
46
|
+
const nowInterest = open.reduce((s, d) => s + Number(d.balance) * rateFor(d, start) / 1200, 0);
|
|
47
|
+
if (nowInterest > 1) {
|
|
48
|
+
const worst = [...open].sort((a, b) => rateFor(b, start) - rateFor(a, start))[0];
|
|
49
|
+
notes.push({ tone: 'warn', text: `about ${money(nowInterest)} a month is going to interest right now, mostly on ${worst.name} (${rateFor(worst, start)}%). that’s the first debt to kill.` });
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// promo deadlines
|
|
53
|
+
for (const d of open.filter((x) => x.promoEnd && x.promoEnd.slice(0, 7) > start.slice(0, 7))) {
|
|
54
|
+
const promoYm = d.promoEnd.slice(0, 7);
|
|
55
|
+
const before = result.months.filter((m) => m.ym < promoYm).pop();
|
|
56
|
+
const left = before ? before.balances[d.id] : Number(d.balance);
|
|
57
|
+
if (left > 0.005) {
|
|
58
|
+
const need = budgetToBeatPromo(open, plan, d);
|
|
59
|
+
notes.push({ tone: 'warn', text: `${d.name} will still owe ${money(left)} when its ${d.promoApr || 0}% rate ends (${monthName(promoYm)}), then it jumps to ${d.apr}%.` +
|
|
60
|
+
(need ? ` putting ${money(need)}/mo toward debt (${money(need - plan.budget)} more) clears it in time.` : '') });
|
|
61
|
+
} else {
|
|
62
|
+
notes.push({ tone: 'good', text: `${d.name} is paid off before its ${d.promoApr || 0}% rate ends in ${monthName(promoYm)} — it never charges you interest.` });
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// charging on a card that carries interest
|
|
67
|
+
const recent = since(transactions, 30);
|
|
68
|
+
for (const d of open.filter((x) => rateFor(x, start) > 0 && !x.payInFull)) {
|
|
69
|
+
const spent = chargesOn(recent, d.name).reduce((s, t) => s - t.amount, 0);
|
|
70
|
+
if (spent > 20) {
|
|
71
|
+
notes.push({ tone: 'warn', text: `${money(spent)} went on ${d.name} in the last 30 days while it carries a ${rateFor(d, start)}% balance. new charges there start costing interest right away — move them to a card you pay in full or debit.` });
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// compared with minimums only
|
|
76
|
+
const base = minimumsOnly(open, start);
|
|
77
|
+
if (result.debtFree && base.stalled) {
|
|
78
|
+
const mins = open.reduce((s, d) => s + Number(d.min || 0), 0);
|
|
79
|
+
notes.push({ tone: 'good', text: `paying only the minimums (${money(mins)}/mo) would never clear this — once the promo rates end, interest outruns them. this plan gets there.` });
|
|
80
|
+
}
|
|
81
|
+
if (result.debtFree) {
|
|
82
|
+
const saved = (base.totalInterest || 0) - result.totalInterest;
|
|
83
|
+
const months = base.debtFree ? monthsBetween(result.debtFree, base.debtFree) : null;
|
|
84
|
+
if (saved > 1 && !base.stalled) {
|
|
85
|
+
notes.push({ tone: 'good', text: `versus paying minimums only, this plan saves ${money(saved)} in interest` + (months ? ` and finishes ${plural(months, 'month')} sooner.` : '.') });
|
|
86
|
+
}
|
|
87
|
+
const faster = simulate(open, { ...plan, budget: plan.budget + 100 });
|
|
88
|
+
const gain = monthsBetween(faster.debtFree, result.debtFree);
|
|
89
|
+
if (gain > 0) notes.push({ tone: 'info', text: `every extra $100 a month moves debt-free from ${monthName(result.debtFree)} to ${monthName(faster.debtFree)}.` });
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// student mode
|
|
93
|
+
if (!settings.studentMode && settings.living > settings.studentLiving) {
|
|
94
|
+
const sm = simulate(open, { ...plan, budget: monthlyBudget({ ...settings, studentMode: true }) });
|
|
95
|
+
if (sm.debtFree) notes.push({ tone: 'info', text: `student mode frees up ${money(settings.living - settings.studentLiving)} a month and gets you to $0 by ${monthName(sm.debtFree)}.` });
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// biggest cuttable spending in the last 30 days
|
|
99
|
+
const cats = byCategory(recent).filter((c) => CUTTABLE.includes(c.category));
|
|
100
|
+
if (cats[0]) {
|
|
101
|
+
const c = cats[0];
|
|
102
|
+
notes.push({ tone: 'info', text: `${c.category}: ${money(c.total)} across ${plural(c.count, 'purchase')} in the last 30 days. it’s the easiest place to find money.` });
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const subs = topMerchants(since(transactions, 45), 50).filter((m) => m.category === 'subscriptions');
|
|
106
|
+
if (subs.length >= 3) {
|
|
107
|
+
const total = subs.reduce((s, m) => s + m.total / Math.max(1, m.count), 0);
|
|
108
|
+
notes.push({ tone: 'info', text: `${plural(subs.length, 'subscription')} cost about ${money(total)} a month. keep the ones that earn you money; pause the rest until you’re at $0.` });
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return notes;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* The summary sent to Claude. Totals and merchant names only — no account numbers,
|
|
116
|
+
* no full transaction list.
|
|
117
|
+
*/
|
|
118
|
+
export function summaryForClaude(state) {
|
|
119
|
+
const { settings, debts, transactions } = state;
|
|
120
|
+
const start = nextMonth();
|
|
121
|
+
const open = debts.filter((d) => Number(d.balance) > 0);
|
|
122
|
+
const plan = { budget: monthlyBudget(settings), strategy: settings.strategy, order: settings.order, lump: settings.lump, start };
|
|
123
|
+
const result = simulate(open, plan);
|
|
124
|
+
const recent = since(transactions, 30);
|
|
125
|
+
return {
|
|
126
|
+
today: new Date().toISOString().slice(0, 10),
|
|
127
|
+
income: { takeHomeMonthly: settings.takeHome, takeHomePerHour: settings.netHourly, fixedBillsMonthly: settings.fixed },
|
|
128
|
+
spending: { everydayMonthly: settings.living, studentModeMonthly: settings.studentLiving, studentModeOn: settings.studentMode },
|
|
129
|
+
plan: { strategy: settings.strategy, monthlyToDebt: plan.budget, extraHoursPerWeek: settings.extraHours, debtFree: result.debtFree, totalInterest: result.totalInterest, stalled: result.stalled, shortfall: result.shortfall },
|
|
130
|
+
progress: (() => { const p = progressOf(state); return p ? { status: p.status, vsPlan: p.diff, checkIns: p.points.length, planDebtFree: state.progress.baseline.debtFree } : null; })(),
|
|
131
|
+
debts: open.map((d) => ({ name: d.name, balance: Number(d.balance), apr: Number(d.apr), promoApr: d.promoApr === '' ? null : d.promoApr, promoEnd: d.promoEnd || null, minimum: Number(d.min), paidInFullMonthly: Boolean(d.payInFull), projectedPayoff: result.paidOff[d.id] || null })),
|
|
132
|
+
last30Days: {
|
|
133
|
+
byCategory: byCategory(recent).map((c) => ({ category: c.category, total: Math.round(c.total), purchases: c.count })),
|
|
134
|
+
topMerchants: topMerchants(recent, 15).map((m) => ({ merchant: m.name.slice(0, 40), total: Math.round(m.total), visits: m.count, category: m.category })),
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// keys.js — keyboard shortcuts. A binding is one key ("i", "/", "?") or one chord
|
|
2
|
+
// ("mod+k", "alt+s"). Sequences like "g d" aren't supported, on purpose: one chord or
|
|
3
|
+
// one key keeps it predictable. "mod" is ⌘ on macOS and ctrl everywhere else.
|
|
4
|
+
|
|
5
|
+
const IS_MAC = /mac|iphone|ipad/i.test(navigator.platform || navigator.userAgent);
|
|
6
|
+
|
|
7
|
+
export function parse(binding) {
|
|
8
|
+
const parts = String(binding).toLowerCase().split('+').map((p) => p.trim()).filter(Boolean);
|
|
9
|
+
const key = parts.pop() || '';
|
|
10
|
+
return { key, mod: parts.includes('mod'), ctrl: parts.includes('ctrl'), alt: parts.includes('alt'), shift: parts.includes('shift') };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function matches(binding, e) {
|
|
14
|
+
const b = parse(binding);
|
|
15
|
+
const modDown = IS_MAC ? e.metaKey : e.ctrlKey;
|
|
16
|
+
if (b.mod !== modDown) return false;
|
|
17
|
+
if (!b.mod && (e.metaKey || e.ctrlKey) !== b.ctrl) return false;
|
|
18
|
+
if (b.alt !== e.altKey) return false;
|
|
19
|
+
// shift is implied by characters like ? and >, so only check it when asked for
|
|
20
|
+
if (b.shift && !e.shiftKey) return false;
|
|
21
|
+
return e.key.toLowerCase() === b.key;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Human label: "mod+k" → "⌘K" on mac, "Ctrl+K" elsewhere. */
|
|
25
|
+
export function label(binding) {
|
|
26
|
+
const b = parse(binding);
|
|
27
|
+
const mods = [b.mod && (IS_MAC ? '⌘' : 'Ctrl+'), b.ctrl && 'Ctrl+', b.alt && (IS_MAC ? '⌥' : 'Alt+'), b.shift && (IS_MAC ? '⇧' : 'Shift+')].filter(Boolean).join('');
|
|
28
|
+
return mods + (b.key.length === 1 ? b.key.toUpperCase() : b.key);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const typing = (el) => el && (el.isContentEditable || /^(input|textarea|select)$/i.test(el.tagName));
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Listens for shortcuts. `bindings` is { name: 'mod+k' }, `actions` is { name: fn }.
|
|
35
|
+
* Plain keys are ignored while you type in a field; chords with mod always work.
|
|
36
|
+
*/
|
|
37
|
+
export function listen(getBindings, actions, extra = () => false) {
|
|
38
|
+
document.addEventListener('keydown', (e) => {
|
|
39
|
+
if (e.isComposing) return;
|
|
40
|
+
if (extra(e)) return;
|
|
41
|
+
for (const [name, binding] of Object.entries(getBindings())) {
|
|
42
|
+
if (!actions[name] || !matches(binding, e)) continue;
|
|
43
|
+
if (!parse(binding).mod && typing(document.activeElement)) return;
|
|
44
|
+
if (document.querySelector('dialog[open]') && name !== 'palette') return;
|
|
45
|
+
e.preventDefault();
|
|
46
|
+
actions[name]();
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|