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

Commit

Permalink
context: column is null when cast force the context creation, fix #260
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Oct 21, 2019
1 parent 90cbd5d commit 14686c9
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,10 @@
* promise: new API module
* errors: finish normalisation of all errors

## Trunk

* context: column is null when cast force the context creation, fix #260

## Version 4.6.4

* errors: don't stringify/parse undefined and null values
Expand Down
2 changes: 1 addition & 1 deletion lib/es5/index.js
Expand Up @@ -1128,7 +1128,7 @@ function (_Transform) {
var columns = this.options.columns;
var isColumns = Array.isArray(columns);
return {
column: isColumns === true ? columns.length >= this.state.record.length ? columns[this.state.record.length].name : null : this.state.record.length,
column: isColumns === true ? columns.length > this.state.record.length ? columns[this.state.record.length].name : null : this.state.record.length,
empty_lines: this.info.empty_lines,
header: columns === true,
index: this.state.record.length,
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Expand Up @@ -903,7 +903,7 @@ class Parser extends Transform {
const isColumns = Array.isArray(columns)
return {
column: isColumns === true ?
( columns.length >= this.state.record.length ?
( columns.length > this.state.record.length ?
columns[this.state.record.length].name :
null
) :
Expand Down
1 change: 1 addition & 0 deletions test/api.assert_error.coffee
Expand Up @@ -8,6 +8,7 @@ module.exports = assert_error = (err, assert = {}, exhaustive = false) ->
return
if exhaustive then for key, value of err
assert.should.have.keys(key)
err.should.be.an.Error()
for key, expect of assert
value = err[key]
if typeof expect is 'string'
Expand Down
20 changes: 16 additions & 4 deletions test/option.columns.coffee
Expand Up @@ -106,7 +106,7 @@ describe 'Option `columns`', ->
4,5,6,x
7,8,9,x
""", columns: ["a", "b", "c", "d"], (err, data) ->
assert_error
assert_error err,
message: 'Invalid Record Length: header length is 4, got 3 on line 1'
code: 'CSV_INVALID_RECORD_LENGTH_DONT_MATCH_COLUMNS'
next()
Expand All @@ -117,14 +117,26 @@ describe 'Option `columns`', ->
4,5,6,x
7,8,9
""", columns: ["a", "b", "c", "d"], (err, data) ->
assert_error
assert_error err,
message: 'Invalid Record Length: header length is 4, got 3 on line 3'
code: 'CSV_INVALID_RECORD_LENGTH_DONT_MATCH_COLUMNS'
next()

it 'context column is null when cast force the context creation', (next) ->
# Trigger cast to force the creation of a context
parse "a\nb,\n",
columns: true
cast: (value) -> value
, (err, data) ->
assert_error err,
message: 'Invalid Record Length: header length is 1, got 2 on line 2'
code: 'CSV_INVALID_RECORD_LENGTH_DONT_MATCH_COLUMNS'
column: null
next()

it 'null context column when columns number inferieur to record length, fix regression #259', (next) ->
it 'context column is null when columns number inferieur to record length, fix regression #259', (next) ->
parse "a\nb,\n", columns: true, (err, data) ->
assert_error
assert_error err,
message: 'Invalid Record Length: header length is 1, got 2 on line 2'
code: 'CSV_INVALID_RECORD_LENGTH_DONT_MATCH_COLUMNS'
column: null
Expand Down
1 change: 0 additions & 1 deletion test/option.skip_lines_with_error.coffee
Expand Up @@ -46,7 +46,6 @@ describe 'Option `skip_lines_with_error`', ->
["line","1"]
["line", "3"]
] unless err
console.log errors
assert_error errors, [
message: 'Invalid Opening Quote: a quote is found inside a field at line 2'
code: 'INVALID_OPENING_QUOTE'
Expand Down

0 comments on commit 14686c9

Please sign in to comment.