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

Commit

Permalink
comment_lines: count the number of commented lines with no records
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Nov 12, 2018
1 parent 6fa5a35 commit 7ff2785
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -31,6 +31,7 @@ New features:
* callback: export info object as third argument
* cast: catch error in user functions
* ts: mark info as readonly with required properties
* comment_lines: count the number of commented lines with no records

API management

Expand Down
5 changes: 5 additions & 0 deletions lib/es5/index.js
Expand Up @@ -242,6 +242,7 @@ function (_Transform) {
wasRowDelimiter: false
};
_this.info = {
comment_lines: 0,
empty_lines: 0,
invalid_field_length: 0,
lines: 1,
Expand Down Expand Up @@ -471,6 +472,10 @@ 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 = commenting = true;
continue;
}
Expand Down
4 changes: 4 additions & 0 deletions lib/index.d.ts
Expand Up @@ -186,6 +186,10 @@ declare namespace parse {
}

interface Info {
/**
* Count the number of lines being fully commented.
*/
readonly comment_lines: number;
/**
* Count the number of processed empty lines.
*/
Expand Down
4 changes: 4 additions & 0 deletions lib/index.js
Expand Up @@ -174,6 +174,7 @@ class Parser extends Transform {
wasRowDelimiter: false
}
this.info = {
comment_lines: 0,
empty_lines: 0,
invalid_field_length: 0,
lines: 1,
Expand Down Expand Up @@ -341,6 +342,9 @@ 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 = commenting = true
continue
}
Expand Down
24 changes: 13 additions & 11 deletions test/api.info.coffee
Expand Up @@ -9,20 +9,22 @@ describe 'API info', ->
a,b,
''', (err, data, info) ->
info.should.eql
comment_lines: 0
empty_lines: 0
invalid_field_length: 0
lines: 2
records: 2
next()

it 'is exported in the callback on success', (next) ->
parse '''
1,2,3
a,b,c
''', (err, data, info) ->
info.should.eql
empty_lines: 0
invalid_field_length: 0
lines: 2
records: 2
next err
it 'is exported in the callback on success', (next) ->
parse '''
1,2,3
a,b,c
''', (err, data, info) ->
info.should.eql
comment_lines: 0
empty_lines: 0
invalid_field_length: 0
lines: 2
records: 2
next err
14 changes: 10 additions & 4 deletions test/api.types.ts
Expand Up @@ -25,8 +25,8 @@ describe('API Types', () => {
const info: Info = parser.info
const keys: any = Object.keys(info)
keys.sort().should.eql([
'empty_lines', 'invalid_field_length',
'lines', 'records'
'comment_lines', 'empty_lines',
'invalid_field_length', 'lines', 'records'
])
})

Expand All @@ -35,10 +35,16 @@ describe('API Types', () => {
describe('Info', () => {

const fakeinfo = {
empty_lines: 1, invalid_field_length: 1,
lines: 1, records: 1
comment_lines: 1, empty_lines: 1,
invalid_field_length: 1, lines: 1, records: 1
}

it('comment_lines', () => {
const info: Info = fakeinfo
const comment_lines: number = info.comment_lines
comment_lines
})

it('empty_lines', () => {
const info: Info = fakeinfo
const empty_lines: number = info.empty_lines
Expand Down
59 changes: 59 additions & 0 deletions test/info.comment_lines.coffee
@@ -0,0 +1,59 @@

parse = require '../lib'

describe 'properties comment_lines', ->

it 'no empty lines', (next) ->
parse '''
a,b,c
d,e,f
''', comment: '#', (err, data, {comment_lines}) ->
comment_lines.should.eql 0 unless err
next err

it 'comment in the middle of a line', (next) ->
parse '''
a,b,c
d,e,f # comment
h,i,j
''', comment: '#', (err, data, {comment_lines}) ->
comment_lines.should.eql 0 unless err
next err

it 'single comment line', (next) ->
parse '# comment', comment: '#', (err, data, {comment_lines}) ->
comment_lines.should.eql 1 unless err
next err

it 'single comment line with empty field', (next) ->
parse '""# comment', comment: '#', (err, data, {comment_lines}) ->
comment_lines.should.eql 0 unless err
next err

it 'two line in the middle of dataset', (next) ->
parse '''
a,b,c
# comment 1
# comment 2
d,e,f
''', comment: '#', skip_empty_lines: true, (err, data, {comment_lines}) ->
comment_lines.should.eql 2 unless err
next err

it 'one line at the end of dataset', (next) ->
parse '''
a,b,c
d,e,f
# comment
''', comment: '#', (err, data, {comment_lines}) ->
comment_lines.should.eql 1 unless err
next err

it 'one line a the begining', (next) ->
parse '''
# comment
a,b,c
d,e,f
''', comment: '#', (err, data, {comment_lines}) ->
comment_lines.should.eql 1 unless err
next err

0 comments on commit 7ff2785

Please sign in to comment.