From 27419908b9ce5319307bb6647335d5c07cd1e3a4 Mon Sep 17 00:00:00 2001 From: David Worms Date: Thu, 21 Oct 2021 20:40:23 +0200 Subject: [PATCH] feat(csv-parse): skip_line_with_errors used with raw print current buffer (fix #292) --- packages/csv-parse/dist/cjs/index.cjs | 7 ++++--- packages/csv-parse/dist/cjs/sync.cjs | 7 ++++--- packages/csv-parse/dist/esm/index.js | 7 ++++--- packages/csv-parse/dist/esm/sync.js | 7 ++++--- packages/csv-parse/dist/iife/index.js | 7 ++++--- packages/csv-parse/dist/iife/sync.js | 7 ++++--- packages/csv-parse/dist/umd/index.js | 7 ++++--- packages/csv-parse/dist/umd/sync.js | 7 ++++--- packages/csv-parse/lib/index.js | 7 ++++--- packages/csv-parse/samples/option.cast.js | 4 ++-- packages/csv-parse/samples/option.info.js | 9 ++++---- packages/csv-parse/test/option.cast.coffee | 6 +++--- packages/csv-parse/test/option.info.coffee | 2 +- .../csv-parse/test/option.on_record.coffee | 6 +++--- .../test/option.skip_lines_with_error.coffee | 21 +++++++++++++++++++ 15 files changed, 71 insertions(+), 40 deletions(-) diff --git a/packages/csv-parse/dist/cjs/index.cjs b/packages/csv-parse/dist/cjs/index.cjs index a901ac8f1..1fb27b4a4 100644 --- a/packages/csv-parse/dist/cjs/index.cjs +++ b/packages/csv-parse/dist/cjs/index.cjs @@ -6172,11 +6172,11 @@ class Parser extends Transform { return 0; } __error(msg){ - const {skip_lines_with_error} = this.options; + const {encoding, raw, skip_lines_with_error} = this.options; const err = typeof msg === 'string' ? new Error(msg) : msg; if(skip_lines_with_error){ this.state.recordHasError = true; - this.emit('skip', err); + this.emit('skip', err, raw ? this.state.rawBuffer.toString(encoding) : undefined); return undefined; }else { return err; @@ -6189,12 +6189,13 @@ class Parser extends Transform { }; } __infoRecord(){ - const {columns} = this.options; + const {columns, raw, encoding} = this.options; return { ...this.__infoDataSet(), error: this.state.error, header: columns === true, index: this.state.record.length, + raw: raw ? this.state.rawBuffer.toString(encoding) : undefined }; } __infoField(){ diff --git a/packages/csv-parse/dist/cjs/sync.cjs b/packages/csv-parse/dist/cjs/sync.cjs index 2b7aac625..4b050fcd4 100644 --- a/packages/csv-parse/dist/cjs/sync.cjs +++ b/packages/csv-parse/dist/cjs/sync.cjs @@ -6172,11 +6172,11 @@ class Parser extends Transform { return 0; } __error(msg){ - const {skip_lines_with_error} = this.options; + const {encoding, raw, skip_lines_with_error} = this.options; const err = typeof msg === 'string' ? new Error(msg) : msg; if(skip_lines_with_error){ this.state.recordHasError = true; - this.emit('skip', err); + this.emit('skip', err, raw ? this.state.rawBuffer.toString(encoding) : undefined); return undefined; }else { return err; @@ -6189,12 +6189,13 @@ class Parser extends Transform { }; } __infoRecord(){ - const {columns} = this.options; + const {columns, raw, encoding} = this.options; return { ...this.__infoDataSet(), error: this.state.error, header: columns === true, index: this.state.record.length, + raw: raw ? this.state.rawBuffer.toString(encoding) : undefined }; } __infoField(){ diff --git a/packages/csv-parse/dist/esm/index.js b/packages/csv-parse/dist/esm/index.js index 3b121f026..6bf2bfcae 100644 --- a/packages/csv-parse/dist/esm/index.js +++ b/packages/csv-parse/dist/esm/index.js @@ -6168,11 +6168,11 @@ class Parser extends Transform { return 0; } __error(msg){ - const {skip_lines_with_error} = this.options; + const {encoding, raw, skip_lines_with_error} = this.options; const err = typeof msg === 'string' ? new Error(msg) : msg; if(skip_lines_with_error){ this.state.recordHasError = true; - this.emit('skip', err); + this.emit('skip', err, raw ? this.state.rawBuffer.toString(encoding) : undefined); return undefined; }else { return err; @@ -6185,12 +6185,13 @@ class Parser extends Transform { }; } __infoRecord(){ - const {columns} = this.options; + const {columns, raw, encoding} = this.options; return { ...this.__infoDataSet(), error: this.state.error, header: columns === true, index: this.state.record.length, + raw: raw ? this.state.rawBuffer.toString(encoding) : undefined }; } __infoField(){ diff --git a/packages/csv-parse/dist/esm/sync.js b/packages/csv-parse/dist/esm/sync.js index 5784db817..1f7af4ca1 100644 --- a/packages/csv-parse/dist/esm/sync.js +++ b/packages/csv-parse/dist/esm/sync.js @@ -6168,11 +6168,11 @@ class Parser extends Transform { return 0; } __error(msg){ - const {skip_lines_with_error} = this.options; + const {encoding, raw, skip_lines_with_error} = this.options; const err = typeof msg === 'string' ? new Error(msg) : msg; if(skip_lines_with_error){ this.state.recordHasError = true; - this.emit('skip', err); + this.emit('skip', err, raw ? this.state.rawBuffer.toString(encoding) : undefined); return undefined; }else { return err; @@ -6185,12 +6185,13 @@ class Parser extends Transform { }; } __infoRecord(){ - const {columns} = this.options; + const {columns, raw, encoding} = this.options; return { ...this.__infoDataSet(), error: this.state.error, header: columns === true, index: this.state.record.length, + raw: raw ? this.state.rawBuffer.toString(encoding) : undefined }; } __infoField(){ diff --git a/packages/csv-parse/dist/iife/index.js b/packages/csv-parse/dist/iife/index.js index f35f4c182..e4b1d8348 100644 --- a/packages/csv-parse/dist/iife/index.js +++ b/packages/csv-parse/dist/iife/index.js @@ -6171,11 +6171,11 @@ var csv_parse = (function (exports) { return 0; } __error(msg){ - const {skip_lines_with_error} = this.options; + const {encoding, raw, skip_lines_with_error} = this.options; const err = typeof msg === 'string' ? new Error(msg) : msg; if(skip_lines_with_error){ this.state.recordHasError = true; - this.emit('skip', err); + this.emit('skip', err, raw ? this.state.rawBuffer.toString(encoding) : undefined); return undefined; }else { return err; @@ -6188,12 +6188,13 @@ var csv_parse = (function (exports) { }; } __infoRecord(){ - const {columns} = this.options; + const {columns, raw, encoding} = this.options; return { ...this.__infoDataSet(), error: this.state.error, header: columns === true, index: this.state.record.length, + raw: raw ? this.state.rawBuffer.toString(encoding) : undefined }; } __infoField(){ diff --git a/packages/csv-parse/dist/iife/sync.js b/packages/csv-parse/dist/iife/sync.js index f1b7e7143..3e3e6a7b9 100644 --- a/packages/csv-parse/dist/iife/sync.js +++ b/packages/csv-parse/dist/iife/sync.js @@ -6171,11 +6171,11 @@ var csv_parse_sync = (function (exports) { return 0; } __error(msg){ - const {skip_lines_with_error} = this.options; + const {encoding, raw, skip_lines_with_error} = this.options; const err = typeof msg === 'string' ? new Error(msg) : msg; if(skip_lines_with_error){ this.state.recordHasError = true; - this.emit('skip', err); + this.emit('skip', err, raw ? this.state.rawBuffer.toString(encoding) : undefined); return undefined; }else { return err; @@ -6188,12 +6188,13 @@ var csv_parse_sync = (function (exports) { }; } __infoRecord(){ - const {columns} = this.options; + const {columns, raw, encoding} = this.options; return { ...this.__infoDataSet(), error: this.state.error, header: columns === true, index: this.state.record.length, + raw: raw ? this.state.rawBuffer.toString(encoding) : undefined }; } __infoField(){ diff --git a/packages/csv-parse/dist/umd/index.js b/packages/csv-parse/dist/umd/index.js index 375d5f9ab..f0187dc77 100644 --- a/packages/csv-parse/dist/umd/index.js +++ b/packages/csv-parse/dist/umd/index.js @@ -6174,11 +6174,11 @@ return 0; } __error(msg){ - const {skip_lines_with_error} = this.options; + const {encoding, raw, skip_lines_with_error} = this.options; const err = typeof msg === 'string' ? new Error(msg) : msg; if(skip_lines_with_error){ this.state.recordHasError = true; - this.emit('skip', err); + this.emit('skip', err, raw ? this.state.rawBuffer.toString(encoding) : undefined); return undefined; }else { return err; @@ -6191,12 +6191,13 @@ }; } __infoRecord(){ - const {columns} = this.options; + const {columns, raw, encoding} = this.options; return { ...this.__infoDataSet(), error: this.state.error, header: columns === true, index: this.state.record.length, + raw: raw ? this.state.rawBuffer.toString(encoding) : undefined }; } __infoField(){ diff --git a/packages/csv-parse/dist/umd/sync.js b/packages/csv-parse/dist/umd/sync.js index 89b410fb8..d00366415 100644 --- a/packages/csv-parse/dist/umd/sync.js +++ b/packages/csv-parse/dist/umd/sync.js @@ -6174,11 +6174,11 @@ return 0; } __error(msg){ - const {skip_lines_with_error} = this.options; + const {encoding, raw, skip_lines_with_error} = this.options; const err = typeof msg === 'string' ? new Error(msg) : msg; if(skip_lines_with_error){ this.state.recordHasError = true; - this.emit('skip', err); + this.emit('skip', err, raw ? this.state.rawBuffer.toString(encoding) : undefined); return undefined; }else { return err; @@ -6191,12 +6191,13 @@ }; } __infoRecord(){ - const {columns} = this.options; + const {columns, raw, encoding} = this.options; return { ...this.__infoDataSet(), error: this.state.error, header: columns === true, index: this.state.record.length, + raw: raw ? this.state.rawBuffer.toString(encoding) : undefined }; } __infoField(){ diff --git a/packages/csv-parse/lib/index.js b/packages/csv-parse/lib/index.js index a0d33a458..9cdbb66cb 100644 --- a/packages/csv-parse/lib/index.js +++ b/packages/csv-parse/lib/index.js @@ -1167,11 +1167,11 @@ class Parser extends Transform { return 0; } __error(msg){ - const {skip_lines_with_error} = this.options; + const {encoding, raw, skip_lines_with_error} = this.options; const err = typeof msg === 'string' ? new Error(msg) : msg; if(skip_lines_with_error){ this.state.recordHasError = true; - this.emit('skip', err); + this.emit('skip', err, raw ? this.state.rawBuffer.toString(encoding) : undefined); return undefined; }else{ return err; @@ -1184,12 +1184,13 @@ class Parser extends Transform { }; } __infoRecord(){ - const {columns} = this.options; + const {columns, raw, encoding} = this.options; return { ...this.__infoDataSet(), error: this.state.error, header: columns === true, index: this.state.record.length, + raw: raw ? this.state.rawBuffer.toString(encoding) : undefined }; } __infoField(){ diff --git a/packages/csv-parse/samples/option.cast.js b/packages/csv-parse/samples/option.cast.js index 6eafcd6d8..c6484c153 100644 --- a/packages/csv-parse/samples/option.cast.js +++ b/packages/csv-parse/samples/option.cast.js @@ -26,11 +26,11 @@ assert.deepStrictEqual(records, [ [ '2000-01-01T05:00:00.000Z', { bytes: 16, comment_lines: 0, empty_lines: 0, invalid_field_length: 0, lines: 1, records: 0, columns: false, error: undefined, header: false, - index: 1, column: 1, quoting: false + index: 1, column: 1, quoting: false, raw: undefined } ], [ '2050-11-27T05:00:00.000Z', { bytes: 35, comment_lines: 0, empty_lines: 0, invalid_field_length: 0, lines: 2, records: 1, columns: false, error: undefined, header: false, - index: 1, column: 1, quoting: false + index: 1, column: 1, quoting: false, raw: undefined } ] ]); diff --git a/packages/csv-parse/samples/option.info.js b/packages/csv-parse/samples/option.info.js index 1ba68eb3d..99d87bfa8 100644 --- a/packages/csv-parse/samples/option.info.js +++ b/packages/csv-parse/samples/option.info.js @@ -9,15 +9,16 @@ const records = parse(data, { assert.deepStrictEqual(records, [{ info: { bytes: 5, + columns: false, comment_lines: 0, empty_lines: 0, + error: undefined, + header: false, + index: 3, invalid_field_length: 0, lines: 1, + raw: undefined, records: 1, - columns: false, - error: undefined, - header: false, - index: 3 }, record: [ 'a', 'b', 'c' ] }]); diff --git a/packages/csv-parse/test/option.cast.coffee b/packages/csv-parse/test/option.cast.coffee index 8096753d2..e6f7519c1 100644 --- a/packages/csv-parse/test/option.cast.coffee +++ b/packages/csv-parse/test/option.cast.coffee @@ -68,7 +68,7 @@ describe 'Option `cast`', -> 'bytes', 'column', 'columns', 'comment_lines', 'empty_lines', 'error', 'header', 'index', 'invalid_field_length', 'lines', 'quoting', - 'records' + 'raw', 'records' ]] ] unless err next err @@ -88,13 +88,13 @@ describe 'Option `cast`', -> bytes: 16, column: 1, columns: false, comment_lines: 0, empty_lines: 0, error: undefined, header: false, index: 1, invalid_field_length: 0, lines: 1, - quoting: false, records: 0 + quoting: false, raw: undefined, records: 0 } ] [ '2050-11-27T05:00:00.000Z', { bytes: 33, column: 1, columns: false, comment_lines: 0, empty_lines: 0, error: undefined, header: false, index: 1, invalid_field_length: 0, lines: 2, - quoting: false, records: 1 + quoting: false, raw: undefined, records: 1 } ] ] unless err next err diff --git a/packages/csv-parse/test/option.info.coffee b/packages/csv-parse/test/option.info.coffee index 7941ccb7c..3ab2fbde7 100644 --- a/packages/csv-parse/test/option.info.coffee +++ b/packages/csv-parse/test/option.info.coffee @@ -31,7 +31,7 @@ describe 'Option `info`', -> Object.keys(info).sort().should.eql [ 'bytes', 'columns', 'comment_lines', 'empty_lines', 'error', 'header', - 'index', 'invalid_field_length', 'lines', 'records' + 'index', 'invalid_field_length', 'lines', 'raw', 'records' ] next err diff --git a/packages/csv-parse/test/option.on_record.coffee b/packages/csv-parse/test/option.on_record.coffee index 11bc26520..2c35c2d0e 100644 --- a/packages/csv-parse/test/option.on_record.coffee +++ b/packages/csv-parse/test/option.on_record.coffee @@ -63,7 +63,7 @@ describe 'Option `on_record`', -> records.should.eql [[ 'bytes', 'columns', 'comment_lines', 'empty_lines', 'error', 'header', - 'index', 'invalid_field_length', 'lines', 'records' + 'index', 'invalid_field_length', 'lines', 'raw', 'records' ]] next() @@ -76,10 +76,10 @@ describe 'Option `on_record`', -> records.should.eql [ bytes: 4, columns: false, comment_lines: 0, empty_lines: 0, error: undefined, header: false - index: 2, invalid_field_length: 0, lines: 1, records: 1 + index: 2, invalid_field_length: 0, lines: 1, raw: undefined, records: 1 , bytes: 7, columns: false, comment_lines: 0, empty_lines: 0, error: undefined, header: false - index: 2, invalid_field_length: 0, lines: 2, records: 2 + index: 2, invalid_field_length: 0, lines: 2, raw: undefined, records: 2 ] next() diff --git a/packages/csv-parse/test/option.skip_lines_with_error.coffee b/packages/csv-parse/test/option.skip_lines_with_error.coffee index e554f18e2..a98fe3e7b 100644 --- a/packages/csv-parse/test/option.skip_lines_with_error.coffee +++ b/packages/csv-parse/test/option.skip_lines_with_error.coffee @@ -129,3 +129,24 @@ describe 'Option `skip_lines_with_error`', -> e,f,g,h ''' parser.end() + + describe 'with `raw` option', -> + + it 'print raw record', (next) -> + errors = 0 + parser = parse ''' + "a","b","c" + "one","two","three" + "four"," " ","six" + "seven","eight","nine" + ''' + , + skip_lines_with_error: true + raw: true + , (err, data) -> + errors.should.eql 1 + next err + parser.on 'skip', (err, raw) -> + err.raw.should.eql '"four"," "' + raw.should.eql '"four"," "' + errors++