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

Commit

Permalink
fix: handle cast value 0 fix #315
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Apr 15, 2021
1 parent 6579da8 commit 9bfc51f
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/browser/index.js
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion lib/browser/sync.js
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion lib/es5/index.js
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions lib/index.js
Expand Up @@ -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++
Expand Down Expand Up @@ -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 {
Expand Down
35 changes: 27 additions & 8 deletions test/option.cast.coffee
Expand Up @@ -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 })
Expand Down Expand Up @@ -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) ->
Expand Down Expand Up @@ -126,7 +126,7 @@ describe 'Option `cast`', ->
[ false, true ]
] unless err
next err

it 'return undefined', ->

it 'accept all values', (next) ->
Expand All @@ -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) ->
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9bfc51f

Please sign in to comment.