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

Commit 14686c9

Browse files
committed
context: column is null when cast force the context creation, fix #260
1 parent 90cbd5d commit 14686c9

File tree

6 files changed

+23
-7
lines changed

6 files changed

+23
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
* promise: new API module
99
* errors: finish normalisation of all errors
1010

11+
## Trunk
12+
13+
* context: column is null when cast force the context creation, fix #260
14+
1115
## Version 4.6.4
1216

1317
* errors: don't stringify/parse undefined and null values

lib/es5/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,7 @@ function (_Transform) {
11281128
var columns = this.options.columns;
11291129
var isColumns = Array.isArray(columns);
11301130
return {
1131-
column: isColumns === true ? columns.length >= this.state.record.length ? columns[this.state.record.length].name : null : this.state.record.length,
1131+
column: isColumns === true ? columns.length > this.state.record.length ? columns[this.state.record.length].name : null : this.state.record.length,
11321132
empty_lines: this.info.empty_lines,
11331133
header: columns === true,
11341134
index: this.state.record.length,

lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ class Parser extends Transform {
903903
const isColumns = Array.isArray(columns)
904904
return {
905905
column: isColumns === true ?
906-
( columns.length >= this.state.record.length ?
906+
( columns.length > this.state.record.length ?
907907
columns[this.state.record.length].name :
908908
null
909909
) :

test/api.assert_error.coffee

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module.exports = assert_error = (err, assert = {}, exhaustive = false) ->
88
return
99
if exhaustive then for key, value of err
1010
assert.should.have.keys(key)
11+
err.should.be.an.Error()
1112
for key, expect of assert
1213
value = err[key]
1314
if typeof expect is 'string'

test/option.columns.coffee

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ describe 'Option `columns`', ->
106106
4,5,6,x
107107
7,8,9,x
108108
""", columns: ["a", "b", "c", "d"], (err, data) ->
109-
assert_error
109+
assert_error err,
110110
message: 'Invalid Record Length: header length is 4, got 3 on line 1'
111111
code: 'CSV_INVALID_RECORD_LENGTH_DONT_MATCH_COLUMNS'
112112
next()
@@ -117,14 +117,26 @@ describe 'Option `columns`', ->
117117
4,5,6,x
118118
7,8,9
119119
""", columns: ["a", "b", "c", "d"], (err, data) ->
120-
assert_error
120+
assert_error err,
121121
message: 'Invalid Record Length: header length is 4, got 3 on line 3'
122122
code: 'CSV_INVALID_RECORD_LENGTH_DONT_MATCH_COLUMNS'
123123
next()
124+
125+
it 'context column is null when cast force the context creation', (next) ->
126+
# Trigger cast to force the creation of a context
127+
parse "a\nb,\n",
128+
columns: true
129+
cast: (value) -> value
130+
, (err, data) ->
131+
assert_error err,
132+
message: 'Invalid Record Length: header length is 1, got 2 on line 2'
133+
code: 'CSV_INVALID_RECORD_LENGTH_DONT_MATCH_COLUMNS'
134+
column: null
135+
next()
124136

125-
it 'null context column when columns number inferieur to record length, fix regression #259', (next) ->
137+
it 'context column is null when columns number inferieur to record length, fix regression #259', (next) ->
126138
parse "a\nb,\n", columns: true, (err, data) ->
127-
assert_error
139+
assert_error err,
128140
message: 'Invalid Record Length: header length is 1, got 2 on line 2'
129141
code: 'CSV_INVALID_RECORD_LENGTH_DONT_MATCH_COLUMNS'
130142
column: null

test/option.skip_lines_with_error.coffee

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ describe 'Option `skip_lines_with_error`', ->
4646
["line","1"]
4747
["line", "3"]
4848
] unless err
49-
console.log errors
5049
assert_error errors, [
5150
message: 'Invalid Opening Quote: a quote is found inside a field at line 2'
5251
code: 'INVALID_OPENING_QUOTE'

0 commit comments

Comments
 (0)