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

Commit

Permalink
invalid_field_length: was skipped_line_count
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Nov 12, 2018
1 parent b1b66a3 commit 7556d4f
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 92 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -13,7 +13,7 @@ This is a complete rewrite based with a Buffer implementation. There are no majo
* `count` is now `info.records`
* `lines` is now `info.lines`
* `empty_line_count` is now `info.empty_lines`
* `skipped_line_count` is now `info.skipped_lines`
* `skipped_line_count` is now `info.invalid_field_length`
* `context.count` is cast function is now `context.records`
* drop support for deprecated options `auto_parse` and `auto_parse_date`
* drop emission of the `record` event
Expand Down
32 changes: 16 additions & 16 deletions lib/es5/index.js
Expand Up @@ -243,9 +243,9 @@ function (_Transform) {
};
_this.info = {
empty_lines: 0,
invalid_field_length: 0,
lines: 1,
records: 0,
skipped_lines: 0
records: 0
};
return _this;
}
Expand Down Expand Up @@ -563,19 +563,19 @@ function (_Transform) {
if (columns === false && this.info.records === 0) {
this.state.expectedRecordLength = recordLength;
} else if (enabled === true) {
if (relax_column_count === true) {
if (recordLength !== this.state.expectedRecordLength) {
this.info.skipped_lines++;
}
} else if (recordLength !== this.state.expectedRecordLength) {
if (columns === false) {
var err = this.__error("Invalid Record Length: expect ".concat(this.state.expectedRecordLength, ", got ").concat(recordLength, " on line ").concat(this.info.lines));

if (err !== undefined) return err;
if (recordLength !== this.state.expectedRecordLength) {
if (relax_column_count === true) {
this.info.invalid_field_length++;
} else {
var _err5 = this.__error("Invalid Record Length: header length is ".concat(columns.length, ", got ").concat(recordLength, " on line ").concat(this.info.lines));
if (columns === false) {
var err = this.__error("Invalid Record Length: expect ".concat(this.state.expectedRecordLength, ", got ").concat(recordLength, " on line ").concat(this.info.lines));

if (err !== undefined) return err;
} else {
var _err5 = this.__error("Invalid Record Length: header length is ".concat(columns.length, ", got ").concat(recordLength, " on line ").concat(this.info.lines));

if (_err5 !== undefined) return _err5;
if (_err5 !== undefined) return _err5;
}
}
}
}
Expand Down Expand Up @@ -727,10 +727,10 @@ function (_Transform) {
empty_lines: this.info.empty_lines,
header: this.options.columns === true,
index: this.state.record.length,
invalid_field_length: this.info.invalid_field_length,
quoting: this.state.wasQuoting,
lines: this.info.lines,
records: this.info.records,
skipped_lines: this.info.skipped_lines
records: this.info.records
};

if (this.state.castField !== null) {
Expand Down Expand Up @@ -913,7 +913,7 @@ var parse = function parse() {
}
});
parser.on('error', function (err) {
callback(err);
callback(err, null, parser.info);
});
parser.on('end', function () {
callback(null, records, parser.info);
Expand Down
4 changes: 2 additions & 2 deletions lib/index.d.ts
Expand Up @@ -37,7 +37,7 @@ declare namespace parse {
readonly quoting: boolean;
readonly lines: number;
readonly records: number;
readonly skipped_lines: number;
readonly invalid_field_length: number;
}

type CastingFunction = (value: string, context: CastingContext) => any;
Expand Down Expand Up @@ -201,6 +201,6 @@ declare namespace parse {
/**
*
*/
skipped_lines?: number;
invalid_field_length?: number;
}
}
30 changes: 15 additions & 15 deletions lib/index.js
Expand Up @@ -175,9 +175,9 @@ class Parser extends Transform {
}
this.info = {
empty_lines: 0,
invalid_field_length: 0,
lines: 1,
records: 0,
skipped_lines: 0
records: 0
}
}
_transform(buf, encoding, callback){
Expand Down Expand Up @@ -406,17 +406,17 @@ class Parser extends Transform {
if(columns === false && this.info.records === 0){
this.state.expectedRecordLength = recordLength
}else if(enabled === true){
if(relax_column_count === true){
if(recordLength !== this.state.expectedRecordLength){
this.info.skipped_lines++
}
}else if(recordLength !== this.state.expectedRecordLength){
if(columns === false){
const err = this.__error(`Invalid Record Length: expect ${this.state.expectedRecordLength}, got ${recordLength} on line ${this.info.lines}`)
if(err !== undefined) return err
if(recordLength !== this.state.expectedRecordLength){
if(relax_column_count === true){
this.info.invalid_field_length++
}else{
const err = this.__error(`Invalid Record Length: header length is ${columns.length}, got ${recordLength} on line ${this.info.lines}`)
if(err !== undefined) return err
if(columns === false){
const err = this.__error(`Invalid Record Length: expect ${this.state.expectedRecordLength}, got ${recordLength} on line ${this.info.lines}`)
if(err !== undefined) return err
}else{
const err = this.__error(`Invalid Record Length: header length is ${columns.length}, got ${recordLength} on line ${this.info.lines}`)
if(err !== undefined) return err
}
}
}
}
Expand Down Expand Up @@ -518,10 +518,10 @@ class Parser extends Transform {
empty_lines: this.info.empty_lines,
header: this.options.columns === true,
index: this.state.record.length,
invalid_field_length: this.info.invalid_field_length,
quoting: this.state.wasQuoting,
lines: this.info.lines,
records: this.info.records,
skipped_lines: this.info.skipped_lines
records: this.info.records
}
if(this.state.castField !== null){
try{
Expand Down Expand Up @@ -664,7 +664,7 @@ const parse = function(){
}
})
parser.on('error', function(err){
callback(err)
callback(err, null, parser.info)
})
parser.on('end', function(){
callback(null, records, parser.info)
Expand Down
4 changes: 2 additions & 2 deletions samples/options.cast.js
Expand Up @@ -19,10 +19,10 @@ const records = parse(data, {
assert.deepEqual(records, [
[ '2000-01-01T05:00:00.000Z', {
column: 1, empty_lines: 0, header: false, index: 1,
quoting: false, lines: 1, records: 0, skipped_lines: 0
invalid_field_length: 0, quoting: false, lines: 1, records: 0
} ],
[ '2050-11-27T05:00:00.000Z', {
column: 1, empty_lines: 0, header: false, index: 1,
quoting: false, lines: 2, records: 1, skipped_lines: 0
invalid_field_length: 0, quoting: false, lines: 2, records: 1
} ]
])
18 changes: 15 additions & 3 deletions test/api.info.coffee
Expand Up @@ -3,14 +3,26 @@ parse = require '../lib'

describe 'API info', ->

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

Expand All @@ -48,9 +48,9 @@ describe('API Types', () => {
info.records = 1
})

it('skipped_lines', () => {
it('invalid_field_length', () => {
const info: Info = {}
info.skipped_lines = 1
info.invalid_field_length = 1
})

})
Expand Down Expand Up @@ -209,10 +209,10 @@ describe('API Types', () => {
const quoting: boolean = context.quoting
const lines: number = context.lines
const records: number = context.records
const skipped_lines: number = context.skipped_lines
const invalid_field_length: number = context.invalid_field_length
return [
column, empty_lines, header, index,
quoting, lines, records, skipped_lines
quoting, lines, records, invalid_field_length
]
}
})
Expand Down
10 changes: 10 additions & 0 deletions test/info.empty_lines.coffee
Expand Up @@ -33,3 +33,13 @@ describe 'properties empty_lines', ->
''', (err, data, {empty_lines}) ->
empty_lines.should.eql 1
next()

it 'dont count empty lines when empty and skip_empty_lines disabled', (next) ->
parse '''
a,b,c
d,e,f
''', (err, data, {empty_lines}) ->
err.message.should.eql 'Invalid Record Length: expect 1, got 3 on line 2'
empty_lines.should.eql 0
next()
33 changes: 33 additions & 0 deletions test/info.invalid_field_length.coffee
@@ -0,0 +1,33 @@

parse = require '../lib'

describe 'properties lines_count', ->

it 'with relax_column_count', (next) ->
parse '''
a,b,c
d,e
f,g,h
i,j
''',
relax_column_count: true
, (err, data, {invalid_field_length}) ->
data.length.should.eql 4
invalid_field_length.should.eql 2
next()

it 'with relax_column_count and skip_empty_lines', (next) ->
parser = parse '''
a,b,c
d,e,f
h,i
''',
relax_column_count: true
skip_empty_lines: true
, (err, data, {empty_lines, invalid_field_length, records})->
empty_lines.should.eql 2
invalid_field_length.should.eql 1
records.should.eql 3
next()
46 changes: 0 additions & 46 deletions test/info.lines_count.coffee

This file was deleted.

10 changes: 8 additions & 2 deletions test/options.cast.coffee
Expand Up @@ -49,8 +49,14 @@ describe 'options "cast"', ->
else {...context}
, (err, records) ->
records.should.eql [
[ '2000-01-01T05:00:00.000Z', {quoting: false, index: 1, column: 1, empty_lines: 0, lines: 1, header: false, records: 0, skipped_lines: 0} ]
[ '2050-11-27T05:00:00.000Z', {quoting: false, index: 1, column: 1, empty_lines: 0, lines: 2, header: false, records: 1, skipped_lines: 0} ]
[ '2000-01-01T05:00:00.000Z', {
quoting: false, index: 1, column: 1, empty_lines: 0, lines: 1,
header: false, invalid_field_length: 0, records: 0
} ]
[ '2050-11-27T05:00:00.000Z', {
quoting: false, index: 1, column: 1, empty_lines: 0, lines: 2,
header: false, invalid_field_length: 0, records: 1
} ]
] unless err
next err

Expand Down

0 comments on commit 7556d4f

Please sign in to comment.