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

Commit

Permalink
cast: function may return non-string values
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Dec 4, 2018
1 parent 58c90c1 commit 40b0bf0
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 60 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,7 @@ New features:

Fix:

* cast: function may return non-string values
* stream: pass stream options without modification

## Version 4.0.1
Expand Down
10 changes: 7 additions & 3 deletions lib/es5/index.js
Expand Up @@ -716,7 +716,8 @@ function (_Transform) {
value: function __onField() {
var _this$options3 = this.options,
cast = _this$options3.cast,
rtrim = _this$options3.rtrim;
rtrim = _this$options3.rtrim,
max_record_size = _this$options3.max_record_size;
var _this$state3 = this.state,
enabled = _this$state3.enabled,
wasQuoting = _this$state3.wasQuoting; // Deal with from_to options
Expand All @@ -741,8 +742,11 @@ function (_Transform) {
field = f;
}

this.state.record.push(field);
this.state.record_length += field.length;
this.state.record.push(field); // Increment record length if record size must not exceed a limit

if (max_record_size !== 0 && typeof field === 'string') {
this.state.record_length += field.length;
}

this.__resetField();
}
Expand Down
7 changes: 5 additions & 2 deletions lib/index.js
Expand Up @@ -517,7 +517,7 @@ class Parser extends Transform {
this.state.record_length = 0
}
__onField(){
const {cast, rtrim} = this.options
const {cast, rtrim, max_record_size} = this.options
const {enabled, wasQuoting} = this.state
// Deal with from_to options
if(this.options.columns !== true && enabled === false){
Expand All @@ -533,7 +533,10 @@ class Parser extends Transform {
field = f
}
this.state.record.push(field)
this.state.record_length += field.length
// Increment record length if record size must not exceed a limit
if(max_record_size !== 0 && typeof field === 'string'){
this.state.record_length += field.length
}
this.__resetField()
}
__resetField(){
Expand Down
130 changes: 75 additions & 55 deletions test/option.cast.coffee
Expand Up @@ -79,63 +79,83 @@ describe 'Option `cast`', ->
[ false, true ]
] unless err
next err

it 'return undefined', ->

it 'work with record count', (next) ->
parse """
1,2,3
4,5,6
""",
max_record_size: 10
cast: (value, context) ->
switch context.index
when 0
undefined
when 1
false
when 2
null
, (err, records) ->
records.shift().should.eql [undefined, false, null]
next err

describe 'columns', ->
describe 'columns', ->

it 'header is true on first line when columns is true', (next) ->
parse """
a,b,c
1,2,3
4,5,6
""",
columns: true
cast: (value, context) ->
if context.header then value else parseInt value
, (err, records) ->
records.should.eql [
{a: 1, b: 2, c: 3}
{a: 4, b: 5, c: 6}
] unless err
next err
it 'header is true on first line when columns is true', (next) ->
parse """
a,b,c
1,2,3
4,5,6
""",
columns: true
cast: (value, context) ->
if context.header then value else parseInt value
, (err, records) ->
records.should.eql [
{a: 1, b: 2, c: 3}
{a: 4, b: 5, c: 6}
] unless err
next err

it 'header is false when columns is an object', (next) ->
parse """
1,2,3
4,5,6
""",
columns: ['a', 'b', 'c']
cast: (value, context) ->
context.header.should.be.false()
parseInt value
, (err, records) ->
records.should.eql [
{a: 1, b: 2, c: 3}
{a: 4, b: 5, c: 6}
] unless err
next err
it 'header is false when columns is an object', (next) ->
parse """
1,2,3
4,5,6
""",
columns: ['a', 'b', 'c']
cast: (value, context) ->
context.header.should.be.false()
parseInt value
, (err, records) ->
records.should.eql [
{a: 1, b: 2, c: 3}
{a: 4, b: 5, c: 6}
] unless err
next err

it 'dont count header line', (next) ->
parse """
a,b,c
1,2,3
4,5,6
""",
columns: true
cast: (value, context) ->
value
, (err, records) ->
next err
it 'dont count header line', (next) ->
parse """
a,b,c
1,2,3
4,5,6
""",
columns: true
cast: (value, context) ->
value
, (err, records) ->
next err

describe 'error', ->
it 'catch error', (next) ->
parse """
1,2,3
4,5,6
""",
cast: (value) ->
if value is '6' then throw Error 'Catchme'
value
, (err, records) ->
err.message.should.eql 'Catchme'
next()
describe 'error', ->

it 'catch error', (next) ->
parse """
1,2,3
4,5,6
""",
cast: (value) ->
if value is '6' then throw Error 'Catchme'
value
, (err, records) ->
err.message.should.eql 'Catchme'
next()

0 comments on commit 40b0bf0

Please sign in to comment.