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

Commit

Permalink
feat: expose columns with cast, info, on_record options
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed May 20, 2021
1 parent 6f9e2d6 commit 81c5c87
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 74 deletions.
4 changes: 3 additions & 1 deletion lib/browser/index.js
Expand Up @@ -1452,7 +1452,9 @@ var Parser = /*#__PURE__*/function (_Transform) {
}, {
key: "__infoDataSet",
value: function __infoDataSet() {
return _objectSpread({}, this.info);
return _objectSpread(_objectSpread({}, this.info), {}, {
columns: this.options.columns
});
}
}, {
key: "__infoRecord",
Expand Down
4 changes: 3 additions & 1 deletion lib/browser/sync.js
Expand Up @@ -1452,7 +1452,9 @@ var Parser = /*#__PURE__*/function (_Transform) {
}, {
key: "__infoDataSet",
value: function __infoDataSet() {
return _objectSpread({}, this.info);
return _objectSpread(_objectSpread({}, this.info), {}, {
columns: this.options.columns
});
}
}, {
key: "__infoRecord",
Expand Down
4 changes: 3 additions & 1 deletion lib/es5/index.js
Expand Up @@ -1344,7 +1344,9 @@ var Parser = /*#__PURE__*/function (_Transform) {
}, {
key: "__infoDataSet",
value: function __infoDataSet() {
return _objectSpread({}, this.info);
return _objectSpread(_objectSpread({}, this.info), {}, {
columns: this.options.columns
});
}
}, {
key: "__infoRecord",
Expand Down
5 changes: 4 additions & 1 deletion lib/index.js
Expand Up @@ -1110,7 +1110,10 @@ class Parser extends Transform {
}
}
__infoDataSet(){
return {...this.info}
return {
...this.info,
columns: this.options.columns
}
}
__infoRecord(){
const {columns} = this.options
Expand Down
21 changes: 21 additions & 0 deletions test/api.info.coffee
Expand Up @@ -9,6 +9,7 @@ describe 'API info', ->
a,b,
''', (err, data, info) ->
info.should.eql
columns: false
comment_lines: 0
empty_lines: 0
invalid_field_length: 0
Expand All @@ -22,13 +23,32 @@ describe 'API info', ->
a,b,c
''', (err, data, info) ->
info.should.eql
columns: false
comment_lines: 0
empty_lines: 0
invalid_field_length: 0
lines: 2
records: 2
next err

it 'discovered columns are included', (next) ->
parse '''
a,b,c
1,2,3
''', columns: true, (err, data, info) ->
info.should.eql
comment_lines: 0
columns: [
{ name: 'a' }
{ name: 'b' }
{ name: 'c' }
]
empty_lines: 0
invalid_field_length: 0
lines: 2
records: 1
next err

it 'with multiline records', (next) ->
parse '''
a,b,c
Expand All @@ -37,6 +57,7 @@ describe 'API info', ->
g,h,i
''', (err, data, info) ->
info.should.eql
columns: false
comment_lines: 0
empty_lines: 0
invalid_field_length: 0
Expand Down
9 changes: 5 additions & 4 deletions test/option.cast.coffee
Expand Up @@ -65,8 +65,9 @@ describe 'Option `cast`', ->
, (err, records) ->
records.should.eql [
[[
'column', 'comment_lines', 'empty_lines', 'error', 'header',
'index', 'invalid_field_length', 'lines', 'quoting', 'records'
'column', 'columns', 'comment_lines', 'empty_lines', 'error',
'header', 'index', 'invalid_field_length', 'lines', 'quoting',
'records'
]]
] unless err
next err
Expand All @@ -83,12 +84,12 @@ describe 'Option `cast`', ->
, (err, records) ->
records.should.eql [
[ '2000-01-01T05:00:00.000Z', {
column: 1, comment_lines: 0, empty_lines: 0, error: undefined,
column: 1, columns: false, comment_lines: 0, empty_lines: 0, error: undefined,
header: false, index: 1, invalid_field_length: 0, lines: 1,
quoting: false, records: 0
} ]
[ '2050-11-27T05:00:00.000Z', {
column: 1, comment_lines: 0, empty_lines: 0, error: undefined,
column: 1, columns: false, comment_lines: 0, empty_lines: 0, error: undefined,
header: false, index: 1, invalid_field_length: 0, lines: 2,
quoting: false, records: 1
} ]
Expand Down
2 changes: 1 addition & 1 deletion test/option.info.coffee
Expand Up @@ -29,7 +29,7 @@ describe 'Option `info`', ->
''', info: true, (err, records) ->
{info} = records[0]
Object.keys(info).sort().should.eql [
'comment_lines', 'empty_lines', 'error', 'header',
'columns', 'comment_lines', 'empty_lines', 'error', 'header',
'index', 'invalid_field_length', 'lines', 'records'
]
next err
Expand Down
132 changes: 67 additions & 65 deletions test/option.on_record.coffee
Expand Up @@ -10,71 +10,73 @@ describe 'Option `on_record`', ->
).should.throw
message: 'Invalid option `on_record`: expect a function, got true'
code: 'CSV_INVALID_OPTION_ON_RECORD'

it 'alter records', (next) ->
parse "a,b", on_record: (record) ->
[record[1], record[0]]
, (err, records) ->
records.should.eql [ ['b', 'a'] ] unless err
next err

it 'filter records', (next) ->
parse "a,b\nc,d\ne,f", on_record: (record, {lines}) ->
if lines is 2 then null else record
, (err, records) ->
records.should.eql [ ['a', 'b'], ['e', 'f'] ] unless err
next err

it 'errors with callback', (next) ->
parse "a,b\nc,d\ne,f",
on_record: (record, {lines}) ->
if lines is 2 then throw Error 'Error thrown on line 2' else record
, (err, records) ->
err.message.should.eql 'Error thrown on line 2'
next()

it 'errors with events', (next) ->
parser = parse "a,a,a\nc,d\ne,f"
parser.on 'error', (err) ->
err.message.should.eql 'Invalid Record Length: expect 3, got 2 on line 2'
next()
parser.on 'end', () ->
next Error 'Should not be called'
describe 'usage', ->

it 'errors not handled by skip_lines_with_error', (next) ->
parse "a,b\nc,d\ne,f",
on_record: (record, {lines}) ->
if lines is 2 then throw Error 'Error thrown on line 2' else record
skip_lines_with_error: true
, (err, records) ->
err.message.should.eql 'Error thrown on line 2'
next()
it 'alter records', (next) ->
parse "a,b", on_record: (record) ->
[record[1], record[0]]
, (err, records) ->
records.should.eql [ ['b', 'a'] ] unless err
next err

it 'filter records', (next) ->
parse "a,b\nc,d\ne,f", on_record: (record, {lines}) ->
if lines is 2 then null else record
, (err, records) ->
records.should.eql [ ['a', 'b'], ['e', 'f'] ] unless err
next err

it 'errors with callback', (next) ->
parse "a,b\nc,d\ne,f",
on_record: (record, {lines}) ->
if lines is 2 then throw Error 'Error thrown on line 2' else record
, (err, records) ->
err.message.should.eql 'Error thrown on line 2'
next()

it 'errors with events', (next) ->
parser = parse "a,a,a\nc,d\ne,f"
parser.on 'error', (err) ->
err.message.should.eql 'Invalid Record Length: expect 3, got 2 on line 2'
next()
parser.on 'end', () ->
next Error 'Should not be called'

it 'errors not handled by skip_lines_with_error', (next) ->
parse "a,b\nc,d\ne,f",
on_record: (record, {lines}) ->
if lines is 2 then throw Error 'Error thrown on line 2' else record
skip_lines_with_error: true
, (err, records) ->
err.message.should.eql 'Error thrown on line 2'
next()

describe 'context', ->

it 'properties', (next) ->
parse "a,b",
on_record: (record, context) ->
Object.keys(context).sort()
skip_lines_with_error: true
, (err, records) ->
records.should.eql [[
'comment_lines', 'empty_lines', 'error', 'header',
'index', 'invalid_field_length', 'lines', 'records'
]]
next()

it 'properties', (next) ->
parse "a,b\nc,d",
on_record: (record, context) ->
context
skip_lines_with_error: true
, (err, records) ->
records.should.eql [
comment_lines: 0, empty_lines: 0, error: undefined, header: false
index: 2, invalid_field_length: 0, lines: 1, records: 1
,
comment_lines: 0, empty_lines: 0, error: undefined, header: false
index: 2, invalid_field_length: 0, lines: 2, records: 2
]
next()
describe 'context', ->
it 'properties', (next) ->
parse "a,b",
on_record: (record, context) ->
Object.keys(context).sort()
skip_lines_with_error: true
, (err, records) ->
records.should.eql [[
'columns', 'comment_lines', 'empty_lines', 'error', 'header',
'index', 'invalid_field_length', 'lines', 'records'
]]
next()
it 'values', (next) ->
parse "a,b\nc,d",
on_record: (record, context) ->
context
skip_lines_with_error: true
, (err, records) ->
records.should.eql [
columns: false, comment_lines: 0, empty_lines: 0, error: undefined, header: false
index: 2, invalid_field_length: 0, lines: 1, records: 1
,
columns: false, comment_lines: 0, empty_lines: 0, error: undefined, header: false
index: 2, invalid_field_length: 0, lines: 2, records: 2
]
next()

0 comments on commit 81c5c87

Please sign in to comment.