From 4b3541b6eba878bb4e8a8b818abe53b1efb471a1 Mon Sep 17 00:00:00 2001 From: Wade Bourne Date: Tue, 29 Nov 2022 12:46:04 -0800 Subject: [PATCH] v1.1.0 - Add support for retrieving parsed headers directly. --- package.json | 2 +- src/__tests__/index.test.ts | 52 ++++++++++++++++++++++++++++++------- src/index.ts | 10 ++++++- 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 6271c61..db3a0ba 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts index 66bece5..8cf78f7 100644 --- a/src/__tests__/index.test.ts +++ b/src/__tests__/index.test.ts @@ -7,6 +7,7 @@ interface CSVParserTest { headers?: Array; chunks: Array; expected: Array>; + expectedHeaders?: Array; } describe('SimpleCSVParser', () => { @@ -105,17 +106,50 @@ describe('SimpleCSVParser', () => { { a: '5', b: '6', c: '7', d: '8' }, ], }, - ])('$name', ({ chunks, expected, headers }) => { - const results: Array> = []; + { + 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> = []; - 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); + } + }, + ); }); diff --git a/src/index.ts b/src/index.ts index 40c20ec..ed7f525 100644 --- a/src/index.ts +++ b/src/index.ts @@ -118,7 +118,7 @@ export class CSVParser { * @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; } @@ -130,6 +130,14 @@ export class CSVParser { } } + /** + * 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) {