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,245 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
+
<meta name="color-scheme" content="light dark">
|
|
7
|
+
<meta name="theme-color" content="#eceee9">
|
|
8
|
+
<title>zero</title>
|
|
9
|
+
<meta name="description" content="a local, private debt payoff planner. import statements, try strategies, see the month you hit $0.">
|
|
10
|
+
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data: blob:; font-src 'self'; connect-src 'self'; object-src 'none'; base-uri 'none'; form-action 'none'">
|
|
11
|
+
<meta property="og:title" content="zero — a local, private debt payoff planner">
|
|
12
|
+
<meta property="og:description" content="import statements, try payoff strategies, and see the month you hit $0. runs on your own computer. open source.">
|
|
13
|
+
<meta property="og:image" content="https://aposoloam.github.io/zero/og.png">
|
|
14
|
+
<meta property="og:url" content="https://aposoloam.github.io/zero/">
|
|
15
|
+
<meta name="twitter:card" content="summary_large_image">
|
|
16
|
+
<link rel="preload" href="fonts/jetbrains-mono.woff2" as="font" type="font/woff2" crossorigin>
|
|
17
|
+
<link rel="stylesheet" href="css/zero.css">
|
|
18
|
+
<link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Ccircle cx='16' cy='16' r='10' fill='none' stroke='%231c2433' stroke-width='4'/%3E%3Cpath d='M6 26 L26 6' stroke='%23b8362a' stroke-width='4' stroke-linecap='round'/%3E%3C/svg%3E">
|
|
19
|
+
<script type="module" src="js/main.js"></script>
|
|
20
|
+
</head>
|
|
21
|
+
<body data-repo="https://github.com/aposoloam/zero">
|
|
22
|
+
<a class="skip" href="#panes">skip to content</a>
|
|
23
|
+
|
|
24
|
+
<header class="bar top">
|
|
25
|
+
<span class="brand" aria-label="zero">zer<span class="slash">o</span></span>
|
|
26
|
+
<span class="where" id="where"></span>
|
|
27
|
+
<nav class="top-actions" aria-label="main">
|
|
28
|
+
<button type="button" data-cmd="palette">commands <kbd data-key="palette"></kbd></button>
|
|
29
|
+
<button type="button" data-cmd="import">import <kbd data-key="import"></kbd></button>
|
|
30
|
+
<button type="button" data-cmd="settings">settings <kbd data-key="settings"></kbd></button>
|
|
31
|
+
</nav>
|
|
32
|
+
</header>
|
|
33
|
+
|
|
34
|
+
<p class="banner" id="banner" hidden></p>
|
|
35
|
+
<p class="banner" id="boot-note">loading zero… if this message stays, open zero through its own server: run <code>npm start</code> in the zero folder, then visit http://localhost:4321. opening index.html directly won’t work.</p>
|
|
36
|
+
|
|
37
|
+
<main class="panes" id="panes">
|
|
38
|
+
|
|
39
|
+
<section class="pane" data-pane="payoff" aria-labelledby="h-payoff">
|
|
40
|
+
<header class="pane-head"><h2 id="h-payoff"><span class="idx"></span>payoff</h2></header>
|
|
41
|
+
<p class="lead" id="hero-lead">debt-free by</p>
|
|
42
|
+
<p class="hero" id="hero-date">—</p>
|
|
43
|
+
<dl class="facts" id="facts" aria-live="polite"></dl>
|
|
44
|
+
<figure class="chart-fig">
|
|
45
|
+
<p class="readout" id="readout" aria-hidden="true"></p>
|
|
46
|
+
<div class="payoff-chart" id="payoff-chart"></div>
|
|
47
|
+
<figcaption class="legend">
|
|
48
|
+
<span class="key key-plan">this plan</span>
|
|
49
|
+
<span class="key key-min" id="key-min">minimums only</span>
|
|
50
|
+
<span class="key key-promo" id="key-promo" hidden>promo rate ends</span>
|
|
51
|
+
</figcaption>
|
|
52
|
+
</figure>
|
|
53
|
+
</section>
|
|
54
|
+
|
|
55
|
+
<section class="pane" data-pane="progress" aria-labelledby="h-progress">
|
|
56
|
+
<header class="pane-head">
|
|
57
|
+
<h2 id="h-progress"><span class="idx"></span>progress</h2>
|
|
58
|
+
<button type="button" class="quiet" data-cmd="checkin">check in <kbd data-key="checkin"></kbd></button>
|
|
59
|
+
</header>
|
|
60
|
+
<div id="progress-body"></div>
|
|
61
|
+
</section>
|
|
62
|
+
|
|
63
|
+
<section class="pane" data-pane="debts" aria-labelledby="h-debts">
|
|
64
|
+
<header class="pane-head">
|
|
65
|
+
<h2 id="h-debts"><span class="idx"></span>debts</h2>
|
|
66
|
+
<button type="button" class="quiet" data-cmd="add-debt">+ add a debt</button>
|
|
67
|
+
</header>
|
|
68
|
+
<div class="table-wrap">
|
|
69
|
+
<table class="debts">
|
|
70
|
+
<thead>
|
|
71
|
+
<tr>
|
|
72
|
+
<th scope="col">name</th>
|
|
73
|
+
<th scope="col" class="num">balance</th>
|
|
74
|
+
<th scope="col" class="num">rate %</th>
|
|
75
|
+
<th scope="col" class="num">promo %</th>
|
|
76
|
+
<th scope="col">promo ends</th>
|
|
77
|
+
<th scope="col" class="num">minimum</th>
|
|
78
|
+
<th scope="col" title="a card you clear every statement">paid in full</th>
|
|
79
|
+
<th scope="col">gone by</th>
|
|
80
|
+
<th scope="col"><span class="sr-only">actions</span></th>
|
|
81
|
+
</tr>
|
|
82
|
+
</thead>
|
|
83
|
+
<tbody id="debt-rows"></tbody>
|
|
84
|
+
</table>
|
|
85
|
+
</div>
|
|
86
|
+
</section>
|
|
87
|
+
|
|
88
|
+
<section class="pane" data-pane="plan" aria-labelledby="h-plan">
|
|
89
|
+
<header class="pane-head"><h2 id="h-plan"><span class="idx"></span>plan</h2></header>
|
|
90
|
+
<fieldset class="strategy">
|
|
91
|
+
<legend>extra money goes to</legend>
|
|
92
|
+
<label><input type="radio" name="strategy" value="avalanche"> highest rate first</label>
|
|
93
|
+
<label><input type="radio" name="strategy" value="snowball"> smallest balance first</label>
|
|
94
|
+
<label><input type="radio" name="strategy" value="custom"> my own order</label>
|
|
95
|
+
<p class="hint" id="strategy-hint"></p>
|
|
96
|
+
</fieldset>
|
|
97
|
+
<label class="switch">
|
|
98
|
+
<input type="checkbox" id="student-mode">
|
|
99
|
+
<span>student mode <kbd data-key="student"></kbd><small id="student-hint"></small></span>
|
|
100
|
+
</label>
|
|
101
|
+
<div class="slider">
|
|
102
|
+
<label for="extra-hours">extra shifts</label>
|
|
103
|
+
<output id="extra-hours-out" for="extra-hours"></output>
|
|
104
|
+
<input type="range" id="extra-hours" min="0" max="20" step="1">
|
|
105
|
+
</div>
|
|
106
|
+
<div class="slider">
|
|
107
|
+
<label for="extra-monthly">find more each month</label>
|
|
108
|
+
<output id="extra-monthly-out" for="extra-monthly"></output>
|
|
109
|
+
<input type="range" id="extra-monthly" min="0" max="1000" step="25">
|
|
110
|
+
</div>
|
|
111
|
+
<div class="lump">
|
|
112
|
+
<span id="lump-label">one-time payment</span>
|
|
113
|
+
<input type="number" id="lump-amount" min="0" step="10" inputmode="decimal" placeholder="0" aria-labelledby="lump-label">
|
|
114
|
+
<input type="month" id="lump-month" aria-label="month of the one-time payment">
|
|
115
|
+
</div>
|
|
116
|
+
<details class="numbers">
|
|
117
|
+
<summary>income and spending</summary>
|
|
118
|
+
<div class="numbers-grid">
|
|
119
|
+
<label>take-home a month <input type="number" data-money="takeHome" min="0" step="10" inputmode="decimal"></label>
|
|
120
|
+
<label>take-home an hour <input type="number" data-money="netHourly" min="0" step="0.05" inputmode="decimal"></label>
|
|
121
|
+
<label>fixed bills a month <input type="number" data-money="fixed" min="0" step="5" inputmode="decimal"></label>
|
|
122
|
+
<label>everyday spending <input type="number" data-money="living" min="0" step="5" inputmode="decimal"></label>
|
|
123
|
+
<label>student-mode spending <input type="number" data-money="studentLiving" min="0" step="5" inputmode="decimal"></label>
|
|
124
|
+
</div>
|
|
125
|
+
</details>
|
|
126
|
+
<p class="math" id="budget-math"></p>
|
|
127
|
+
</section>
|
|
128
|
+
|
|
129
|
+
<section class="pane" data-pane="notes" aria-labelledby="h-notes">
|
|
130
|
+
<header class="pane-head">
|
|
131
|
+
<h2 id="h-notes"><span class="idx"></span>notes</h2>
|
|
132
|
+
<button type="button" class="quiet" data-cmd="ask" id="ask-claude">ask claude <kbd data-key="ask"></kbd></button>
|
|
133
|
+
</header>
|
|
134
|
+
<ol class="note-list" id="local-notes"></ol>
|
|
135
|
+
<div class="claude-notes" id="claude-notes" aria-live="polite"></div>
|
|
136
|
+
</section>
|
|
137
|
+
|
|
138
|
+
<section class="pane" data-pane="flow" aria-labelledby="h-flow">
|
|
139
|
+
<header class="pane-head">
|
|
140
|
+
<h2 id="h-flow"><span class="idx"></span>money in / out</h2>
|
|
141
|
+
<label class="inline">window
|
|
142
|
+
<select id="window">
|
|
143
|
+
<option value="30">30 days</option>
|
|
144
|
+
<option value="90">90 days</option>
|
|
145
|
+
<option value="0">all</option>
|
|
146
|
+
</select>
|
|
147
|
+
</label>
|
|
148
|
+
</header>
|
|
149
|
+
<div class="flow-grid">
|
|
150
|
+
<figure>
|
|
151
|
+
<figcaption>by month</figcaption>
|
|
152
|
+
<div id="flow-chart" class="flow-chart"></div>
|
|
153
|
+
<p class="legend"><span class="key key-in">in</span><span class="key key-out">out</span></p>
|
|
154
|
+
</figure>
|
|
155
|
+
<figure>
|
|
156
|
+
<figcaption>where it went</figcaption>
|
|
157
|
+
<ol class="cats" id="cats"></ol>
|
|
158
|
+
</figure>
|
|
159
|
+
</div>
|
|
160
|
+
</section>
|
|
161
|
+
|
|
162
|
+
<section class="pane" data-pane="transactions" aria-labelledby="h-tx">
|
|
163
|
+
<header class="pane-head">
|
|
164
|
+
<h2 id="h-tx"><span class="idx"></span>transactions</h2>
|
|
165
|
+
<span class="hint" id="tx-count"></span>
|
|
166
|
+
</header>
|
|
167
|
+
<div class="filters">
|
|
168
|
+
<input type="search" id="tx-search" placeholder="search /" aria-label="search transactions">
|
|
169
|
+
<select id="tx-account" aria-label="account"></select>
|
|
170
|
+
<select id="tx-category" aria-label="category"></select>
|
|
171
|
+
</div>
|
|
172
|
+
<div class="table-wrap">
|
|
173
|
+
<table class="txs">
|
|
174
|
+
<thead><tr><th scope="col">date</th><th scope="col">description</th><th scope="col">account</th><th scope="col">category</th><th scope="col" class="num">amount</th><th scope="col"><span class="sr-only">actions</span></th></tr></thead>
|
|
175
|
+
<tbody id="tx-rows"></tbody>
|
|
176
|
+
</table>
|
|
177
|
+
</div>
|
|
178
|
+
<button type="button" class="quiet more" id="tx-more" data-cmd="more-tx" hidden>show more</button>
|
|
179
|
+
</section>
|
|
180
|
+
</main>
|
|
181
|
+
|
|
182
|
+
<footer class="bar status" id="status" aria-label="status"></footer>
|
|
183
|
+
|
|
184
|
+
<!-- command palette -->
|
|
185
|
+
<dialog id="palette" class="palette" aria-label="commands">
|
|
186
|
+
<input type="text" placeholder="type a command" aria-label="command" role="combobox" aria-expanded="true" aria-controls="palette-list" autocomplete="off" spellcheck="false">
|
|
187
|
+
<ol id="palette-list" role="listbox"></ol>
|
|
188
|
+
</dialog>
|
|
189
|
+
|
|
190
|
+
<!-- import -->
|
|
191
|
+
<dialog id="import-dialog" aria-labelledby="import-h">
|
|
192
|
+
<form method="dialog" class="dialog-body">
|
|
193
|
+
<h2 id="import-h">import</h2>
|
|
194
|
+
<label class="drop" id="drop">
|
|
195
|
+
<input type="file" id="import-file" accept=".csv,text/csv,.ofx,.qfx,.json,application/json,.pdf,application/pdf,image/png,image/jpeg,image/webp">
|
|
196
|
+
<span>choose a file or drop it here</span>
|
|
197
|
+
<small>a csv or OFX/QFX download from your bank, a zero backup (.json), or a PDF statement or screenshot for claude to read</small>
|
|
198
|
+
</label>
|
|
199
|
+
<div id="import-preview"></div>
|
|
200
|
+
<p class="error" id="import-error" role="alert"></p>
|
|
201
|
+
<div class="dialog-actions">
|
|
202
|
+
<button value="cancel" class="quiet">cancel</button>
|
|
203
|
+
<button type="button" id="import-confirm" data-cmd="confirm-import" disabled>add transactions</button>
|
|
204
|
+
</div>
|
|
205
|
+
</form>
|
|
206
|
+
</dialog>
|
|
207
|
+
|
|
208
|
+
<!-- check-in -->
|
|
209
|
+
<dialog id="checkin-dialog" aria-labelledby="checkin-h">
|
|
210
|
+
<form method="dialog" class="dialog-body" id="checkin-form">
|
|
211
|
+
<h2 id="checkin-h">check in</h2>
|
|
212
|
+
<p class="hint">type in what each account says you owe today. zero compares the total with your plan, and these become your current balances.</p>
|
|
213
|
+
<label class="field"><span>date</span><span class="row"><input type="date" id="checkin-date" required></span></label>
|
|
214
|
+
<div id="checkin-rows" class="checkin-rows"></div>
|
|
215
|
+
<p class="hint" id="checkin-total"></p>
|
|
216
|
+
<div class="dialog-actions">
|
|
217
|
+
<button value="cancel" class="quiet" formnovalidate>cancel</button>
|
|
218
|
+
<button type="button" data-cmd="save-checkin">save check-in</button>
|
|
219
|
+
</div>
|
|
220
|
+
</form>
|
|
221
|
+
</dialog>
|
|
222
|
+
|
|
223
|
+
<!-- settings -->
|
|
224
|
+
<dialog id="settings-dialog" class="settings" aria-labelledby="settings-h">
|
|
225
|
+
<div class="dialog-body">
|
|
226
|
+
<header class="dialog-head">
|
|
227
|
+
<h2 id="settings-h">settings</h2>
|
|
228
|
+
<button type="button" class="quiet" data-cmd="close-settings">close <kbd>esc</kbd></button>
|
|
229
|
+
</header>
|
|
230
|
+
<div id="settings-body"></div>
|
|
231
|
+
</div>
|
|
232
|
+
</dialog>
|
|
233
|
+
|
|
234
|
+
<!-- shortcuts -->
|
|
235
|
+
<dialog id="help-dialog" aria-labelledby="help-h">
|
|
236
|
+
<form method="dialog" class="dialog-body">
|
|
237
|
+
<h2 id="help-h">keyboard</h2>
|
|
238
|
+
<dl class="shortcuts" id="shortcuts"></dl>
|
|
239
|
+
<div class="dialog-actions"><button value="ok" class="quiet">close</button></div>
|
|
240
|
+
</form>
|
|
241
|
+
</dialog>
|
|
242
|
+
|
|
243
|
+
<p class="toast" id="toast" role="status" aria-live="polite"></p>
|
|
244
|
+
</body>
|
|
245
|
+
</html>
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// analysis.js — looks back at transactions: money in vs out, where it went, who it went to.
|
|
2
|
+
|
|
3
|
+
import { kindOf, merchantKey } from './categorize.js';
|
|
4
|
+
|
|
5
|
+
/** The newest transaction date, so "last 30 days" means the last 30 days of your data. */
|
|
6
|
+
export function latestDate(transactions) {
|
|
7
|
+
return transactions.reduce((max, t) => (t.date > max ? t.date : max), '0000-00-00');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function since(transactions, days) {
|
|
11
|
+
if (!days || !transactions.length) return transactions;
|
|
12
|
+
// whole-day arithmetic in UTC, so time zones can't shift the cut-off by a day
|
|
13
|
+
const [y, m, d] = latestDate(transactions).split('-').map(Number);
|
|
14
|
+
const cut = new Date(Date.UTC(y, m - 1, d - days)).toISOString().slice(0, 10);
|
|
15
|
+
return transactions.filter((t) => t.date > cut);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** [{ ym, in, out }] oldest first. Card payments are skipped so nothing is counted twice. */
|
|
19
|
+
export function monthlyFlow(transactions) {
|
|
20
|
+
const by = {};
|
|
21
|
+
for (const t of transactions) {
|
|
22
|
+
const kind = kindOf(t.category);
|
|
23
|
+
if (kind === 'transfer') continue;
|
|
24
|
+
const ym = t.date.slice(0, 7);
|
|
25
|
+
by[ym] ||= { ym, in: 0, out: 0 };
|
|
26
|
+
if (kind === 'in') by[ym].in += t.amount;
|
|
27
|
+
else by[ym].out -= t.amount; // refunds (positive) shrink money out
|
|
28
|
+
}
|
|
29
|
+
return Object.values(by).sort((a, b) => a.ym.localeCompare(b.ym));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** [{ category, total, count }] biggest first, money out only. */
|
|
33
|
+
export function byCategory(transactions) {
|
|
34
|
+
const by = {};
|
|
35
|
+
for (const t of transactions) {
|
|
36
|
+
if (kindOf(t.category) !== 'out') continue;
|
|
37
|
+
by[t.category] ||= { category: t.category, total: 0, count: 0 };
|
|
38
|
+
by[t.category].total -= t.amount;
|
|
39
|
+
if (t.amount < 0) by[t.category].count++;
|
|
40
|
+
}
|
|
41
|
+
return Object.values(by).filter((c) => c.total > 0.005).sort((a, b) => b.total - a.total);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** [{ name, total, count, category }] biggest first. `name` is a readable sample description. */
|
|
45
|
+
export function topMerchants(transactions, limit = 12) {
|
|
46
|
+
const by = {};
|
|
47
|
+
for (const t of transactions) {
|
|
48
|
+
if (kindOf(t.category) !== 'out' || t.amount >= 0) continue;
|
|
49
|
+
const k = merchantKey(t.description) || t.description;
|
|
50
|
+
by[k] ||= { name: t.description, total: 0, count: 0, category: t.category };
|
|
51
|
+
by[k].total -= t.amount;
|
|
52
|
+
by[k].count++;
|
|
53
|
+
}
|
|
54
|
+
return Object.values(by).sort((a, b) => b.total - a.total).slice(0, limit);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Purchases on a debt's own account (matched by name) — spending on a card that charges interest. */
|
|
58
|
+
export function chargesOn(transactions, debtName) {
|
|
59
|
+
const n = debtName.toLowerCase();
|
|
60
|
+
return transactions.filter((t) => t.amount < 0 && kindOf(t.category) === 'out' &&
|
|
61
|
+
(t.account.toLowerCase().includes(n) || n.includes(t.account.toLowerCase())));
|
|
62
|
+
}
|
package/public/js/api.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
// api.js — talks to the local server (server.js). If there's no server — the app was
|
|
2
|
+
// opened as static files, e.g. a hosted demo — zero runs in demo mode: data stays in
|
|
3
|
+
// this browser's localStorage and Claude is off. API keys are never kept in the browser.
|
|
4
|
+
|
|
5
|
+
let info = null; // the server's /api/health answer, or null in demo mode
|
|
6
|
+
|
|
7
|
+
const HEADERS = { 'content-type': 'application/json', 'x-zero': '1' };
|
|
8
|
+
|
|
9
|
+
async function call(method, path, body, extraHeaders = {}) {
|
|
10
|
+
let res;
|
|
11
|
+
try {
|
|
12
|
+
res = await fetch(`api/${path}`, { method, headers: { ...HEADERS, ...extraHeaders }, body: body === undefined ? undefined : JSON.stringify(body) });
|
|
13
|
+
} catch {
|
|
14
|
+
throw new Error('can’t reach the zero server. is `npm start` still running?');
|
|
15
|
+
}
|
|
16
|
+
const data = await res.json().catch(() => ({}));
|
|
17
|
+
if (!res.ok) throw Object.assign(new Error(data.error || `the server answered ${res.status}`), { status: res.status });
|
|
18
|
+
return data;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Finds out whether the local server is there. Call once at startup. */
|
|
22
|
+
export async function connect() {
|
|
23
|
+
// the server only ever listens on this machine, so anywhere else is the static demo
|
|
24
|
+
if (!['localhost', '127.0.0.1'].includes(location.hostname)) { info = null; return info; }
|
|
25
|
+
try {
|
|
26
|
+
const res = await fetch('api/health', { headers: HEADERS });
|
|
27
|
+
info = res.ok ? await res.json() : null;
|
|
28
|
+
} catch { info = null; }
|
|
29
|
+
return info;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const isLocal = () => Boolean(info);
|
|
33
|
+
export const serverInfo = () => info;
|
|
34
|
+
export async function refresh() { return connect(); }
|
|
35
|
+
|
|
36
|
+
/* ---------- data files: state.json and settings.json ---------- */
|
|
37
|
+
|
|
38
|
+
const LS = { state: 'zero.state.v1', settings: 'zero.settings.v1' };
|
|
39
|
+
|
|
40
|
+
// the version of each file this tab last read or wrote. sent with every write, so the server
|
|
41
|
+
// can refuse to overwrite changes made in another tab or by hand (it answers 409).
|
|
42
|
+
const revs = {};
|
|
43
|
+
|
|
44
|
+
/** Resolves to the saved value or null. Throws (err.status 422) if the file on disk is broken. */
|
|
45
|
+
export async function readFile(name) {
|
|
46
|
+
if (info) {
|
|
47
|
+
const { value, rev } = await call('GET', `data/${name}`);
|
|
48
|
+
revs[name] = rev;
|
|
49
|
+
return value;
|
|
50
|
+
}
|
|
51
|
+
try { const raw = localStorage.getItem(LS[name]); return raw ? JSON.parse(raw) : null; } catch { return null; }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Throws with err.status 409 if the file changed since this tab read it. */
|
|
55
|
+
export async function writeFile(name, value) {
|
|
56
|
+
if (info) {
|
|
57
|
+
const r = await call('PUT', `data/${name}`, value, revs[name] ? { 'x-zero-rev': revs[name] } : {});
|
|
58
|
+
revs[name] = r.rev;
|
|
59
|
+
return r;
|
|
60
|
+
}
|
|
61
|
+
try { localStorage.setItem(LS[name], JSON.stringify(value)); return { ok: true }; } catch { throw new Error('this browser is out of space or won’t let zero save'); }
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export async function eraseLocal() {
|
|
65
|
+
try { Object.values(LS).forEach((k) => localStorage.removeItem(k)); } catch { /* nothing stored */ }
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/* ---------- api key ---------- */
|
|
69
|
+
|
|
70
|
+
export async function saveKey(key) {
|
|
71
|
+
if (!info) throw new Error('keys can only be saved through the local server');
|
|
72
|
+
const r = await call('POST', 'key', { key });
|
|
73
|
+
await connect();
|
|
74
|
+
return r;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export async function removeKey() {
|
|
78
|
+
await call('DELETE', 'key');
|
|
79
|
+
await connect();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/* ---------- claude ---------- */
|
|
83
|
+
|
|
84
|
+
export async function claude(task, payload) {
|
|
85
|
+
if (!info) throw new Error('claude needs the local server. run `npm start` and open http://localhost:4321.');
|
|
86
|
+
if (!info.key.set) throw new Error('add your anthropic api key in settings first.');
|
|
87
|
+
return (await call('POST', 'claude', { task, payload })).result;
|
|
88
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// categorize.js — guesses a category from a transaction description.
|
|
2
|
+
//
|
|
3
|
+
// To teach it a new merchant, add a line to RULES. The first match wins, so put
|
|
4
|
+
// specific patterns above general ones. Categories you set by hand in the app are
|
|
5
|
+
// remembered per merchant and always beat these rules (see `merchantKey`).
|
|
6
|
+
|
|
7
|
+
export const CATEGORIES = [
|
|
8
|
+
'income', 'groceries', 'gas & convenience', 'fast food & dining', 'work meals',
|
|
9
|
+
'subscriptions', 'gaming', 'shopping', 'personal care & style', 'transport & parking',
|
|
10
|
+
'car', 'bills', 'giving', 'loan payment', 'card payment', 'interest & fees',
|
|
11
|
+
'business', 'refund', 'other',
|
|
12
|
+
];
|
|
13
|
+
|
|
14
|
+
// What a category means for the money in / out view.
|
|
15
|
+
// in counts as money in
|
|
16
|
+
// transfer money moving between your own accounts (card payments) — not counted,
|
|
17
|
+
// because the purchases themselves are already counted on the card
|
|
18
|
+
// out everything else counts as money out
|
|
19
|
+
export function kindOf(category) {
|
|
20
|
+
if (category === 'income') return 'in';
|
|
21
|
+
if (category === 'card payment') return 'transfer';
|
|
22
|
+
return 'out';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const RULES = [
|
|
26
|
+
[/payroll|direct dep|paycheck|salary/i, 'income'],
|
|
27
|
+
[/card.*pay(ment)?|crd epay|autopay|payment thank you|mobile pmt|payment from/i, 'card payment'],
|
|
28
|
+
[/student ln|dept education|navient|nelnet|mohela|aidvantage/i, 'loan payment'],
|
|
29
|
+
[/interest charge|late fee|annual fee|finance charge/i, 'interest & fees'],
|
|
30
|
+
[/donation|tithe|tithing|church|charity/i, 'giving'],
|
|
31
|
+
[/kroger|king soopers|walmart|safeway|target|costco|whole foods|trader joe|aldi|sprouts|publix|h-e-b/i, 'groceries'],
|
|
32
|
+
[/7-eleven|loaf\W*n\W*jug|shell|conoco|chevron|exxon|circle k|maverik|kum ?& ?go|quiktrip|wawa|sheetz/i, 'gas & convenience'],
|
|
33
|
+
[/mcdonald|raising cane|chick-?fil|taco bell|wendy|burger|chipotle|subway|starbucks|domino|pizza|doordash|uber eats|grubhub/i, 'fast food & dining'],
|
|
34
|
+
[/spotify|netflix|hulu|disney\+|discord|google one|icloud|apple\.com\/bill|anthropic|claude|openai|amazon prime|prime video|youtube premium/i, 'subscriptions'],
|
|
35
|
+
[/steam|xbox|playstation|nintendo|epic games|riot|roblox/i, 'gaming'],
|
|
36
|
+
[/sally beauty|sephora|ulta|barber|salon/i, 'personal care & style'],
|
|
37
|
+
[/parking|uber(?! eats)|lyft|transit|toll/i, 'transport & parking'],
|
|
38
|
+
[/grease monkey|jiffy|autozone|o'?reilly|napa|tire|car wash|valvoline/i, 'car'],
|
|
39
|
+
[/secretary.?state|cloudflare|namecheap|github|vercel|gumroad/i, 'business'],
|
|
40
|
+
[/xcel|comcast|xfinity|verizon|t-mobile|at&t|insurance|geico|progressive|rent/i, 'bills'],
|
|
41
|
+
[/amazon|amzn|ebay|etsy|best buy/i, 'shopping'],
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
/** Normalizes a description so "MCDONALDS 17005 130-3660880 CO" and "McDonald's" share a key. */
|
|
45
|
+
export function merchantKey(description) {
|
|
46
|
+
return String(description || '')
|
|
47
|
+
.toLowerCase()
|
|
48
|
+
.replace(/[0-9#*]+/g, ' ')
|
|
49
|
+
.replace(/[^a-z&' ]+/g, ' ')
|
|
50
|
+
.replace(/\b(co|ca|wa|ny|tn|va|de|castle rock|denver|pending)\b/g, ' ')
|
|
51
|
+
.replace(/\s+/g, ' ')
|
|
52
|
+
.trim()
|
|
53
|
+
.split(' ')
|
|
54
|
+
.slice(0, 3)
|
|
55
|
+
.join(' ');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @param {string} description
|
|
60
|
+
* @param {number} amount signed: + money in, − money out
|
|
61
|
+
* @param {Object} learned { merchantKey: category } from the user's own edits
|
|
62
|
+
*/
|
|
63
|
+
export function categorize(description, amount, learned = {}) {
|
|
64
|
+
const key = merchantKey(description);
|
|
65
|
+
if (learned[key]) return learned[key];
|
|
66
|
+
for (const [pattern, category] of RULES) if (pattern.test(description)) return category;
|
|
67
|
+
if (amount > 0 && /refund|return|credit/i.test(description)) return 'refund';
|
|
68
|
+
return amount > 0 ? 'income' : 'other';
|
|
69
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
// charts.js — two small hand-drawn SVG charts. No chart library, so no third-party
|
|
2
|
+
// script ever runs next to your numbers. Colors come from CSS classes in zero.css.
|
|
3
|
+
|
|
4
|
+
import { money, monthName, monthsBetween } from './format.js';
|
|
5
|
+
import { addMonths } from './payoff.js';
|
|
6
|
+
|
|
7
|
+
const NS = 'http://www.w3.org/2000/svg';
|
|
8
|
+
|
|
9
|
+
function el(name, attrs = {}, parent) {
|
|
10
|
+
const n = document.createElementNS(NS, name);
|
|
11
|
+
for (const [k, v] of Object.entries(attrs)) n.setAttribute(k, v);
|
|
12
|
+
if (parent) parent.appendChild(n);
|
|
13
|
+
return n;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Tidy y-axis ticks: about `ticks` steps of 1, 2, 2.5 or 5 × a power of ten. 4,163 → step 1,000, max 5,000. */
|
|
17
|
+
function niceScale(v, ticks = 4) {
|
|
18
|
+
if (v <= 0) return { max: 100, step: 25 };
|
|
19
|
+
const raw = v / ticks;
|
|
20
|
+
const p = 10 ** Math.floor(Math.log10(raw));
|
|
21
|
+
const step = [0.5, 1, 2, 2.5, 5, 10].map((m) => m * p).find((m) => v / m <= ticks + 1);
|
|
22
|
+
return { max: Math.ceil(v / step) * step, step };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The hero chart: total balance over time for this plan and for minimums only.
|
|
27
|
+
* @param {HTMLElement} box
|
|
28
|
+
* @param {Object} o { start, startTotal, plan, base, promos: [{ ym, name }], showMin, animate, label, onHover }
|
|
29
|
+
*/
|
|
30
|
+
export function payoffChart(box, o) {
|
|
31
|
+
const W = box.clientWidth || 800, H = box.clientHeight || 300;
|
|
32
|
+
const pad = { t: 12, r: 16, b: 28, l: 56 };
|
|
33
|
+
const iw = W - pad.l - pad.r, ih = H - pad.t - pad.b;
|
|
34
|
+
|
|
35
|
+
// x runs from today to a bit past the plan's last month (never shorter than a year)
|
|
36
|
+
const planPts = [{ ym: o.start, total: o.startTotal }, ...o.plan.months.map((m) => ({ ym: m.ym, total: m.total }))];
|
|
37
|
+
const span = Math.max(12, Math.ceil((planPts.length - 1) * 1.6));
|
|
38
|
+
const basePts = [{ ym: o.start, total: o.startTotal }, ...o.base.months.slice(0, span).map((m) => ({ ym: m.ym, total: m.total }))];
|
|
39
|
+
const { max: yMax, step } = niceScale(Math.max(o.startTotal, ...planPts.map((p) => p.total), ...(o.showMin !== false ? basePts.map((p) => p.total) : [])));
|
|
40
|
+
const x = (i) => pad.l + (i / span) * iw;
|
|
41
|
+
const y = (v) => pad.t + ih - (v / yMax) * ih;
|
|
42
|
+
|
|
43
|
+
const svg = el('svg', { viewBox: `0 0 ${W} ${H}`, role: 'img', 'aria-label': o.label });
|
|
44
|
+
const grid = el('g', { class: 'grid' }, svg);
|
|
45
|
+
const axis = el('g', { class: 'axis' }, svg);
|
|
46
|
+
for (let v = 0; v <= yMax + 0.01; v += step) {
|
|
47
|
+
el('line', { x1: pad.l, x2: W - pad.r, y1: y(v), y2: y(v) }, grid);
|
|
48
|
+
el('text', { x: pad.l - 8, y: y(v) + 4, 'text-anchor': 'end' }, axis).textContent = money(v);
|
|
49
|
+
}
|
|
50
|
+
// label as many months as fit at ~64px apart, on a step that reads naturally
|
|
51
|
+
const fit = Math.max(1, Math.floor(iw / 76));
|
|
52
|
+
const every = [1, 2, 3, 6, 12, 24, 60].find((s) => span / s <= fit) || 120;
|
|
53
|
+
for (let i = 0; i <= span; i += every) {
|
|
54
|
+
const ym = addMonths(o.start, i);
|
|
55
|
+
el('text', { x: x(i), y: H - 6, 'text-anchor': i === span ? 'end' : 'middle' }, axis).textContent = monthName(ym, true);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// promo deadlines: a red pencil tick where a 0% rate runs out
|
|
59
|
+
for (const p of o.promos) {
|
|
60
|
+
const i = monthsBetween(o.start, p.ym);
|
|
61
|
+
if (i <= 0 || i > span) continue;
|
|
62
|
+
const g = el('g', { class: 'promo' }, svg);
|
|
63
|
+
el('line', { x1: x(i), x2: x(i), y1: pad.t, y2: pad.t + ih }, g);
|
|
64
|
+
el('text', { x: x(i) + 5, y: pad.t + 12 }, g).textContent = `${p.name} 0% ends`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const path = (pts) => pts.map((p, i) => `${i ? 'L' : 'M'}${x(i).toFixed(1)},${y(p.total).toFixed(1)}`).join('');
|
|
68
|
+
if (o.showMin !== false) el('path', { class: 'line-min', d: path(basePts) }, svg);
|
|
69
|
+
el('path', { class: 'area-plan', d: `${path(planPts)}L${x(planPts.length - 1)},${y(0)}L${x(0)},${y(0)}Z` }, svg);
|
|
70
|
+
const line = el('path', { class: 'line-plan', d: path(planPts) }, svg);
|
|
71
|
+
|
|
72
|
+
const done = o.plan.debtFree;
|
|
73
|
+
let dot;
|
|
74
|
+
if (done) dot = el('circle', { class: 'zero-dot', cx: x(planPts.length - 1), cy: y(0), r: 5 }, svg);
|
|
75
|
+
|
|
76
|
+
// hover / touch readout
|
|
77
|
+
const cursor = el('g', { class: 'cursor', visibility: 'hidden' }, svg);
|
|
78
|
+
const cl = el('line', { y1: pad.t, y2: pad.t + ih }, cursor);
|
|
79
|
+
const cc = el('circle', { r: 3.5 }, cursor);
|
|
80
|
+
const hit = el('rect', { x: pad.l, y: pad.t, width: iw, height: ih, fill: 'transparent' }, svg);
|
|
81
|
+
const move = (evt) => {
|
|
82
|
+
const r = svg.getBoundingClientRect();
|
|
83
|
+
const px = ((evt.clientX - r.left) / r.width) * W;
|
|
84
|
+
const i = Math.max(0, Math.min(planPts.length - 1, Math.round(((px - pad.l) / iw) * span)));
|
|
85
|
+
cl.setAttribute('x1', x(i)); cl.setAttribute('x2', x(i));
|
|
86
|
+
cc.setAttribute('cx', x(i)); cc.setAttribute('cy', y(planPts[i].total));
|
|
87
|
+
cursor.setAttribute('visibility', 'visible');
|
|
88
|
+
o.onHover?.(planPts[i], basePts[i]);
|
|
89
|
+
};
|
|
90
|
+
hit.addEventListener('pointermove', move);
|
|
91
|
+
hit.addEventListener('pointerdown', move);
|
|
92
|
+
hit.addEventListener('pointerleave', () => { cursor.setAttribute('visibility', 'hidden'); o.onHover?.(null); });
|
|
93
|
+
|
|
94
|
+
box.replaceChildren(svg);
|
|
95
|
+
|
|
96
|
+
// the one orchestrated moment: the line inks itself in on first load
|
|
97
|
+
if (o.animate && !matchMedia('(prefers-reduced-motion: reduce)').matches) {
|
|
98
|
+
const len = line.getTotalLength();
|
|
99
|
+
line.style.setProperty('--len', len);
|
|
100
|
+
line.classList.add('draw');
|
|
101
|
+
if (dot) { dot.style.opacity = 0; setTimeout(() => { dot.style.transition = 'opacity .3s'; dot.style.opacity = 1; }, 1500); }
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Monthly bars: money in (ink) beside money out (red pencil). */
|
|
106
|
+
export function flowChart(box, months) {
|
|
107
|
+
const W = box.clientWidth || 500, H = box.clientHeight || 220;
|
|
108
|
+
const pad = { t: 8, r: 4, b: 24, l: 52 };
|
|
109
|
+
const iw = W - pad.l - pad.r, ih = H - pad.t - pad.b;
|
|
110
|
+
const data = months.slice(-8);
|
|
111
|
+
if (!data.length) { box.innerHTML = '<p class="empty">import transactions to see this.</p>'; return; }
|
|
112
|
+
const { max: yMax, step } = niceScale(Math.max(...data.flatMap((m) => [m.in, m.out])), 3);
|
|
113
|
+
const y = (v) => pad.t + ih - (Math.max(0, v) / yMax) * ih;
|
|
114
|
+
const slot = iw / data.length, bw = Math.min(18, slot / 3);
|
|
115
|
+
|
|
116
|
+
const svg = el('svg', { viewBox: `0 0 ${W} ${H}`, role: 'img', 'aria-label': data.map((m) => `${monthName(m.ym)}: ${money(m.in)} in, ${money(m.out)} out`).join('; ') });
|
|
117
|
+
const grid = el('g', { class: 'grid' }, svg), axis = el('g', { class: 'axis' }, svg);
|
|
118
|
+
for (let v = 0; v <= yMax + 0.01; v += step) {
|
|
119
|
+
el('line', { x1: pad.l, x2: W - pad.r, y1: y(v), y2: y(v) }, grid);
|
|
120
|
+
el('text', { x: pad.l - 8, y: y(v) + 4, 'text-anchor': 'end' }, axis).textContent = money(v);
|
|
121
|
+
}
|
|
122
|
+
data.forEach((m, i) => {
|
|
123
|
+
const cx = pad.l + slot * i + slot / 2;
|
|
124
|
+
el('rect', { class: 'bar-in', x: cx - bw - 1, y: y(m.in), width: bw, height: pad.t + ih - y(m.in), rx: 1.5 }, svg)
|
|
125
|
+
.appendChild(el('title')).textContent = `in ${money(m.in)}`;
|
|
126
|
+
el('rect', { class: 'bar-out', x: cx + 1, y: y(m.out), width: bw, height: pad.t + ih - y(m.out), rx: 1.5 }, svg)
|
|
127
|
+
.appendChild(el('title')).textContent = `out ${money(m.out)}`;
|
|
128
|
+
el('text', { x: cx, y: H - 6, 'text-anchor': 'middle' }, axis).textContent = monthName(m.ym, true);
|
|
129
|
+
});
|
|
130
|
+
box.replaceChildren(svg);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Progress chart: the plan you set (a line) and your check-ins (dots), on a day axis.
|
|
135
|
+
* @param {HTMLElement} box
|
|
136
|
+
* @param {Object} o { baseline, points: [{ date, actual }], label }
|
|
137
|
+
*/
|
|
138
|
+
export function progressChart(box, o) {
|
|
139
|
+
const W = box.clientWidth || 600, H = box.clientHeight || 180;
|
|
140
|
+
const pad = { t: 10, r: 14, b: 24, l: 56 };
|
|
141
|
+
const iw = W - pad.l - pad.r, ih = H - pad.t - pad.b;
|
|
142
|
+
const day = (iso) => { const [y, m, d] = iso.split('-').map(Number); return Date.UTC(y, m - 1, d); };
|
|
143
|
+
const monthEnd = (ym) => { const [y, m] = ym.split('-').map(Number); return Date.UTC(y, m, 0); };
|
|
144
|
+
|
|
145
|
+
const plan = [[day(o.baseline.setOn), o.baseline.startTotal], ...o.baseline.months.map((m) => [monthEnd(m.ym), m.total])];
|
|
146
|
+
const actual = o.points.map((p) => [day(p.date), p.actual]);
|
|
147
|
+
// show the plan up to a little past your latest check-in, so recent months stay readable
|
|
148
|
+
const lastT = Math.max(plan[0][0], ...actual.map((p) => p[0]));
|
|
149
|
+
const endT = Math.min(plan[plan.length - 1][0], lastT + 1000 * 86400 * 150);
|
|
150
|
+
const t0 = plan[0][0], t1 = Math.max(endT, t0 + 1000 * 86400 * 60);
|
|
151
|
+
const { max: yMax, step } = niceScale(Math.max(o.baseline.startTotal, ...actual.map((p) => p[1])));
|
|
152
|
+
const x = (t) => pad.l + ((t - t0) / (t1 - t0)) * iw;
|
|
153
|
+
const y = (v) => pad.t + ih - (v / yMax) * ih;
|
|
154
|
+
|
|
155
|
+
const svg = el('svg', { viewBox: `0 0 ${W} ${H}`, role: 'img', 'aria-label': o.label });
|
|
156
|
+
const grid = el('g', { class: 'grid' }, svg), axis = el('g', { class: 'axis' }, svg);
|
|
157
|
+
for (let v = 0; v <= yMax + 0.01; v += step) {
|
|
158
|
+
el('line', { x1: pad.l, x2: W - pad.r, y1: y(v), y2: y(v) }, grid);
|
|
159
|
+
el('text', { x: pad.l - 8, y: y(v) + 4, 'text-anchor': 'end' }, axis).textContent = money(v);
|
|
160
|
+
}
|
|
161
|
+
const months = o.baseline.months.filter((m) => monthEnd(m.ym) <= t1);
|
|
162
|
+
const every = Math.max(1, Math.ceil(months.length / Math.max(1, Math.floor(iw / 76))));
|
|
163
|
+
months.forEach((m, i) => { if (i % every === 0) el('text', { x: x(monthEnd(m.ym)), y: H - 6, 'text-anchor': 'middle' }, axis).textContent = monthName(m.ym, true); });
|
|
164
|
+
|
|
165
|
+
const shown = plan.filter(([t]) => t <= t1);
|
|
166
|
+
el('path', { class: 'line-baseline', d: shown.map(([t, v], i) => `${i ? 'L' : 'M'}${x(t).toFixed(1)},${y(v).toFixed(1)}`).join('') }, svg);
|
|
167
|
+
if (actual.length > 1) el('path', { class: 'line-actual', d: actual.map(([t, v], i) => `${i ? 'L' : 'M'}${x(t).toFixed(1)},${y(v).toFixed(1)}`).join('') }, svg);
|
|
168
|
+
o.points.forEach((p) => {
|
|
169
|
+
el('circle', { class: `dot-actual ${p.status.replace(' ', '-')}`, cx: x(day(p.date)), cy: y(p.actual), r: 4.5 }, svg)
|
|
170
|
+
.appendChild(el('title')).textContent = `${p.date}: ${money(p.actual)} (plan ${money(p.planned)})`;
|
|
171
|
+
});
|
|
172
|
+
box.replaceChildren(svg);
|
|
173
|
+
}
|