From 9bfc51fc77efb15d47c0173b040ebe2d8a3e3763 Mon Sep 17 00:00:00 2001 From: David Worms Date: Thu, 15 Apr 2021 19:44:46 +0200 Subject: [PATCH] fix: handle cast value 0 fix #315 --- CHANGELOG.md | 4 ++++ lib/browser/index.js | 2 +- lib/browser/sync.js | 2 +- lib/es5/index.js | 2 +- lib/index.js | 4 ++-- test/option.cast.coffee | 35 +++++++++++++++++++++++++++-------- 6 files changed, 36 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 10b7c68..67d7aef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,10 @@ Please join and contribute: * relax_column_count: rename INCONSISTENT_RECORD_LENGTH to RECORD_INCONSISTENT_FIELDS_LENGTH (easy) * relax_column_count: rename RECORD_DONT_MATCH_COLUMNS_LENGTH to RECORD_INCONSISTENT_COLUMNS (easy) +## Trunk + +* fix: handle cast value 0 fix #315 + ## Version 4.15.3 * feat: lib/browser compatibility with ES5 diff --git a/lib/browser/index.js b/lib/browser/index.js index 33fface..e965e2f 100644 --- a/lib/browser/index.js +++ b/lib/browser/index.js @@ -1066,7 +1066,7 @@ var Parser = /*#__PURE__*/function (_Transform) { for (var i = 0, l = record.length; i < l; i++) { if (columns[i] === undefined || columns[i].disabled) continue; // Turn duplicate columns into an array - if (columns_duplicates_to_array === true && obj[columns[i].name]) { + if (columns_duplicates_to_array === true && obj[columns[i].name] !== undefined) { if (Array.isArray(obj[columns[i].name])) { obj[columns[i].name] = obj[columns[i].name].concat(record[i]); } else { diff --git a/lib/browser/sync.js b/lib/browser/sync.js index 501a3d2..cc5bad6 100644 --- a/lib/browser/sync.js +++ b/lib/browser/sync.js @@ -1066,7 +1066,7 @@ var Parser = /*#__PURE__*/function (_Transform) { for (var i = 0, l = record.length; i < l; i++) { if (columns[i] === undefined || columns[i].disabled) continue; // Turn duplicate columns into an array - if (columns_duplicates_to_array === true && obj[columns[i].name]) { + if (columns_duplicates_to_array === true && obj[columns[i].name] !== undefined) { if (Array.isArray(obj[columns[i].name])) { obj[columns[i].name] = obj[columns[i].name].concat(record[i]); } else { diff --git a/lib/es5/index.js b/lib/es5/index.js index 9f4ad34..d9577e5 100644 --- a/lib/es5/index.js +++ b/lib/es5/index.js @@ -958,7 +958,7 @@ var Parser = /*#__PURE__*/function (_Transform) { for (var i = 0, l = record.length; i < l; i++) { if (columns[i] === undefined || columns[i].disabled) continue; // Turn duplicate columns into an array - if (columns_duplicates_to_array === true && obj[columns[i].name]) { + if (columns_duplicates_to_array === true && obj[columns[i].name] !== undefined) { if (Array.isArray(obj[columns[i].name])) { obj[columns[i].name] = obj[columns[i].name].concat(record[i]); } else { diff --git a/lib/index.js b/lib/index.js index d3cb4aa..c966b07 100644 --- a/lib/index.js +++ b/lib/index.js @@ -786,7 +786,7 @@ class Parser extends Transform { ], this.options, this.__context(), { record: record, }) - if(relax_column_count === true || + if(relax_column_count === true || (relax_column_count_less === true && recordLength < this.state.expectedRecordLength) || (relax_column_count_more === true && recordLength > this.state.expectedRecordLength) ){ this.info.invalid_field_length++ @@ -816,7 +816,7 @@ class Parser extends Transform { for(let i = 0, l = record.length; i < l; i++){ if(columns[i] === undefined || columns[i].disabled) continue // Turn duplicate columns into an array - if (columns_duplicates_to_array === true && obj[columns[i].name]) { + if (columns_duplicates_to_array === true && obj[columns[i].name] !== undefined) { if (Array.isArray(obj[columns[i].name])) { obj[columns[i].name] = obj[columns[i].name].concat(record[i]) } else { diff --git a/test/option.cast.coffee b/test/option.cast.coffee index 3d38c1b..ee3f13d 100644 --- a/test/option.cast.coffee +++ b/test/option.cast.coffee @@ -3,21 +3,21 @@ parse = require '../lib' assert_error = require './api.assert_error' describe 'Option `cast`', -> - + it 'validate', -> (-> parse cast: 'ohno', ( -> ) ).should.throw message: 'Invalid option cast: cast must be true or a function, got "ohno"' code: 'CSV_INVALID_OPTION_CAST' - + describe 'boolean true', -> - + it 'all columns', (next) -> parse '1,2,3', cast: true, (err, data) -> data.should.eql [ [1, 2, 3] ] next() - + it 'convert numbers', (next) -> data = [] parser = parse({ cast: true }) @@ -53,7 +53,7 @@ describe 'Option `cast`', -> parse '123a,1.23,0.123,01.23,.123,123.', cast: true, (err, data) -> data.should.eql [ ['123a', 1.23, 0.123, 1.23, 0.123, 123] ] next() - + describe 'function', -> it 'custom function', (next) -> @@ -126,7 +126,7 @@ describe 'Option `cast`', -> [ false, true ] ] unless err next err - + it 'return undefined', -> it 'accept all values', (next) -> @@ -146,7 +146,7 @@ describe 'Option `cast`', -> , (err, records) -> records.shift().should.eql [undefined, false, null] next err - + describe 'columns', -> it 'header is true on first line when columns is true', (next) -> @@ -234,8 +234,27 @@ describe 'Option `cast`', -> code: 'CSV_INVALID_COLUMN_DEFINITION' next() + describe 'columns_duplicates_to_array', -> + + it 'leading zeros are maintained when columns_duplicates_to_array is true', (next) -> + parse """ + FIELD_1,FIELD_1,FIELD_1 + 0,2,3 + 0,0,4 + """, + cast: true + columns: true + columns_duplicates_to_array: true + , (err, data) -> + data.should.eql [ + 'FIELD_1': [0, 2, 3] + , + 'FIELD_1': [0, 0, 4] + ] unless err + next err + describe 'error', -> - + it 'catch error', (next) -> parse """ 1,2,3