From 7d84199b29ea250cd742e3ff42391a39525cf6d1 Mon Sep 17 00:00:00 2001 From: David Worms Date: Thu, 20 May 2021 19:51:06 +0200 Subject: [PATCH] fix: info print the number of encountered line when emited --- lib/browser/index.js | 26 +++++++++++++------------- lib/browser/sync.js | 26 +++++++++++++------------- lib/es5/index.js | 26 +++++++++++++------------- lib/index.js | 16 ++++++++-------- test/api.info.coffee | 15 +++++++++++++++ test/option.info.coffee | 2 +- 6 files changed, 63 insertions(+), 48 deletions(-) diff --git a/lib/browser/index.js b/lib/browser/index.js index 4a867f5..19e17b3 100644 --- a/lib/browser/index.js +++ b/lib/browser/index.js @@ -624,7 +624,7 @@ var Parser = /*#__PURE__*/function (_Transform) { expectedRecordLength: Array.isArray(options.columns) ? options.columns.length : undefined, field: new ResizeableBuffer(20), firstLineToHeaders: fnFirstLineToHeaders, - info: Object.assign({}, this.info), + // info: Object.assign({}, this.info), needMoreDataSize: Math.max.apply(Math, [// Skip if the remaining buffer smaller than comment options.comment !== null ? options.comment.length : 0].concat(_toConsumableArray(options.delimiter.map(function (delimiter) { return delimiter.length; @@ -682,7 +682,6 @@ var Parser = /*#__PURE__*/function (_Transform) { comment = _this$options.comment, escape = _this$options.escape, from_line = _this$options.from_line, - info = _this$options.info, ltrim = _this$options.ltrim, max_record_size = _this$options.max_record_size, quote = _this$options.quote, @@ -754,11 +753,9 @@ var Parser = /*#__PURE__*/function (_Transform) { } if (this.state.wasRowDelimiter === true) { - this.info.lines++; - - if (info === true && this.state.record.length === 0 && this.state.field.length === 0 && this.state.wasQuoting === false) { - this.state.info = Object.assign({}, this.info); - } + this.info.lines++; // if(info === true && this.state.record.length === 0 && this.state.field.length === 0 && this.state.wasQuoting === false){ + // this.state.info = Object.assign({}, this.info) + // } this.state.wasRowDelimiter = false; } @@ -1061,6 +1058,7 @@ var Parser = /*#__PURE__*/function (_Transform) { this.info.records++; if (from === 1 || this.info.records >= from) { + // With columns, records are object if (columns !== false) { var obj = {}; // Transform record array to an object @@ -1078,7 +1076,7 @@ var Parser = /*#__PURE__*/function (_Transform) { } } - var objname = this.options.objname; + var objname = this.options.objname; // Without objname (default) if (objname === undefined) { if (raw === true || info === true) { @@ -1087,7 +1085,7 @@ var Parser = /*#__PURE__*/function (_Transform) { }, raw === true ? { raw: this.state.rawBuffer.toString(encoding) } : {}, info === true ? { - info: this.state.info + info: _objectSpread({}, this.info) } : {})); if (_err5) { @@ -1099,7 +1097,8 @@ var Parser = /*#__PURE__*/function (_Transform) { if (_err6) { return _err6; } - } + } // With objname (default) + } else { if (raw === true || info === true) { var _err7 = this.__push(Object.assign({ @@ -1107,7 +1106,7 @@ var Parser = /*#__PURE__*/function (_Transform) { }, raw === true ? { raw: this.state.rawBuffer.toString(encoding) } : {}, info === true ? { - info: this.state.info + info: _objectSpread({}, this.info) } : {})); if (_err7) { @@ -1120,7 +1119,8 @@ var Parser = /*#__PURE__*/function (_Transform) { return _err8; } } - } + } // Without columns, records are array + } else { if (raw === true || info === true) { var _err9 = this.__push(Object.assign({ @@ -1128,7 +1128,7 @@ var Parser = /*#__PURE__*/function (_Transform) { }, raw === true ? { raw: this.state.rawBuffer.toString(encoding) } : {}, info === true ? { - info: this.state.info + info: _objectSpread({}, this.info) } : {})); if (_err9) { diff --git a/lib/browser/sync.js b/lib/browser/sync.js index c2bdd03..08a81ed 100644 --- a/lib/browser/sync.js +++ b/lib/browser/sync.js @@ -624,7 +624,7 @@ var Parser = /*#__PURE__*/function (_Transform) { expectedRecordLength: Array.isArray(options.columns) ? options.columns.length : undefined, field: new ResizeableBuffer(20), firstLineToHeaders: fnFirstLineToHeaders, - info: Object.assign({}, this.info), + // info: Object.assign({}, this.info), needMoreDataSize: Math.max.apply(Math, [// Skip if the remaining buffer smaller than comment options.comment !== null ? options.comment.length : 0].concat(_toConsumableArray(options.delimiter.map(function (delimiter) { return delimiter.length; @@ -682,7 +682,6 @@ var Parser = /*#__PURE__*/function (_Transform) { comment = _this$options.comment, escape = _this$options.escape, from_line = _this$options.from_line, - info = _this$options.info, ltrim = _this$options.ltrim, max_record_size = _this$options.max_record_size, quote = _this$options.quote, @@ -754,11 +753,9 @@ var Parser = /*#__PURE__*/function (_Transform) { } if (this.state.wasRowDelimiter === true) { - this.info.lines++; - - if (info === true && this.state.record.length === 0 && this.state.field.length === 0 && this.state.wasQuoting === false) { - this.state.info = Object.assign({}, this.info); - } + this.info.lines++; // if(info === true && this.state.record.length === 0 && this.state.field.length === 0 && this.state.wasQuoting === false){ + // this.state.info = Object.assign({}, this.info) + // } this.state.wasRowDelimiter = false; } @@ -1061,6 +1058,7 @@ var Parser = /*#__PURE__*/function (_Transform) { this.info.records++; if (from === 1 || this.info.records >= from) { + // With columns, records are object if (columns !== false) { var obj = {}; // Transform record array to an object @@ -1078,7 +1076,7 @@ var Parser = /*#__PURE__*/function (_Transform) { } } - var objname = this.options.objname; + var objname = this.options.objname; // Without objname (default) if (objname === undefined) { if (raw === true || info === true) { @@ -1087,7 +1085,7 @@ var Parser = /*#__PURE__*/function (_Transform) { }, raw === true ? { raw: this.state.rawBuffer.toString(encoding) } : {}, info === true ? { - info: this.state.info + info: _objectSpread({}, this.info) } : {})); if (_err5) { @@ -1099,7 +1097,8 @@ var Parser = /*#__PURE__*/function (_Transform) { if (_err6) { return _err6; } - } + } // With objname (default) + } else { if (raw === true || info === true) { var _err7 = this.__push(Object.assign({ @@ -1107,7 +1106,7 @@ var Parser = /*#__PURE__*/function (_Transform) { }, raw === true ? { raw: this.state.rawBuffer.toString(encoding) } : {}, info === true ? { - info: this.state.info + info: _objectSpread({}, this.info) } : {})); if (_err7) { @@ -1120,7 +1119,8 @@ var Parser = /*#__PURE__*/function (_Transform) { return _err8; } } - } + } // Without columns, records are array + } else { if (raw === true || info === true) { var _err9 = this.__push(Object.assign({ @@ -1128,7 +1128,7 @@ var Parser = /*#__PURE__*/function (_Transform) { }, raw === true ? { raw: this.state.rawBuffer.toString(encoding) } : {}, info === true ? { - info: this.state.info + info: _objectSpread({}, this.info) } : {})); if (_err9) { diff --git a/lib/es5/index.js b/lib/es5/index.js index bc1f6b4..14ecb0c 100644 --- a/lib/es5/index.js +++ b/lib/es5/index.js @@ -516,7 +516,7 @@ var Parser = /*#__PURE__*/function (_Transform) { expectedRecordLength: Array.isArray(options.columns) ? options.columns.length : undefined, field: new ResizeableBuffer(20), firstLineToHeaders: fnFirstLineToHeaders, - info: Object.assign({}, this.info), + // info: Object.assign({}, this.info), needMoreDataSize: Math.max.apply(Math, [// Skip if the remaining buffer smaller than comment options.comment !== null ? options.comment.length : 0].concat(_toConsumableArray(options.delimiter.map(function (delimiter) { return delimiter.length; @@ -574,7 +574,6 @@ var Parser = /*#__PURE__*/function (_Transform) { comment = _this$options.comment, escape = _this$options.escape, from_line = _this$options.from_line, - info = _this$options.info, ltrim = _this$options.ltrim, max_record_size = _this$options.max_record_size, quote = _this$options.quote, @@ -646,11 +645,9 @@ var Parser = /*#__PURE__*/function (_Transform) { } if (this.state.wasRowDelimiter === true) { - this.info.lines++; - - if (info === true && this.state.record.length === 0 && this.state.field.length === 0 && this.state.wasQuoting === false) { - this.state.info = Object.assign({}, this.info); - } + this.info.lines++; // if(info === true && this.state.record.length === 0 && this.state.field.length === 0 && this.state.wasQuoting === false){ + // this.state.info = Object.assign({}, this.info) + // } this.state.wasRowDelimiter = false; } @@ -953,6 +950,7 @@ var Parser = /*#__PURE__*/function (_Transform) { this.info.records++; if (from === 1 || this.info.records >= from) { + // With columns, records are object if (columns !== false) { var obj = {}; // Transform record array to an object @@ -970,7 +968,7 @@ var Parser = /*#__PURE__*/function (_Transform) { } } - var objname = this.options.objname; + var objname = this.options.objname; // Without objname (default) if (objname === undefined) { if (raw === true || info === true) { @@ -979,7 +977,7 @@ var Parser = /*#__PURE__*/function (_Transform) { }, raw === true ? { raw: this.state.rawBuffer.toString(encoding) } : {}, info === true ? { - info: this.state.info + info: _objectSpread({}, this.info) } : {})); if (_err5) { @@ -991,7 +989,8 @@ var Parser = /*#__PURE__*/function (_Transform) { if (_err6) { return _err6; } - } + } // With objname (default) + } else { if (raw === true || info === true) { var _err7 = this.__push(Object.assign({ @@ -999,7 +998,7 @@ var Parser = /*#__PURE__*/function (_Transform) { }, raw === true ? { raw: this.state.rawBuffer.toString(encoding) } : {}, info === true ? { - info: this.state.info + info: _objectSpread({}, this.info) } : {})); if (_err7) { @@ -1012,7 +1011,8 @@ var Parser = /*#__PURE__*/function (_Transform) { return _err8; } } - } + } // Without columns, records are array + } else { if (raw === true || info === true) { var _err9 = this.__push(Object.assign({ @@ -1020,7 +1020,7 @@ var Parser = /*#__PURE__*/function (_Transform) { }, raw === true ? { raw: this.state.rawBuffer.toString(encoding) } : {}, info === true ? { - info: this.state.info + info: _objectSpread({}, this.info) } : {})); if (_err9) { diff --git a/lib/index.js b/lib/index.js index 445e47b..a906f82 100644 --- a/lib/index.js +++ b/lib/index.js @@ -443,7 +443,6 @@ class Parser extends Transform { expectedRecordLength: Array.isArray(options.columns) ? options.columns.length : undefined, field: new ResizeableBuffer(20), firstLineToHeaders: fnFirstLineToHeaders, - info: Object.assign({}, this.info), needMoreDataSize: Math.max( // Skip if the remaining buffer smaller than comment options.comment !== null ? options.comment.length : 0, @@ -486,7 +485,7 @@ class Parser extends Transform { } // Central parser implementation __parse(nextBuf, end){ - const {bom, comment, escape, from_line, info, ltrim, max_record_size, quote, raw, relax, rtrim, skip_empty_lines, to, to_line} = this.options + const {bom, comment, escape, from_line, ltrim, max_record_size, quote, raw, relax, rtrim, skip_empty_lines, to, to_line} = this.options let {record_delimiter} = this.options const {bomSkipped, previousBuf, rawBuffer, escapeIsQuote} = this.state let buf @@ -537,9 +536,6 @@ class Parser extends Transform { } if(this.state.wasRowDelimiter === true){ this.info.lines++ - if(info === true && this.state.record.length === 0 && this.state.field.length === 0 && this.state.wasQuoting === false){ - this.state.info = Object.assign({}, this.info) - } this.state.wasRowDelimiter = false } if(to_line !== -1 && this.info.lines > to_line){ @@ -813,6 +809,7 @@ class Parser extends Transform { } this.info.records++ if(from === 1 || this.info.records >= from){ + // With columns, records are object if(columns !== false){ const obj = {} // Transform record array to an object @@ -830,12 +827,13 @@ class Parser extends Transform { } } const {objname} = this.options + // Without objname (default) if(objname === undefined){ if(raw === true || info === true){ const err = this.__push(Object.assign( {record: obj}, (raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}), - (info === true ? {info: this.state.info}: {}) + (info === true ? {info: {...this.info}}: {}) )) if(err){ return err @@ -846,12 +844,13 @@ class Parser extends Transform { return err } } + // With objname (default) }else{ if(raw === true || info === true){ const err = this.__push(Object.assign( {record: [obj[objname], obj]}, raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}, - info === true ? {info: this.state.info}: {} + info === true ? {info: {...this.info}}: {} )) if(err){ return err @@ -863,12 +862,13 @@ class Parser extends Transform { } } } + // Without columns, records are array }else{ if(raw === true || info === true){ const err = this.__push(Object.assign( {record: record}, raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}, - info === true ? {info: this.state.info}: {} + info === true ? {info: {...this.info}}: {} )) if(err){ return err diff --git a/test/api.info.coffee b/test/api.info.coffee index 2831a59..15d5dd0 100644 --- a/test/api.info.coffee +++ b/test/api.info.coffee @@ -28,3 +28,18 @@ describe 'API info', -> lines: 2 records: 2 next err + + it 'with multiline records', (next) -> + parse ''' + a,b,c + d,"e + ",f + g,h,i + ''', (err, data, info) -> + info.should.eql + comment_lines: 0 + empty_lines: 0 + invalid_field_length: 0 + lines: 4 + records: 3 + next err diff --git a/test/option.info.coffee b/test/option.info.coffee index 1b17b84..295c08b 100644 --- a/test/option.info.coffee +++ b/test/option.info.coffee @@ -60,6 +60,6 @@ describe 'Option `info`', -> ''', info: true, (err, records) -> records.map( ({info}) -> info.lines - ).should.eql [1, 2, 4] unless err + ).should.eql [1, 3, 4] unless err next err