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

Commit bda1a17

Browse files
committed
feat: cast expose context.empty_lines
1 parent 3113b32 commit bda1a17

File tree

7 files changed

+61
-43
lines changed

7 files changed

+61
-43
lines changed

lib/browser/index.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,17 +1457,13 @@ var Parser = /*#__PURE__*/function (_Transform) {
14571457
value: function __context() {
14581458
var columns = this.options.columns;
14591459
var isColumns = Array.isArray(columns);
1460-
return {
1460+
return _objectSpread(_objectSpread({}, this.info), {
14611461
column: isColumns === true ? columns.length > this.state.record.length ? columns[this.state.record.length].name : null : this.state.record.length,
1462-
empty_lines: this.info.empty_lines,
14631462
error: this.state.error,
14641463
header: columns === true,
1465-
index: this.state.record.length,
1466-
invalid_field_length: this.info.invalid_field_length,
14671464
quoting: this.state.wasQuoting,
1468-
lines: this.info.lines,
1469-
records: this.info.records
1470-
};
1465+
index: this.state.record.length
1466+
});
14711467
}
14721468
}]);
14731469

lib/browser/sync.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,17 +1457,13 @@ var Parser = /*#__PURE__*/function (_Transform) {
14571457
value: function __context() {
14581458
var columns = this.options.columns;
14591459
var isColumns = Array.isArray(columns);
1460-
return {
1460+
return _objectSpread(_objectSpread({}, this.info), {
14611461
column: isColumns === true ? columns.length > this.state.record.length ? columns[this.state.record.length].name : null : this.state.record.length,
1462-
empty_lines: this.info.empty_lines,
14631462
error: this.state.error,
14641463
header: columns === true,
1465-
index: this.state.record.length,
1466-
invalid_field_length: this.info.invalid_field_length,
14671464
quoting: this.state.wasQuoting,
1468-
lines: this.info.lines,
1469-
records: this.info.records
1470-
};
1465+
index: this.state.record.length
1466+
});
14711467
}
14721468
}]);
14731469

lib/es5/index.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,17 +1349,13 @@ var Parser = /*#__PURE__*/function (_Transform) {
13491349
value: function __context() {
13501350
var columns = this.options.columns;
13511351
var isColumns = Array.isArray(columns);
1352-
return {
1352+
return _objectSpread(_objectSpread({}, this.info), {
13531353
column: isColumns === true ? columns.length > this.state.record.length ? columns[this.state.record.length].name : null : this.state.record.length,
1354-
empty_lines: this.info.empty_lines,
13551354
error: this.state.error,
13561355
header: columns === true,
1357-
index: this.state.record.length,
1358-
invalid_field_length: this.info.invalid_field_length,
13591356
quoting: this.state.wasQuoting,
1360-
lines: this.info.lines,
1361-
records: this.info.records
1362-
};
1357+
index: this.state.record.length
1358+
});
13631359
}
13641360
}]);
13651361

lib/index.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,20 +1112,19 @@ class Parser extends Transform {
11121112
const {columns} = this.options
11131113
const isColumns = Array.isArray(columns)
11141114
return {
1115-
column: isColumns === true ?
1116-
( columns.length > this.state.record.length ?
1117-
columns[this.state.record.length].name :
1118-
null
1119-
) :
1120-
this.state.record.length,
1121-
empty_lines: this.info.empty_lines,
1122-
error: this.state.error,
1123-
header: columns === true,
1124-
index: this.state.record.length,
1125-
invalid_field_length: this.info.invalid_field_length,
1126-
quoting: this.state.wasQuoting,
1127-
lines: this.info.lines,
1128-
records: this.info.records
1115+
...this.info,
1116+
...{
1117+
column: isColumns === true ?
1118+
( columns.length > this.state.record.length ?
1119+
columns[this.state.record.length].name :
1120+
null
1121+
) :
1122+
this.state.record.length,
1123+
error: this.state.error,
1124+
header: columns === true,
1125+
quoting: this.state.wasQuoting,
1126+
index: this.state.record.length,
1127+
}
11291128
}
11301129
}
11311130
}

samples/option.info.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
const parse = require('../lib/sync')
3+
const assert = require('assert')
4+
5+
const data = "a,b,c"
6+
const records = parse(data, {
7+
info: true
8+
})
9+
assert.deepStrictEqual(records, [{
10+
info: {
11+
comment_lines: 0,
12+
empty_lines: 0,
13+
invalid_field_length: 0,
14+
lines: 1,
15+
records: 0
16+
},
17+
record: [ 'a', 'b', 'c' ]
18+
}])

test/option.cast.coffee

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,17 @@ describe 'Option `cast`', ->
5656

5757
describe 'function', ->
5858

59-
it.only 'custom function', (next) ->
59+
it 'custom function', (next) ->
6060
parse """
6161
hello
6262
""",
6363
cast: (value, context) ->
64-
Object.keys context
64+
Object.keys(context).sort()
6565
, (err, records) ->
6666
records.should.eql [
6767
[[
68-
'column', 'empty_lines', 'error', 'header', 'index'
69-
'invalid_field_length', 'quoting', 'lines', 'records'
68+
'column', 'comment_lines', 'empty_lines', 'error', 'header',
69+
'index', 'invalid_field_length', 'lines', 'quoting', 'records'
7070
]]
7171
] unless err
7272
next err
@@ -83,12 +83,14 @@ describe 'Option `cast`', ->
8383
, (err, records) ->
8484
records.should.eql [
8585
[ '2000-01-01T05:00:00.000Z', {
86-
column: 1, empty_lines: 0, error: undefined, header: false, index: 1,
87-
invalid_field_length: 0, lines: 1, quoting: false, records: 0
86+
column: 1, comment_lines: 0, empty_lines: 0, error: undefined,
87+
header: false, index: 1, invalid_field_length: 0, lines: 1,
88+
quoting: false, records: 0
8889
} ]
8990
[ '2050-11-27T05:00:00.000Z', {
90-
column: 1, empty_lines: 0, error: undefined, header: false, index: 1,
91-
invalid_field_length: 0, lines: 2, quoting: false, records: 1
91+
column: 1, comment_lines: 0, empty_lines: 0, error: undefined,
92+
header: false, index: 1, invalid_field_length: 0, lines: 2,
93+
quoting: false, records: 1
9294
} ]
9395
] unless err
9496
next err

test/option.on_record.coffee

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,14 @@ describe 'Option `on_record`', ->
4949
, (err, records) ->
5050
err.message.should.eql 'Error thrown on line 2'
5151
next()
52+
53+
describe 'context', ->
54+
55+
it 'properties', (next) ->
56+
parse "a,b\nc,d",
57+
on_record: (record, context) ->
58+
context
59+
skip_lines_with_error: true
60+
, (err, records) ->
61+
console.log records
62+
next()

0 commit comments

Comments
 (0)