From 14686c9df6559e82a90a98ed84df0fd419aedb10 Mon Sep 17 00:00:00 2001 From: David Worms Date: Mon, 21 Oct 2019 16:35:59 +0200 Subject: [PATCH] context: column is null when cast force the context creation, fix #260 --- CHANGELOG.md | 4 ++++ lib/es5/index.js | 2 +- lib/index.js | 2 +- test/api.assert_error.coffee | 1 + test/option.columns.coffee | 20 ++++++++++++++++---- test/option.skip_lines_with_error.coffee | 1 - 6 files changed, 23 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e0cd1e9..5ebe02c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/es5/index.js b/lib/es5/index.js index ae57d8a..281a115 100644 --- a/lib/es5/index.js +++ b/lib/es5/index.js @@ -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, diff --git a/lib/index.js b/lib/index.js index 3106e08..31f6d70 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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 ) : diff --git a/test/api.assert_error.coffee b/test/api.assert_error.coffee index 6a4f6a5..3587369 100644 --- a/test/api.assert_error.coffee +++ b/test/api.assert_error.coffee @@ -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' diff --git a/test/option.columns.coffee b/test/option.columns.coffee index bb25f29..b7160f4 100644 --- a/test/option.columns.coffee +++ b/test/option.columns.coffee @@ -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() @@ -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 diff --git a/test/option.skip_lines_with_error.coffee b/test/option.skip_lines_with_error.coffee index 9f9d70d..dad325f 100644 --- a/test/option.skip_lines_with_error.coffee +++ b/test/option.skip_lines_with_error.coffee @@ -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'