From 6c5cb5efb333ed0f6e3f64d2a8cddb2d4f813521 Mon Sep 17 00:00:00 2001 From: David Worms Date: Mon, 13 May 2024 17:49:12 +0200 Subject: [PATCH] feat(csv-issues-esm): issue 411 reproductible attempt --- demo/issues-esm/lib/411.csv | 10 ++++++++++ demo/issues-esm/lib/411.js | 27 +++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 demo/issues-esm/lib/411.csv create mode 100644 demo/issues-esm/lib/411.js diff --git a/demo/issues-esm/lib/411.csv b/demo/issues-esm/lib/411.csv new file mode 100644 index 00000000..f560e243 --- /dev/null +++ b/demo/issues-esm/lib/411.csv @@ -0,0 +1,10 @@ +id,amount,timestamp +1,$33.60,05/26/2022 +2,$91.47,08/24/2022 +3,$88.70,06/15/2022 +4,$46.97,06/11/2022,faulty +5,$93.78,05/05/2022 +6,$56.80,05/07/2022 +7,$3.54,05/12/2022 +8,$57.59,07/05/2022 +9,$76.49,05/02/2022 diff --git a/demo/issues-esm/lib/411.js b/demo/issues-esm/lib/411.js new file mode 100644 index 00000000..374b93ee --- /dev/null +++ b/demo/issues-esm/lib/411.js @@ -0,0 +1,27 @@ +import assert from 'node:assert'; +import { createReadStream } from 'node:fs'; +import { Writable } from 'node:stream' +import { finished } from 'node:stream/promises'; +import desm from "desm"; +import { parse } from 'csv-parse'; + +const __dirname = desm(import.meta.url); +const errors = [] + +const parser = parse({ + bom: true, + skipRecordsWithError: true, +}); +// Create a stream and consume its source +const sink = new Writable ({objectMode: true, write: (_, __, callback) => callback()}) +const outStream = createReadStream(`${__dirname}/411.csv`).pipe(parser).pipe(sink); +// Catch records with errors +parser.on('skip', (e) => { + errors.push(e); +}); +// Wait for stream to be consumed +await finished(outStream); +// Catch error from skip event +assert.deepStrictEqual(errors.map(e => e.message), [ + 'Invalid Record Length: expect 3, got 4 on line 5' +])