diff --git a/lib/es5/index.js b/lib/es5/index.js index 0975686..25fb9bb 100644 --- a/lib/es5/index.js +++ b/lib/es5/index.js @@ -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; @@ -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; } @@ -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; @@ -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 { @@ -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", diff --git a/lib/index.js b/lib/index.js index 6896028..d6505ca 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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() @@ -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 } @@ -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{ @@ -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 diff --git a/test/info.comment_lines.coffee b/test/info.comment_lines.coffee index 35bcc09..4427216 100644 --- a/test/info.comment_lines.coffee +++ b/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 ''' diff --git a/test/info.empty_lines.coffee b/test/info.empty_lines.coffee index e9df90c..7dc4ff6 100644 --- a/test/info.empty_lines.coffee +++ b/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}) -> @@ -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() diff --git a/test/info.invalid_field_length.coffee b/test/info.invalid_field_length.coffee index 55d81a8..1ccdc4b 100644 --- a/test/info.invalid_field_length.coffee +++ b/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 ''' diff --git a/test/options.comment.coffee b/test/options.comment.coffee index 6674413..4f0d757 100644 --- a/test/options.comment.coffee +++ b/test/options.comment.coffee @@ -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