Skip to content

Commit

Permalink
fix(csv): add quoting/newline support in header fields
Browse files Browse the repository at this point in the history
- add tests
  • Loading branch information
postspectacular committed Nov 17, 2020
1 parent 93d79ec commit 28cac18
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
17 changes: 13 additions & 4 deletions packages/csv/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,19 @@ export function parseCSV(opts?: Partial<CSVOpts>, src?: Iterable<string>): any {
record = [];
return reduce(acc, row);
} else {
const names = line.split(delim);
cols && (index = initIndex(names, cols));
all && (revIndex = initRevIndex(names));
first = false;
isQuoted = tokenizeLine(
line,
record,
isQuoted,
delim,
quote
);
if (!isQuoted) {
cols && (index = initIndex(record, cols));
all && (revIndex = initRevIndex(record));
first = false;
record = [];
}
return acc;
}
});
Expand Down
21 changes: 14 additions & 7 deletions packages/csv/test/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from "assert";
import { parseCSV } from "../src";
import { parseCSV, parseCSVString } from "../src";

describe("csv", () => {
it("header", () => {
Expand Down Expand Up @@ -32,16 +32,23 @@ describe("csv", () => {

it("quotes", () => {
assert.deepStrictEqual(
[
...parseCSV(
{},
`a,b,c\n"ha ""he""\nho","2,",3\n4,,6`.split("\n")
),
],
[...parseCSVString({}, `a,b,c\n"ha ""he""\nho","2,",3\n4,,6`)],
[
{ a: `ha "he"\nho`, b: "2,", c: "3" },
{ a: "4", b: "", c: "6" },
]
);
});

it("quotes in header", () => {
assert.deepStrictEqual(
[
...parseCSVString(
{},
`"foo","bar\nbaz","fin,\n#ignore"\n#ignore2\n1,2,3\n`
),
],
[{ foo: "1", "bar\nbaz": "2", "fin,\n#ignore": "3" }]
);
});
});

0 comments on commit 28cac18

Please sign in to comment.