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

Commit

Permalink
comment: handle empty field before comment
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Nov 16, 2018
1 parent deeeb80 commit 7e9f9a0
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 27 deletions.
22 changes: 9 additions & 13 deletions lib/es5/index.js
Expand Up @@ -436,17 +436,17 @@ function (_Transform) {

if (recordDelimiterLength !== 0) {
// Do not emit comments which take a full line
var skipCommentLine = this.state.commenting && this.state.record.length === 0 && this.state.field.length === 0;
var skipCommentLine = this.state.commenting && this.state.wasQuoting === false && this.state.record.length === 0 && this.state.field.length === 0;

if (skipCommentLine) {
// TODO: update the doc, a line with comment is considered an
// empty line in the sense that no record is found inside
this.info.empty_lines++;
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++;
continue;
}
} // 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 Down Expand Up @@ -485,10 +485,6 @@ function (_Transform) {
var commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr);

if (commentCount !== 0) {
if (this.state.record.length === 0 && this.state.field.length === 0 && this.state.wasQuoting === false) {
this.info.comment_lines++;
}

this.state.commenting = true;
continue;
}
Expand Down Expand Up @@ -533,7 +529,7 @@ function (_Transform) {
if (_err4 !== undefined) return _err4;
} else {
// Skip last line if it has no characters
if (this.state.record.length !== 0 || this.state.field.length !== 0) {
if (this.state.wasQuoting === true || this.state.record.length !== 0 || this.state.field.length !== 0) {
var _errField2 = this.__onField();

if (_errField2 !== undefined) return _errField2;
Expand All @@ -543,6 +539,8 @@ function (_Transform) {
if (_errRecord !== undefined) return _errRecord;
} else if (this.state.wasRowDelimiter === true) {
this.info.empty_lines++;
} else if (this.state.commenting === true) {
this.info.comment_lines++;
}
}
} else {
Expand Down Expand Up @@ -706,9 +704,7 @@ function (_Transform) {
}

this.state.record = [];
this.state.record_length = 0; // if(info === true){
// this.state.info = null
// }
this.state.record_length = 0;
}
}, {
key: "__onField",
Expand Down
19 changes: 8 additions & 11 deletions lib/index.js
Expand Up @@ -315,16 +315,17 @@ class Parser extends Transform {
let recordDelimiterLength = this.__isRecordDelimiter(chr, buf, pos)
if(recordDelimiterLength !== 0){
// Do not emit comments which take a full line
const skipCommentLine = this.state.commenting && (this.state.record.length === 0 && this.state.field.length === 0)
const skipCommentLine = this.state.commenting && (this.state.wasQuoting === false && this.state.record.length === 0 && this.state.field.length === 0)
if(skipCommentLine){
// TODO: update the doc, a line with comment is considered an
// empty line in the sense that no record is found inside
this.info.empty_lines++
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++
continue
}
// 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
this.__resetField()
Expand All @@ -351,9 +352,6 @@ class Parser extends Transform {
}
const commentCount = comment === null ? 0 : this.__compareBytes(comment, buf, pos, chr)
if(commentCount !== 0){
if(this.state.record.length === 0 && this.state.field.length === 0 && this.state.wasQuoting === false){
this.info.comment_lines++
}
this.state.commenting = true
continue
}
Expand Down Expand Up @@ -389,13 +387,15 @@ class Parser extends Transform {
if(err !== undefined) return err
}else{
// Skip last line if it has no characters
if(this.state.record.length !== 0 || this.state.field.length !== 0){
if(this.state.wasQuoting === true || this.state.record.length !== 0 || this.state.field.length !== 0){
const errField = this.__onField()
if(errField !== undefined) return errField
const errRecord = this.__onRow()
if(errRecord !== undefined) return errRecord
}else if(this.state.wasRowDelimiter === true){
this.info.empty_lines++
}else if(this.state.commenting === true){
this.info.comment_lines++
}
}
}else{
Expand Down Expand Up @@ -515,9 +515,6 @@ class Parser extends Transform {
}
this.state.record = []
this.state.record_length = 0
// if(info === true){
// this.state.info = null
// }
}
__onField(){
const {cast, rtrim} = this.options
Expand Down
2 changes: 1 addition & 1 deletion test/info.comment_lines.coffee
@@ -1,7 +1,7 @@

parse = require '../lib'

describe 'properties comment_lines', ->
describe 'info comment_lines', ->

it 'no empty lines', (next) ->
parse '''
Expand Down
11 changes: 10 additions & 1 deletion test/info.empty_lines.coffee
@@ -1,7 +1,7 @@

parse = require '../lib'

describe 'properties empty_lines', ->
describe 'info empty_lines', ->

it 'no lines', (next) ->
parse '', (err, data, {empty_lines}) ->
Expand Down Expand Up @@ -43,3 +43,12 @@ describe 'properties empty_lines', ->
err.message.should.eql 'Invalid Record Length: expect 1, got 3 on line 2'
empty_lines.should.eql 0
next()

it 'dont count commented lines', (next) ->
parse '''
a,b,c
d,e,f
# comment
''', (err, data, {empty_lines}) ->
empty_lines.should.eql 0
next()
2 changes: 1 addition & 1 deletion test/info.invalid_field_length.coffee
@@ -1,7 +1,7 @@

parse = require '../lib'

describe 'properties lines_count', ->
describe 'info invalid_field_length', ->

it 'with relax_column_count', (next) ->
parse '''
Expand Down
10 changes: 10 additions & 0 deletions test/options.comment.coffee
Expand Up @@ -15,6 +15,16 @@ describe 'options comment', ->
parse '', comment: 2, (->)
).should.throw 'Invalid Option: comment must be a buffer or a string, got 2'

it 'single comment line', (next) ->
parse '# comment', comment: '#', (err, data) ->
data.length.should.eql 0
next err

it 'single comment line with empty field', (next) ->
parse '""# comment', comment: '#', (err, data) ->
data.length.should.eql 1
next err

it 'skip line starting by single comment char', (next) ->
parse """
# skip this
Expand Down

0 comments on commit 7e9f9a0

Please sign in to comment.