-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(csv-parse): skip event not raised with bom (fix #411)
- Loading branch information
Showing
4 changed files
with
52 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
id,first_name,last_name,email,modified_at | ||
id,first_name,last_name,email,modified_at | ||
1,Ring,Grinyov,[email protected],2022-02-14 | ||
2,Kylie,Lauderdale,[email protected],2022-02-14, | ||
3,Cammi,Bendix,[email protected],2022-02-14 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,27 @@ | ||
import path from 'path'; | ||
import { pipeline } from 'stream/promises'; | ||
import { parse as parseCSV } from 'csv-parse'; | ||
import { Writable } from 'stream'; | ||
import { createReadStream } from 'fs'; | ||
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"; | ||
const __dirname = desm(import.meta.url); | ||
|
||
async function testRecordsSkip() { | ||
const errors = []; | ||
const records = []; | ||
|
||
const sink = new Writable({ | ||
objectMode: true, | ||
write: (_, __, callback) => { | ||
records.push(_); | ||
callback(); | ||
}, | ||
}); | ||
import { parse } from 'csv-parse'; | ||
|
||
const csvSource = createReadStream(path.join(__dirname, '411.csv')); | ||
const parser = parseCSV({ | ||
skip_records_with_error: true, | ||
bom: true, | ||
}); | ||
parser.on('skip', function (err) { | ||
errors.push(err); | ||
}); | ||
|
||
await pipeline(csvSource, parser, sink); | ||
|
||
console.log({ | ||
records, | ||
errors, | ||
}); | ||
} | ||
const __dirname = desm(import.meta.url); | ||
const errors = [] | ||
|
||
testRecordsSkip().catch(console.error); | ||
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 5, got 6 on line 3' | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters