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

Commit

Permalink
feat: cast expose context.empty_lines
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed May 20, 2021
1 parent 3113b32 commit bda1a17
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 43 deletions.
10 changes: 3 additions & 7 deletions lib/browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1457,17 +1457,13 @@ var Parser = /*#__PURE__*/function (_Transform) {
value: function __context() {
var columns = this.options.columns;
var isColumns = Array.isArray(columns);
return {
return _objectSpread(_objectSpread({}, this.info), {
column: isColumns === true ? columns.length > this.state.record.length ? columns[this.state.record.length].name : null : this.state.record.length,
empty_lines: this.info.empty_lines,
error: this.state.error,
header: columns === true,
index: this.state.record.length,
invalid_field_length: this.info.invalid_field_length,
quoting: this.state.wasQuoting,
lines: this.info.lines,
records: this.info.records
};
index: this.state.record.length
});
}
}]);

Expand Down
10 changes: 3 additions & 7 deletions lib/browser/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -1457,17 +1457,13 @@ var Parser = /*#__PURE__*/function (_Transform) {
value: function __context() {
var columns = this.options.columns;
var isColumns = Array.isArray(columns);
return {
return _objectSpread(_objectSpread({}, this.info), {
column: isColumns === true ? columns.length > this.state.record.length ? columns[this.state.record.length].name : null : this.state.record.length,
empty_lines: this.info.empty_lines,
error: this.state.error,
header: columns === true,
index: this.state.record.length,
invalid_field_length: this.info.invalid_field_length,
quoting: this.state.wasQuoting,
lines: this.info.lines,
records: this.info.records
};
index: this.state.record.length
});
}
}]);

Expand Down
10 changes: 3 additions & 7 deletions lib/es5/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1349,17 +1349,13 @@ var Parser = /*#__PURE__*/function (_Transform) {
value: function __context() {
var columns = this.options.columns;
var isColumns = Array.isArray(columns);
return {
return _objectSpread(_objectSpread({}, this.info), {
column: isColumns === true ? columns.length > this.state.record.length ? columns[this.state.record.length].name : null : this.state.record.length,
empty_lines: this.info.empty_lines,
error: this.state.error,
header: columns === true,
index: this.state.record.length,
invalid_field_length: this.info.invalid_field_length,
quoting: this.state.wasQuoting,
lines: this.info.lines,
records: this.info.records
};
index: this.state.record.length
});
}
}]);

Expand Down
27 changes: 13 additions & 14 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1112,20 +1112,19 @@ class Parser extends Transform {
const {columns} = this.options
const isColumns = Array.isArray(columns)
return {
column: isColumns === true ?
( columns.length > this.state.record.length ?
columns[this.state.record.length].name :
null
) :
this.state.record.length,
empty_lines: this.info.empty_lines,
error: this.state.error,
header: columns === true,
index: this.state.record.length,
invalid_field_length: this.info.invalid_field_length,
quoting: this.state.wasQuoting,
lines: this.info.lines,
records: this.info.records
...this.info,
...{
column: isColumns === true ?
( columns.length > this.state.record.length ?
columns[this.state.record.length].name :
null
) :
this.state.record.length,
error: this.state.error,
header: columns === true,
quoting: this.state.wasQuoting,
index: this.state.record.length,
}
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions samples/option.info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

const parse = require('../lib/sync')
const assert = require('assert')

const data = "a,b,c"
const records = parse(data, {
info: true
})
assert.deepStrictEqual(records, [{
info: {
comment_lines: 0,
empty_lines: 0,
invalid_field_length: 0,
lines: 1,
records: 0
},
record: [ 'a', 'b', 'c' ]
}])
18 changes: 10 additions & 8 deletions test/option.cast.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ describe 'Option `cast`', ->

describe 'function', ->

it.only 'custom function', (next) ->
it 'custom function', (next) ->
parse """
hello
""",
cast: (value, context) ->
Object.keys context
Object.keys(context).sort()
, (err, records) ->
records.should.eql [
[[
'column', 'empty_lines', 'error', 'header', 'index'
'invalid_field_length', 'quoting', 'lines', 'records'
'column', 'comment_lines', 'empty_lines', 'error', 'header',
'index', 'invalid_field_length', 'lines', 'quoting', 'records'
]]
] unless err
next err
Expand All @@ -83,12 +83,14 @@ describe 'Option `cast`', ->
, (err, records) ->
records.should.eql [
[ '2000-01-01T05:00:00.000Z', {
column: 1, empty_lines: 0, error: undefined, header: false, index: 1,
invalid_field_length: 0, lines: 1, quoting: false, records: 0
column: 1, 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, empty_lines: 0, error: undefined, header: false, index: 1,
invalid_field_length: 0, lines: 2, quoting: false, records: 1
column: 1, comment_lines: 0, empty_lines: 0, error: undefined,
header: false, index: 1, invalid_field_length: 0, lines: 2,
quoting: false, records: 1
} ]
] unless err
next err
Expand Down
11 changes: 11 additions & 0 deletions test/option.on_record.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,14 @@ describe 'Option `on_record`', ->
, (err, records) ->
err.message.should.eql 'Error thrown on line 2'
next()

describe 'context', ->

it 'properties', (next) ->
parse "a,b\nc,d",
on_record: (record, context) ->
context
skip_lines_with_error: true
, (err, records) ->
console.log records
next()

0 comments on commit bda1a17

Please sign in to comment.