Budgets from your full months, the middle value per category, and say which months
A half month at the start of your history or one unusual month no longer sets the budget. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c3276ab5c1
commit
37a97fe20a
6 files changed
+40
-15
No files matched your search
@@ -60,3 +60,11 @@ console.log("budget: ok");
|
||||
]);
|
||||
assert.deepEqual(budgetChanges({ Coffee: null }, cats, {}), []); // nothing to remove
|
||||
}
|
||||
|
||||
// The usual month: one big month doesn't set the budget.
|
||||
import { median as med, monthList as ml } from "./budget.ts";
|
||||
assert.equal(med([120, 900, 140]), 140);
|
||||
assert.equal(med([100, 200]), 150);
|
||||
assert.equal(med([]), 0);
|
||||
assert.equal(ml(["2026-10", "2026-08", "2026-09"]), "August, September and October");
|
||||
assert.equal(ml(["2026-10"]), "October");
|
||||
@@ -6,6 +6,19 @@ export const step = (n: number) => (n < 200 ? 10 : n < 1000 ? 25 : 50);
|
||||
// A suggested budget: the average rounded up to the next step.
|
||||
export const roundUp = (n: number) => (n > 0 ? Math.ceil(n / step(n)) * step(n) : 0);
|
||||
|
||||
// The middle value: one unusual month (a holiday, a big repair) doesn't set the budget. Averages the middle two when even.
|
||||
export function median(ns: number[]) {
|
||||
if (!ns.length) return 0;
|
||||
const s = [...ns].sort((a, b) => a - b), mid = Math.floor(s.length / 2);
|
||||
return s.length % 2 ? s[mid] : (s[mid - 1] + s[mid]) / 2;
|
||||
}
|
||||
|
||||
// "August, September and October" from ["2026-10", "2026-08", "2026-09"].
|
||||
export function monthList(months: string[]) {
|
||||
const names = [...months].sort().map((m) => new Date(`${m}-01T00:00:00Z`).toLocaleDateString("en-US", { month: "long", timeZone: "UTC" }));
|
||||
return names.length <= 1 ? names.join("") : `${names.slice(0, -1).join(", ")} and ${names.at(-1)}`;
|
||||
}
|
||||
|
||||
// A suggested budget for a category: the 3-month average, but never below what is already spent this month, rounded up.
|
||||
// "Use these" never puts the user over budget on day one.
|
||||
export const suggest = (avg: number, spentThisMonth: number) => roundUp(Math.max(avg, spentThisMonth));
|
||||
|
||||
@@ -86,7 +86,7 @@ const rows = [
|
||||
{ month: "2026-09-01", income: 3240, spend: 1800 },
|
||||
{ month: "2026-10-01", income: 3240, spend: 7000 },
|
||||
];
|
||||
assert.deepEqual(typicalMonth(rows, "2026-10-23", "2026-07-30"), { income: 3240, spend: 1000, months: 2 });
|
||||
assert.deepEqual(typicalMonth(rows, "2026-10-23", "2026-07-30"), { income: 3240, spend: 1000, months: 2, list: ["2026-09", "2026-08"] }, "the partial July is left out");
|
||||
assert.equal(typicalMonth(rows, "2026-10-23", "2026-06-01")!.income, (810 + 3240 + 3240) / 3, "3 full months");
|
||||
// The e2e user: history from Jul 30 but only cents of interest until October's paychecks. Not $0.12 a month:
|
||||
// October so far (2,835.92 over 23 days) scaled to a month.
|
||||
|
||||
+2
-2
@@ -39,7 +39,7 @@ export function typicalMonth(rows: { month: string; income: number; spend: numbe
|
||||
const of = (months: string[], f: (r: (typeof rows)[number]) => number) => sum(rows.filter((r) => months.includes(r.month.slice(0, 7))).map(f));
|
||||
const paid = (ym: string) => of([ym], (r) => r.income) >= 100;
|
||||
const full = [1, 2, 3].map((k) => new Date(Date.UTC(y, m - 1 - k, 1)).toISOString().slice(0, 7)).filter((ym) => `${ym}-01` >= oldest && paid(ym));
|
||||
if (full.length) return { income: of(full, (r) => r.income) / full.length, spend: of(full, (r) => r.spend) / full.length, months: full.length };
|
||||
if (full.length) return { income: of(full, (r) => r.income) / full.length, spend: of(full, (r) => r.spend) / full.length, months: full.length, list: full };
|
||||
const seen = [...new Set(rows.map((r) => r.month.slice(0, 7)))].sort();
|
||||
const first = seen.find(paid);
|
||||
if (!first) return null;
|
||||
@@ -48,7 +48,7 @@ export function typicalMonth(rows: { month: string; income: number; spend: numbe
|
||||
if (days < 14) return null;
|
||||
const since = seen.filter((ym) => ym >= start.slice(0, 7));
|
||||
const scale = 365 / 12 / days;
|
||||
return { income: of(since, (r) => r.income) * scale, spend: of(since, (r) => r.spend) * scale, months: 0 };
|
||||
return { income: of(since, (r) => r.income) * scale, spend: of(since, (r) => r.spend) * scale, months: 0, list: [] as string[] };
|
||||
}
|
||||
|
||||
// The take-home pay typed in onboarding (profile.pay), as a month. Used only when no paycheck is found, never added to one.
|
||||
|
||||
Reference in new issue
Block a user