Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions __tests__/lib/csv-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,20 @@ describe("parseCsvBatch", () => {
});
});

describe("UTF-8 BOM handling", () => {
it("parses CSV with a leading UTF-8 BOM identically to one without", () => {
const csvWithoutBom = `${HEADER}\n${makeRow()}`;
const csvWithBom = `\uFEFF${csvWithoutBom}`;
const resultWithout = parseCsvBatch(csvWithoutBom);
const resultWith = parseCsvBatch(csvWithBom);

expect(resultWith.errors).toHaveLength(0);
expect(resultWith.rows).toHaveLength(1);
expect(resultWith.rows[0].recipient).toBe("GABC");
expect(resultWith).toEqual(resultWithout);
});
});

describe("quoted fields", () => {
it("handles quoted values containing commas", () => {
const csv = [HEADER, '"GABC,rest",1000,1000,2000'].join("\n");
Expand Down Expand Up @@ -228,4 +242,5 @@ describe("parseDuration", () => {
expect(parseDuration("1.5d")).toBeNull();
expect(parseDuration("-5d")).toBeNull();
});

});
1 change: 1 addition & 0 deletions lib/csv-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export function parseCsvBatch(
customColumnMapping?: Record<string, number>,
): CsvParseResult {
const lines = csvText
.replace(/^\uFEFF/, "")
.replace(/\r\n/g, "\n")
.split("\n")
.map((line) => line.trim())
Expand Down
Loading