Skip to content

A personal agent with a browser, terminal, files, and work that keeps going built with CopilotKit and AG-UI.

TypeScript

fix: accept a byte order mark before the transaction CSV header (#146)

Charan Rathored344dc8c3511
diff --git a/apps/server/src/engine/finance.ts b/apps/server/src/engine/finance.ts
index b42d1fa..f7704ee 100644
--- a/apps/server/src/engine/finance.ts
+++ b/apps/server/src/engine/finance.ts
@@ -3,6 +3,8 @@ import { z } from "zod";
/** CSV amounts use positive expenses and negative income. No currency conversion is inferred. */
export function analyzeSpending(csv: string) {
if (csv.length > 500000) throw new Error("Import at most 500 KB of transaction CSV");
+ // Excel and many bank exports start a UTF-8 CSV with a byte order mark. It is not part of the first header.
+ if (csv.charCodeAt(0) === 0xfeff) csv = csv.slice(1);
const rows: string[][] = [];
let row: string[] = [],
cell = "",
diff --git a/tests/finance.test.ts b/tests/finance.test.ts
index 94ae732..e26e121 100644
--- a/tests/finance.test.ts
+++ b/tests/finance.test.ts
@@ -30,3 +30,14 @@ test("transaction imports preserve valid quoted fields at CSV boundaries", () =>
assert.equal(report.transactions[1].category, "Uncategorized");
}
});
+
+test("transaction imports accept a UTF-8 byte order mark before the header", () => {
+ for (const header of [
+ "date,description,amount,category",
+ '"date","description","amount","category"',
+ ]) {
+ const report = analyzeSpending(`\uFEFF${header}\n2026-09-01,Coffee,10.10,Food`);
+ assert.equal(report.count, 1);
+ assert.equal(report.spending, 10.1);
+ }
+});