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

Commit 0a695e6

Browse files
committed
context: null column when columns number inferieur to record length
1 parent b076b8a commit 0a695e6

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
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: null column when columns number inferieur to record length
14+
1115
## Version 4.6.1
1216

1317
* src: set const in for loop

lib/es5/index.js

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

lib/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,10 @@ class Parser extends Transform {
891891
const isColumns = Array.isArray(columns)
892892
return {
893893
column: isColumns === true ?
894-
columns[this.state.record.length].name :
894+
( columns.length >= this.state.record.length ?
895+
columns[this.state.record.length].name :
896+
null
897+
) :
895898
this.state.record.length,
896899
empty_lines: this.info.empty_lines,
897900
header: columns === true,

test/option.columns.coffee

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ describe 'Option `columns`', ->
106106
4,5,6,x
107107
7,8,9,x
108108
""", columns: ["a", "b", "c", "d"], (err, data) ->
109-
err.message.should.eql 'Invalid Record Length: header length is 4, got 3 on line 1'
109+
assert_error
110+
message: 'Invalid Record Length: header length is 4, got 3 on line 1'
111+
code: 'CSV_INVALID_RECORD_LENGTH_DONT_MATCH_COLUMNS'
110112
next()
111113

112114
it 'validate options column length on last line', (next) ->
@@ -115,7 +117,17 @@ describe 'Option `columns`', ->
115117
4,5,6,x
116118
7,8,9
117119
""", columns: ["a", "b", "c", "d"], (err, data) ->
118-
err.message.should.eql 'Invalid Record Length: header length is 4, got 3 on line 3'
120+
assert_error
121+
message: 'Invalid Record Length: header length is 4, got 3 on line 3'
122+
code: 'CSV_INVALID_RECORD_LENGTH_DONT_MATCH_COLUMNS'
123+
next()
124+
125+
it 'null context column when columns number inferieur to record length, fix regression #259', (next) ->
126+
parse "a\nb,\n", columns: true, (err, data) ->
127+
assert_error
128+
message: 'Invalid Record Length: header length is 1, got 2 on line 2'
129+
code: 'CSV_INVALID_RECORD_LENGTH_DONT_MATCH_COLUMNS'
130+
column: null
119131
next()
120132

121133
it 'skips column names defined as undefined', (next) ->

0 commit comments

Comments
 (0)