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

Commit 81c5c87

Browse files
committed
feat: expose columns with cast, info, on_record options
1 parent 6f9e2d6 commit 81c5c87

File tree

8 files changed

+107
-74
lines changed

8 files changed

+107
-74
lines changed

lib/browser/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1452,7 +1452,9 @@ var Parser = /*#__PURE__*/function (_Transform) {
14521452
}, {
14531453
key: "__infoDataSet",
14541454
value: function __infoDataSet() {
1455-
return _objectSpread({}, this.info);
1455+
return _objectSpread(_objectSpread({}, this.info), {}, {
1456+
columns: this.options.columns
1457+
});
14561458
}
14571459
}, {
14581460
key: "__infoRecord",

lib/browser/sync.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1452,7 +1452,9 @@ var Parser = /*#__PURE__*/function (_Transform) {
14521452
}, {
14531453
key: "__infoDataSet",
14541454
value: function __infoDataSet() {
1455-
return _objectSpread({}, this.info);
1455+
return _objectSpread(_objectSpread({}, this.info), {}, {
1456+
columns: this.options.columns
1457+
});
14561458
}
14571459
}, {
14581460
key: "__infoRecord",

lib/es5/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1344,7 +1344,9 @@ var Parser = /*#__PURE__*/function (_Transform) {
13441344
}, {
13451345
key: "__infoDataSet",
13461346
value: function __infoDataSet() {
1347-
return _objectSpread({}, this.info);
1347+
return _objectSpread(_objectSpread({}, this.info), {}, {
1348+
columns: this.options.columns
1349+
});
13481350
}
13491351
}, {
13501352
key: "__infoRecord",

lib/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,10 @@ class Parser extends Transform {
11101110
}
11111111
}
11121112
__infoDataSet(){
1113-
return {...this.info}
1113+
return {
1114+
...this.info,
1115+
columns: this.options.columns
1116+
}
11141117
}
11151118
__infoRecord(){
11161119
const {columns} = this.options

test/api.info.coffee

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe 'API info', ->
99
a,b,
1010
''', (err, data, info) ->
1111
info.should.eql
12+
columns: false
1213
comment_lines: 0
1314
empty_lines: 0
1415
invalid_field_length: 0
@@ -22,13 +23,32 @@ describe 'API info', ->
2223
a,b,c
2324
''', (err, data, info) ->
2425
info.should.eql
26+
columns: false
2527
comment_lines: 0
2628
empty_lines: 0
2729
invalid_field_length: 0
2830
lines: 2
2931
records: 2
3032
next err
3133

34+
it 'discovered columns are included', (next) ->
35+
parse '''
36+
a,b,c
37+
1,2,3
38+
''', columns: true, (err, data, info) ->
39+
info.should.eql
40+
comment_lines: 0
41+
columns: [
42+
{ name: 'a' }
43+
{ name: 'b' }
44+
{ name: 'c' }
45+
]
46+
empty_lines: 0
47+
invalid_field_length: 0
48+
lines: 2
49+
records: 1
50+
next err
51+
3252
it 'with multiline records', (next) ->
3353
parse '''
3454
a,b,c
@@ -37,6 +57,7 @@ describe 'API info', ->
3757
g,h,i
3858
''', (err, data, info) ->
3959
info.should.eql
60+
columns: false
4061
comment_lines: 0
4162
empty_lines: 0
4263
invalid_field_length: 0

test/option.cast.coffee

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ describe 'Option `cast`', ->
6565
, (err, records) ->
6666
records.should.eql [
6767
[[
68-
'column', 'comment_lines', 'empty_lines', 'error', 'header',
69-
'index', 'invalid_field_length', 'lines', 'quoting', 'records'
68+
'column', 'columns', 'comment_lines', 'empty_lines', 'error',
69+
'header', 'index', 'invalid_field_length', 'lines', 'quoting',
70+
'records'
7071
]]
7172
] unless err
7273
next err
@@ -83,12 +84,12 @@ describe 'Option `cast`', ->
8384
, (err, records) ->
8485
records.should.eql [
8586
[ '2000-01-01T05:00:00.000Z', {
86-
column: 1, comment_lines: 0, empty_lines: 0, error: undefined,
87+
column: 1, columns: false, comment_lines: 0, empty_lines: 0, error: undefined,
8788
header: false, index: 1, invalid_field_length: 0, lines: 1,
8889
quoting: false, records: 0
8990
} ]
9091
[ '2050-11-27T05:00:00.000Z', {
91-
column: 1, comment_lines: 0, empty_lines: 0, error: undefined,
92+
column: 1, columns: false, comment_lines: 0, empty_lines: 0, error: undefined,
9293
header: false, index: 1, invalid_field_length: 0, lines: 2,
9394
quoting: false, records: 1
9495
} ]

test/option.info.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe 'Option `info`', ->
2929
''', info: true, (err, records) ->
3030
{info} = records[0]
3131
Object.keys(info).sort().should.eql [
32-
'comment_lines', 'empty_lines', 'error', 'header',
32+
'columns', 'comment_lines', 'empty_lines', 'error', 'header',
3333
'index', 'invalid_field_length', 'lines', 'records'
3434
]
3535
next err

test/option.on_record.coffee

Lines changed: 67 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -10,71 +10,73 @@ describe 'Option `on_record`', ->
1010
).should.throw
1111
message: 'Invalid option `on_record`: expect a function, got true'
1212
code: 'CSV_INVALID_OPTION_ON_RECORD'
13-
14-
it 'alter records', (next) ->
15-
parse "a,b", on_record: (record) ->
16-
[record[1], record[0]]
17-
, (err, records) ->
18-
records.should.eql [ ['b', 'a'] ] unless err
19-
next err
20-
21-
it 'filter records', (next) ->
22-
parse "a,b\nc,d\ne,f", on_record: (record, {lines}) ->
23-
if lines is 2 then null else record
24-
, (err, records) ->
25-
records.should.eql [ ['a', 'b'], ['e', 'f'] ] unless err
26-
next err
27-
28-
it 'errors with callback', (next) ->
29-
parse "a,b\nc,d\ne,f",
30-
on_record: (record, {lines}) ->
31-
if lines is 2 then throw Error 'Error thrown on line 2' else record
32-
, (err, records) ->
33-
err.message.should.eql 'Error thrown on line 2'
34-
next()
3513

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

44-
it 'errors not handled by skip_lines_with_error', (next) ->
45-
parse "a,b\nc,d\ne,f",
46-
on_record: (record, {lines}) ->
47-
if lines is 2 then throw Error 'Error thrown on line 2' else record
48-
skip_lines_with_error: true
49-
, (err, records) ->
50-
err.message.should.eql 'Error thrown on line 2'
51-
next()
16+
it 'alter records', (next) ->
17+
parse "a,b", on_record: (record) ->
18+
[record[1], record[0]]
19+
, (err, records) ->
20+
records.should.eql [ ['b', 'a'] ] unless err
21+
next err
22+
23+
it 'filter records', (next) ->
24+
parse "a,b\nc,d\ne,f", on_record: (record, {lines}) ->
25+
if lines is 2 then null else record
26+
, (err, records) ->
27+
records.should.eql [ ['a', 'b'], ['e', 'f'] ] unless err
28+
next err
29+
30+
it 'errors with callback', (next) ->
31+
parse "a,b\nc,d\ne,f",
32+
on_record: (record, {lines}) ->
33+
if lines is 2 then throw Error 'Error thrown on line 2' else record
34+
, (err, records) ->
35+
err.message.should.eql 'Error thrown on line 2'
36+
next()
37+
38+
it 'errors with events', (next) ->
39+
parser = parse "a,a,a\nc,d\ne,f"
40+
parser.on 'error', (err) ->
41+
err.message.should.eql 'Invalid Record Length: expect 3, got 2 on line 2'
42+
next()
43+
parser.on 'end', () ->
44+
next Error 'Should not be called'
45+
46+
it 'errors not handled by skip_lines_with_error', (next) ->
47+
parse "a,b\nc,d\ne,f",
48+
on_record: (record, {lines}) ->
49+
if lines is 2 then throw Error 'Error thrown on line 2' else record
50+
skip_lines_with_error: true
51+
, (err, records) ->
52+
err.message.should.eql 'Error thrown on line 2'
53+
next()
5254

53-
describe 'context', ->
54-
55-
it 'properties', (next) ->
56-
parse "a,b",
57-
on_record: (record, context) ->
58-
Object.keys(context).sort()
59-
skip_lines_with_error: true
60-
, (err, records) ->
61-
records.should.eql [[
62-
'comment_lines', 'empty_lines', 'error', 'header',
63-
'index', 'invalid_field_length', 'lines', 'records'
64-
]]
65-
next()
66-
67-
it 'properties', (next) ->
68-
parse "a,b\nc,d",
69-
on_record: (record, context) ->
70-
context
71-
skip_lines_with_error: true
72-
, (err, records) ->
73-
records.should.eql [
74-
comment_lines: 0, empty_lines: 0, error: undefined, header: false
75-
index: 2, invalid_field_length: 0, lines: 1, records: 1
76-
,
77-
comment_lines: 0, empty_lines: 0, error: undefined, header: false
78-
index: 2, invalid_field_length: 0, lines: 2, records: 2
79-
]
80-
next()
55+
describe 'context', ->
56+
57+
it 'properties', (next) ->
58+
parse "a,b",
59+
on_record: (record, context) ->
60+
Object.keys(context).sort()
61+
skip_lines_with_error: true
62+
, (err, records) ->
63+
records.should.eql [[
64+
'columns', 'comment_lines', 'empty_lines', 'error', 'header',
65+
'index', 'invalid_field_length', 'lines', 'records'
66+
]]
67+
next()
68+
69+
it 'values', (next) ->
70+
parse "a,b\nc,d",
71+
on_record: (record, context) ->
72+
context
73+
skip_lines_with_error: true
74+
, (err, records) ->
75+
records.should.eql [
76+
columns: false, comment_lines: 0, empty_lines: 0, error: undefined, header: false
77+
index: 2, invalid_field_length: 0, lines: 1, records: 1
78+
,
79+
columns: false, comment_lines: 0, empty_lines: 0, error: undefined, header: false
80+
index: 2, invalid_field_length: 0, lines: 2, records: 2
81+
]
82+
next()

0 commit comments

Comments
 (0)