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

Commit

Permalink
cast: ensure column is a string and not an array
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Dec 6, 2018
1 parent 40d1fee commit 8477d8c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -6,9 +6,11 @@
New features:

* options: accept camelize and underscore forms
* cast: dont call cast for non column-mappable fields

Fix:

* cast: ensure column is a string and not an array
* stream: handle empty input streams
* cast: function may return non-string values
* stream: pass stream options without modification
Expand Down
8 changes: 7 additions & 1 deletion lib/es5/index.js
Expand Up @@ -764,8 +764,14 @@ function (_Transform) {
}, {
key: "__cast",
value: function __cast(field) {
var isColumns = Array.isArray(this.options.columns); // Dont loose time calling cast if the field wont be part of the final record

if (isColumns === true && this.options.columns.length <= this.state.record.length) {
return [undefined, undefined];
}

var context = {
column: Array.isArray(this.options.columns) === true ? this.options.columns[this.state.record.length] : this.state.record.length,
column: isColumns === true ? this.options.columns[this.state.record.length].name : this.state.record.length,
empty_lines: this.info.empty_lines,
header: this.options.columns === true,
index: this.state.record.length,
Expand Down
9 changes: 8 additions & 1 deletion lib/index.js
Expand Up @@ -549,8 +549,15 @@ class Parser extends Transform {
this.state.wasQuoting = false
}
__cast(field){
const isColumns = Array.isArray(this.options.columns)
// Dont loose time calling cast if the field wont be part of the final record
if( isColumns === true && this.options.columns.length <= this.state.record.length ){
return [undefined, undefined]
}
const context = {
column: Array.isArray(this.options.columns) === true ? this.options.columns[this.state.record.length] : this.state.record.length,
column: isColumns === true ?
this.options.columns[this.state.record.length].name :
this.state.record.length,
empty_lines: this.info.empty_lines,
header: this.options.columns === true,
index: this.state.record.length,
Expand Down
55 changes: 43 additions & 12 deletions test/option.cast.coffee
Expand Up @@ -18,7 +18,7 @@ describe 'Option `cast`', ->
28392898392,1974,8.8392926e7,8E2,DEF,23,2050-11-27
"""
parser.on 'readable', ->
while(d = parser.read())
while d = parser.read()
data.push d
parser.on 'error', (err) ->
next err
Expand Down Expand Up @@ -54,16 +54,47 @@ describe 'Option `cast`', ->
, (err, records) ->
records.should.eql [
[ '2000-01-01T05:00:00.000Z', {
quoting: false, index: 1, column: 1, empty_lines: 0, lines: 1,
header: false, invalid_field_length: 0, records: 0
column: 1, empty_lines: 0, header: false, index: 1,
invalid_field_length: 0, lines: 1, quoting: false, records: 0
} ]
[ '2050-11-27T05:00:00.000Z', {
quoting: false, index: 1, column: 1, empty_lines: 0, lines: 2,
header: false, invalid_field_length: 0, records: 1
column: 1, empty_lines: 0, header: false, index: 1,
invalid_field_length: 0, lines: 2, quoting: false, records: 1
} ]
] unless err
next err

it 'column is a string', (next) ->
parse """
1,2
3,4,5
6
""",
columns: ['a', 'b']
relax_column_count: true
cast: (value, {header, column}) ->
typeof column
, (err, records) ->
records.should.eql [
{a: 'string', b: 'string'}
{a: 'string', b: 'string'}
{a: 'string'}
] unless err
next err

it 'dont call cast on unreferenced columns', (next) ->
parse """
1,2
3,4,5,6
7
""",
columns: ['a', 'b']
relax_column_count: true
cast: (value, {header, column}) ->
throw Error 'Oh no' if value > 4 and value < 7
, (err, records) ->
next err

it 'custom function with quoting context', (next) ->
parse """
"2000-01-01",date1
Expand All @@ -88,8 +119,8 @@ describe 'Option `cast`', ->
4,5,6
""",
max_record_size: 10
cast: (value, context) ->
switch context.index
cast: (value, {index}) ->
switch index
when 0
undefined
when 1
Expand All @@ -109,8 +140,8 @@ describe 'Option `cast`', ->
4,5,6
""",
columns: true
cast: (value, context) ->
if context.header then value else parseInt value
cast: (value, {header}) ->
if header then value else parseInt value
, (err, records) ->
records.should.eql [
{a: 1, b: 2, c: 3}
Expand All @@ -124,8 +155,8 @@ describe 'Option `cast`', ->
4,5,6
""",
columns: ['a', 'b', 'c']
cast: (value, context) ->
context.header.should.be.false()
cast: (value, {header}) ->
header.should.be.false()
parseInt value
, (err, records) ->
records.should.eql [
Expand All @@ -141,7 +172,7 @@ describe 'Option `cast`', ->
4,5,6
""",
columns: true
cast: (value, context) ->
cast: (value) ->
value
, (err, records) ->
next err
Expand Down

0 comments on commit 8477d8c

Please sign in to comment.