Skip to content

Commit

Permalink
v1.1.0
Browse files Browse the repository at this point in the history
- Add support for retrieving parsed headers directly.
  • Loading branch information
wbourne0 committed Nov 29, 2022
1 parent ef1ea8b commit 4b3541b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "incremental-csv-parser",
"version": "1.0.2",
"version": "1.1.0",
"description": "A simple, browser compatible, incremental CSV parser.",
"main": "dist/cjs/index.js",
"repository": "https://github.com/AllAwesome497/incremental-csv-parser",
Expand Down
52 changes: 43 additions & 9 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ interface CSVParserTest {
headers?: Array<string>;
chunks: Array<string>;
expected: Array<Record<string, string>>;
expectedHeaders?: Array<string>;
}

describe('SimpleCSVParser', () => {
Expand Down Expand Up @@ -105,17 +106,50 @@ describe('SimpleCSVParser', () => {
{ a: '5', b: '6', c: '7', d: '8' },
],
},
])('$name', ({ chunks, expected, headers }) => {
const results: Array<Record<string, string>> = [];
{
name: 'headers no data',
chunks: ['a,b,c,d\n'],
expected: [],
expectedHeaders: ['a', 'b', 'c', 'd'],
},
{
name: 'headers no data (no newline)',
chunks: ['a,b,c,d'],
expected: [],
expectedHeaders: ['a', 'b', 'c', 'd'],
},
{
name: 'manually defined headers',
chunks: [],
expected: [],
expectedHeaders: ['a', 'b', 'c'],
headers: ['a', 'b', 'c'],
}
])(
'$name',
({
chunks,
expected,
expectedHeaders = expected.length > 0
? Object.keys(expected[0])
: undefined,
headers,
}) => {
const results: Array<Record<string, string>> = [];

const parser = new CSVParser((data) => results.push(data), headers);
const parser = new CSVParser((data) => results.push(data), headers);

for (const chunk of chunks) {
parser.process(chunk);
}
for (const chunk of chunks) {
parser.process(chunk);
}

parser.flush();
parser.flush();

expect(results).toEqual(expected);
});
expect(results).toEqual(expected);

if (expectedHeaders) {
expect(parser.headers()).toEqual(expectedHeaders);
}
},
);
});
10 changes: 9 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export class CSVParser<K extends string> {
* @throws Error if the stream did not end in a valid row.
*/
flush() {
if ((!this._value && this._row.length === 0) || !this._headers) {
if (!this._value && this._row.length === 0) {
// clean exit
return;
}
Expand All @@ -130,6 +130,14 @@ export class CSVParser<K extends string> {
}
}

/**
* Gets headers parsed for the csv.
* @returns Headers for the csv (or undefined if they haven't been parsed yet)
*/
headers() {
return this._headers;
}

protected _finalizeValue(rowEnd?: boolean) {
// empty line
if (!this._row.length && rowEnd && !this._value) {
Expand Down

0 comments on commit 4b3541b

Please sign in to comment.