From 7e4d82cc6ac85f53e3154ab8113e340a359261fd Mon Sep 17 00:00:00 2001 From: MilaSonger Date: Thu, 15 Apr 2021 11:57:57 -0400 Subject: [PATCH] maintain leading zero numbers in arrays --- lib/index.js | 4 ++-- test/option.cast.coffee | 35 +++++++++++++++++++++++++++-------- 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/lib/index.js b/lib/index.js index d3cb4aa..15480db 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] || obj[columns[i].name] === 0)) { 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