Skip to content
This repository has been archived by the owner on Jun 28, 2021. It is now read-only.

Commit

Permalink
fix(skip_lines_with_error): work with relax_column_count (#303)
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Dec 16, 2020
1 parent 246af22 commit 92e2f65
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Please join and contribute:

## Trunk

* fix(skip_lines_with_error): work with relax_column_count (#303)
* sample: async iterator
* sample: promises

Expand Down
12 changes: 7 additions & 5 deletions lib/es5/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -896,22 +896,24 @@ var Parser = /*#__PURE__*/function (_Transform) {
}

if (recordLength !== this.state.expectedRecordLength) {
var err = columns === false ? this.__error( // Todo: rename CSV_INCONSISTENT_RECORD_LENGTH to
var err = columns === false ? // Todo: rename CSV_INCONSISTENT_RECORD_LENGTH to
// CSV_RECORD_INCONSISTENT_FIELDS_LENGTH
new CsvError('CSV_INCONSISTENT_RECORD_LENGTH', ['Invalid Record Length:', "expect ".concat(this.state.expectedRecordLength, ","), "got ".concat(recordLength, " on line ").concat(this.info.lines)], this.options, this.__context(), {
record: record
})) : this.__error( // Todo: rename CSV_RECORD_DONT_MATCH_COLUMNS_LENGTH to
}) : // Todo: rename CSV_RECORD_DONT_MATCH_COLUMNS_LENGTH to
// CSV_RECORD_INCONSISTENT_COLUMNS
new CsvError('CSV_RECORD_DONT_MATCH_COLUMNS_LENGTH', ['Invalid Record Length:', "columns length is ".concat(columns.length, ","), // rename columns
"got ".concat(recordLength, " on line ").concat(this.info.lines)], this.options, this.__context(), {
record: record
}));
});

if (relax_column_count === true || relax_column_count_less === true && recordLength < this.state.expectedRecordLength || relax_column_count_more === true && recordLength > this.state.expectedRecordLength) {
this.info.invalid_field_length++;
this.state.error = err; // Error is undefined with skip_lines_with_error
} else if (err !== undefined) {
return err;
} else {
var finalErr = this.__error(err);

if (finalErr) return finalErr;
}
}

Expand Down
45 changes: 21 additions & 24 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -747,37 +747,34 @@ class Parser extends Transform {
}
if(recordLength !== this.state.expectedRecordLength){
const err = columns === false ?
this.__error(
// Todo: rename CSV_INCONSISTENT_RECORD_LENGTH to
// CSV_RECORD_INCONSISTENT_FIELDS_LENGTH
new CsvError('CSV_INCONSISTENT_RECORD_LENGTH', [
'Invalid Record Length:',
`expect ${this.state.expectedRecordLength},`,
`got ${recordLength} on line ${this.info.lines}`,
], this.options, this.__context(), {
record: record,
})
)
// Todo: rename CSV_INCONSISTENT_RECORD_LENGTH to
// CSV_RECORD_INCONSISTENT_FIELDS_LENGTH
new CsvError('CSV_INCONSISTENT_RECORD_LENGTH', [
'Invalid Record Length:',
`expect ${this.state.expectedRecordLength},`,
`got ${recordLength} on line ${this.info.lines}`,
], this.options, this.__context(), {
record: record,
})
:
this.__error(
// Todo: rename CSV_RECORD_DONT_MATCH_COLUMNS_LENGTH to
// CSV_RECORD_INCONSISTENT_COLUMNS
new CsvError('CSV_RECORD_DONT_MATCH_COLUMNS_LENGTH', [
'Invalid Record Length:',
`columns length is ${columns.length},`, // rename columns
`got ${recordLength} on line ${this.info.lines}`,
], this.options, this.__context(), {
record: record,
})
)
// Todo: rename CSV_RECORD_DONT_MATCH_COLUMNS_LENGTH to
// CSV_RECORD_INCONSISTENT_COLUMNS
new CsvError('CSV_RECORD_DONT_MATCH_COLUMNS_LENGTH', [
'Invalid Record Length:',
`columns length is ${columns.length},`, // rename columns
`got ${recordLength} on line ${this.info.lines}`,
], this.options, this.__context(), {
record: record,
})
if(relax_column_count === true ||
(relax_column_count_less === true && recordLength < this.state.expectedRecordLength) ||
(relax_column_count_more === true && recordLength > this.state.expectedRecordLength) ){
this.info.invalid_field_length++
this.state.error = err
// Error is undefined with skip_lines_with_error
}else if(err !== undefined){
return err
}else{
const finalErr = this.__error(err)
if(finalErr) return finalErr
}
}
if(skip_lines_with_empty_values === true){
Expand Down
16 changes: 16 additions & 0 deletions test/option.relax_column_count.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,20 @@ describe 'Option `relax_column_count`', ->
{ a: '3', b: '4' }
]
next()

describe 'with skip_lines_with_error', (next) ->

it 'dont skip records', ->
parse """
column_a
a,b
""",
skip_lines_with_error: true
relax_column_count: true
, (err, records) ->
records.should.eql [
[ 'column_a' ]
[ 'a', 'b' ]
]


0 comments on commit 92e2f65

Please sign in to comment.