Skip to content

Commit

Permalink
feat(csv-parse): skip_line_with_errors used with raw print current bu…
Browse files Browse the repository at this point in the history
…ffer (fix #292)
  • Loading branch information
wdavidw committed Oct 21, 2021
1 parent 2c2623f commit 2741990
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 40 deletions.
7 changes: 4 additions & 3 deletions packages/csv-parse/dist/cjs/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(){
Expand Down
7 changes: 4 additions & 3 deletions packages/csv-parse/dist/cjs/sync.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(){
Expand Down
7 changes: 4 additions & 3 deletions packages/csv-parse/dist/esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(){
Expand Down
7 changes: 4 additions & 3 deletions packages/csv-parse/dist/esm/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(){
Expand Down
7 changes: 4 additions & 3 deletions packages/csv-parse/dist/iife/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(){
Expand Down
7 changes: 4 additions & 3 deletions packages/csv-parse/dist/iife/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(){
Expand Down
7 changes: 4 additions & 3 deletions packages/csv-parse/dist/umd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(){
Expand Down
7 changes: 4 additions & 3 deletions packages/csv-parse/dist/umd/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(){
Expand Down
7 changes: 4 additions & 3 deletions packages/csv-parse/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(){
Expand Down
4 changes: 2 additions & 2 deletions packages/csv-parse/samples/option.cast.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
} ]
]);
9 changes: 5 additions & 4 deletions packages/csv-parse/samples/option.info.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' ]
}]);
6 changes: 3 additions & 3 deletions packages/csv-parse/test/option.cast.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/csv-parse/test/option.info.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions packages/csv-parse/test/option.on_record.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()
21 changes: 21 additions & 0 deletions packages/csv-parse/test/option.skip_lines_with_error.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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++

0 comments on commit 2741990

Please sign in to comment.