From bda1a176237fd53aa6e3526c84e67055b350c3f8 Mon Sep 17 00:00:00 2001 From: David Worms Date: Thu, 20 May 2021 19:24:29 +0200 Subject: [PATCH] feat: cast expose context.empty_lines --- lib/browser/index.js | 10 +++------- lib/browser/sync.js | 10 +++------- lib/es5/index.js | 10 +++------- lib/index.js | 27 +++++++++++++-------------- samples/option.info.js | 18 ++++++++++++++++++ test/option.cast.coffee | 18 ++++++++++-------- test/option.on_record.coffee | 11 +++++++++++ 7 files changed, 61 insertions(+), 43 deletions(-) create mode 100644 samples/option.info.js diff --git a/lib/browser/index.js b/lib/browser/index.js index 3fb1be2..4a867f5 100644 --- a/lib/browser/index.js +++ b/lib/browser/index.js @@ -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 + }); } }]); diff --git a/lib/browser/sync.js b/lib/browser/sync.js index 0604509..c2bdd03 100644 --- a/lib/browser/sync.js +++ b/lib/browser/sync.js @@ -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 + }); } }]); diff --git a/lib/es5/index.js b/lib/es5/index.js index c956d8b..bc1f6b4 100644 --- a/lib/es5/index.js +++ b/lib/es5/index.js @@ -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 + }); } }]); diff --git a/lib/index.js b/lib/index.js index 9f8dced..445e47b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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, + } } } } diff --git a/samples/option.info.js b/samples/option.info.js new file mode 100644 index 0000000..28e9df0 --- /dev/null +++ b/samples/option.info.js @@ -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' ] +}]) diff --git a/test/option.cast.coffee b/test/option.cast.coffee index 08f5d45..266e6b7 100644 --- a/test/option.cast.coffee +++ b/test/option.cast.coffee @@ -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 @@ -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 diff --git a/test/option.on_record.coffee b/test/option.on_record.coffee index 00543fd..5d211e8 100644 --- a/test/option.on_record.coffee +++ b/test/option.on_record.coffee @@ -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()