Skip to content

Commit 4b3541b

Browse files
committed
v1.1.0
- Add support for retrieving parsed headers directly.
1 parent ef1ea8b commit 4b3541b

File tree

3 files changed

+53
-11
lines changed

3 files changed

+53
-11
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "incremental-csv-parser",
3-
"version": "1.0.2",
3+
"version": "1.1.0",
44
"description": "A simple, browser compatible, incremental CSV parser.",
55
"main": "dist/cjs/index.js",
66
"repository": "https://github.com/AllAwesome497/incremental-csv-parser",

src/__tests__/index.test.ts

+43-9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ interface CSVParserTest {
77
headers?: Array<string>;
88
chunks: Array<string>;
99
expected: Array<Record<string, string>>;
10+
expectedHeaders?: Array<string>;
1011
}
1112

1213
describe('SimpleCSVParser', () => {
@@ -105,17 +106,50 @@ describe('SimpleCSVParser', () => {
105106
{ a: '5', b: '6', c: '7', d: '8' },
106107
],
107108
},
108-
])('$name', ({ chunks, expected, headers }) => {
109-
const results: Array<Record<string, string>> = [];
109+
{
110+
name: 'headers no data',
111+
chunks: ['a,b,c,d\n'],
112+
expected: [],
113+
expectedHeaders: ['a', 'b', 'c', 'd'],
114+
},
115+
{
116+
name: 'headers no data (no newline)',
117+
chunks: ['a,b,c,d'],
118+
expected: [],
119+
expectedHeaders: ['a', 'b', 'c', 'd'],
120+
},
121+
{
122+
name: 'manually defined headers',
123+
chunks: [],
124+
expected: [],
125+
expectedHeaders: ['a', 'b', 'c'],
126+
headers: ['a', 'b', 'c'],
127+
}
128+
])(
129+
'$name',
130+
({
131+
chunks,
132+
expected,
133+
expectedHeaders = expected.length > 0
134+
? Object.keys(expected[0])
135+
: undefined,
136+
headers,
137+
}) => {
138+
const results: Array<Record<string, string>> = [];
110139

111-
const parser = new CSVParser((data) => results.push(data), headers);
140+
const parser = new CSVParser((data) => results.push(data), headers);
112141

113-
for (const chunk of chunks) {
114-
parser.process(chunk);
115-
}
142+
for (const chunk of chunks) {
143+
parser.process(chunk);
144+
}
116145

117-
parser.flush();
146+
parser.flush();
118147

119-
expect(results).toEqual(expected);
120-
});
148+
expect(results).toEqual(expected);
149+
150+
if (expectedHeaders) {
151+
expect(parser.headers()).toEqual(expectedHeaders);
152+
}
153+
},
154+
);
121155
});

src/index.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export class CSVParser<K extends string> {
118118
* @throws Error if the stream did not end in a valid row.
119119
*/
120120
flush() {
121-
if ((!this._value && this._row.length === 0) || !this._headers) {
121+
if (!this._value && this._row.length === 0) {
122122
// clean exit
123123
return;
124124
}
@@ -130,6 +130,14 @@ export class CSVParser<K extends string> {
130130
}
131131
}
132132

133+
/**
134+
* Gets headers parsed for the csv.
135+
* @returns Headers for the csv (or undefined if they haven't been parsed yet)
136+
*/
137+
headers() {
138+
return this._headers;
139+
}
140+
133141
protected _finalizeValue(rowEnd?: boolean) {
134142
// empty line
135143
if (!this._row.length && rowEnd && !this._value) {

0 commit comments

Comments
 (0)