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

Commit

Permalink
fix: skip_empty_lines don't interfere with from_line
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Feb 1, 2021
1 parent afa0bc5 commit d137514
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 59 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Please join and contribute:
* relax_column_count: rename INCONSISTENT_RECORD_LENGTH to RECORD_INCONSISTENT_FIELDS_LENGTH (easy)
* relax_column_count: rename RECORD_DONT_MATCH_COLUMNS_LENGTH to RECORD_INCONSISTENT_COLUMNS (easy)

## Trunk

* fix: skip_empty_lines don't interfere with from_line

## Version 4.15.0

* feat: new ignore_last_delimiters option, solve #193
Expand Down
23 changes: 11 additions & 12 deletions lib/browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7180,25 +7180,24 @@ class Parser extends Transform {
this.info.comment_lines++
// Skip full comment line
}else{
// Skip if line is empty and skip_empty_lines activated
if(skip_empty_lines === true && this.state.wasQuoting === false && this.state.record.length === 0 && this.state.field.length === 0){
this.info.empty_lines++
pos += recordDelimiterLength - 1
continue
}
// Activate records emition if above from_line
if(this.state.enabled === false && this.info.lines + (this.state.wasRowDelimiter === true ? 1: 0 ) >= from_line){
if(this.state.enabled === false && this.info.lines + (this.state.wasRowDelimiter === true ? 1: 0) >= from_line){
this.state.enabled = true
this.__resetField()
this.__resetRecord()
pos += recordDelimiterLength - 1
continue
}else{
const errField = this.__onField()
if(errField !== undefined) return errField
const errRecord = this.__onRecord()
if(errRecord !== undefined) return errRecord
}
// Skip if line is empty and skip_empty_lines activated
if(skip_empty_lines === true && this.state.wasQuoting === false && this.state.record.length === 0 && this.state.field.length === 0){
this.info.empty_lines++
pos += recordDelimiterLength - 1
continue
}
const errField = this.__onField()
if(errField !== undefined) return errField
const errRecord = this.__onRecord()
if(errRecord !== undefined) return errRecord
if(to !== -1 && this.info.records >= to){
this.state.stop = true
this.push(null)
Expand Down
23 changes: 11 additions & 12 deletions lib/browser/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -7180,25 +7180,24 @@ class Parser extends Transform {
this.info.comment_lines++
// Skip full comment line
}else{
// Skip if line is empty and skip_empty_lines activated
if(skip_empty_lines === true && this.state.wasQuoting === false && this.state.record.length === 0 && this.state.field.length === 0){
this.info.empty_lines++
pos += recordDelimiterLength - 1
continue
}
// Activate records emition if above from_line
if(this.state.enabled === false && this.info.lines + (this.state.wasRowDelimiter === true ? 1: 0 ) >= from_line){
if(this.state.enabled === false && this.info.lines + (this.state.wasRowDelimiter === true ? 1: 0) >= from_line){
this.state.enabled = true
this.__resetField()
this.__resetRecord()
pos += recordDelimiterLength - 1
continue
}else{
const errField = this.__onField()
if(errField !== undefined) return errField
const errRecord = this.__onRecord()
if(errRecord !== undefined) return errRecord
}
// Skip if line is empty and skip_empty_lines activated
if(skip_empty_lines === true && this.state.wasQuoting === false && this.state.record.length === 0 && this.state.field.length === 0){
this.info.empty_lines++
pos += recordDelimiterLength - 1
continue
}
const errField = this.__onField()
if(errField !== undefined) return errField
const errRecord = this.__onRecord()
if(errRecord !== undefined) return errRecord
if(to !== -1 && this.info.records >= to){
this.state.stop = true
this.push(null)
Expand Down
28 changes: 14 additions & 14 deletions lib/es5/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -759,14 +759,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
if (skipCommentLine) {
this.info.comment_lines++; // Skip full comment line
} else {
// Skip if line is empty and skip_empty_lines activated
if (skip_empty_lines === true && this.state.wasQuoting === false && this.state.record.length === 0 && this.state.field.length === 0) {
this.info.empty_lines++;
pos += recordDelimiterLength - 1;
continue;
} // Activate records emition if above from_line


// Activate records emition if above from_line
if (this.state.enabled === false && this.info.lines + (this.state.wasRowDelimiter === true ? 1 : 0) >= from_line) {
this.state.enabled = true;

Expand All @@ -776,16 +769,23 @@ var Parser = /*#__PURE__*/function (_Transform) {

pos += recordDelimiterLength - 1;
continue;
} else {
var errField = this.__onField();
} // Skip if line is empty and skip_empty_lines activated

if (errField !== undefined) return errField;

var errRecord = this.__onRecord();

if (errRecord !== undefined) return errRecord;
if (skip_empty_lines === true && this.state.wasQuoting === false && this.state.record.length === 0 && this.state.field.length === 0) {
this.info.empty_lines++;
pos += recordDelimiterLength - 1;
continue;
}

var errField = this.__onField();

if (errField !== undefined) return errField;

var errRecord = this.__onRecord();

if (errRecord !== undefined) return errRecord;

if (to !== -1 && this.info.records >= to) {
this.state.stop = true;
this.push(null);
Expand Down
23 changes: 11 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -641,25 +641,24 @@ class Parser extends Transform {
this.info.comment_lines++
// Skip full comment line
}else{
// Skip if line is empty and skip_empty_lines activated
if(skip_empty_lines === true && this.state.wasQuoting === false && this.state.record.length === 0 && this.state.field.length === 0){
this.info.empty_lines++
pos += recordDelimiterLength - 1
continue
}
// Activate records emition if above from_line
if(this.state.enabled === false && this.info.lines + (this.state.wasRowDelimiter === true ? 1: 0 ) >= from_line){
if(this.state.enabled === false && this.info.lines + (this.state.wasRowDelimiter === true ? 1: 0) >= from_line){
this.state.enabled = true
this.__resetField()
this.__resetRecord()
pos += recordDelimiterLength - 1
continue
}else{
const errField = this.__onField()
if(errField !== undefined) return errField
const errRecord = this.__onRecord()
if(errRecord !== undefined) return errRecord
}
// Skip if line is empty and skip_empty_lines activated
if(skip_empty_lines === true && this.state.wasQuoting === false && this.state.record.length === 0 && this.state.field.length === 0){
this.info.empty_lines++
pos += recordDelimiterLength - 1
continue
}
const errField = this.__onField()
if(errField !== undefined) return errField
const errRecord = this.__onRecord()
if(errRecord !== undefined) return errRecord
if(to !== -1 && this.info.records >= to){
this.state.stop = true
this.push(null)
Expand Down
34 changes: 25 additions & 9 deletions test/option.from_line.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ describe 'Option `from_line`', ->
] unless err
next err

it 'handle lines with inconsistent number of fields', (next) ->
parse """
a
1,2,3
""", from_line: 2, (err, data) ->
data.should.eql [
['1', '2', '3']
] unless err
next err
it 'handle lines with inconsistent number of fields', (next) ->
parse """
a
1,2,3
""", from_line: 2, (err, data) ->
data.should.eql [
['1', '2', '3']
] unless err
next err

it 'records with quoted line at the begining of line', (next) ->
parse """
Expand Down Expand Up @@ -92,6 +92,22 @@ describe 'Option `from_line`', ->
[ '7','8','\n9' ]
] unless err
next err

it 'no influenced by skip_empty_lines option', (next) ->
parse """
a,b,c
1,2,3
4,5,6
7,8,9
""", from_line: 5, skip_empty_lines: true, (err, data) ->
data.should.eql [
['4', '5', '6']
['7', '8', '9']
] unless err
next err

it 'handle multiple bytes record delimiters', (next) ->
parse """
Expand Down

0 comments on commit d137514

Please sign in to comment.