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

Commit

Permalink
fix: info print the number of encountered line when emited
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed May 20, 2021
1 parent bda1a17 commit 7d84199
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 48 deletions.
26 changes: 13 additions & 13 deletions lib/browser/index.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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

Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -1099,15 +1097,16 @@ var Parser = /*#__PURE__*/function (_Transform) {
if (_err6) {
return _err6;
}
}
} // With objname (default)

} else {
if (raw === true || info === true) {
var _err7 = this.__push(Object.assign({
record: [obj[objname], obj]
}, raw === true ? {
raw: this.state.rawBuffer.toString(encoding)
} : {}, info === true ? {
info: this.state.info
info: _objectSpread({}, this.info)
} : {}));

if (_err7) {
Expand All @@ -1120,15 +1119,16 @@ var Parser = /*#__PURE__*/function (_Transform) {
return _err8;
}
}
}
} // Without columns, records are array

} else {
if (raw === true || info === true) {
var _err9 = this.__push(Object.assign({
record: record
}, raw === true ? {
raw: this.state.rawBuffer.toString(encoding)
} : {}, info === true ? {
info: this.state.info
info: _objectSpread({}, this.info)
} : {}));

if (_err9) {
Expand Down
26 changes: 13 additions & 13 deletions lib/browser/sync.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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

Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -1099,15 +1097,16 @@ var Parser = /*#__PURE__*/function (_Transform) {
if (_err6) {
return _err6;
}
}
} // With objname (default)

} else {
if (raw === true || info === true) {
var _err7 = this.__push(Object.assign({
record: [obj[objname], obj]
}, raw === true ? {
raw: this.state.rawBuffer.toString(encoding)
} : {}, info === true ? {
info: this.state.info
info: _objectSpread({}, this.info)
} : {}));

if (_err7) {
Expand All @@ -1120,15 +1119,16 @@ var Parser = /*#__PURE__*/function (_Transform) {
return _err8;
}
}
}
} // Without columns, records are array

} else {
if (raw === true || info === true) {
var _err9 = this.__push(Object.assign({
record: record
}, raw === true ? {
raw: this.state.rawBuffer.toString(encoding)
} : {}, info === true ? {
info: this.state.info
info: _objectSpread({}, this.info)
} : {}));

if (_err9) {
Expand Down
26 changes: 13 additions & 13 deletions lib/es5/index.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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

Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -991,15 +989,16 @@ var Parser = /*#__PURE__*/function (_Transform) {
if (_err6) {
return _err6;
}
}
} // With objname (default)

} else {
if (raw === true || info === true) {
var _err7 = this.__push(Object.assign({
record: [obj[objname], obj]
}, raw === true ? {
raw: this.state.rawBuffer.toString(encoding)
} : {}, info === true ? {
info: this.state.info
info: _objectSpread({}, this.info)
} : {}));

if (_err7) {
Expand All @@ -1012,15 +1011,16 @@ var Parser = /*#__PURE__*/function (_Transform) {
return _err8;
}
}
}
} // Without columns, records are array

} else {
if (raw === true || info === true) {
var _err9 = this.__push(Object.assign({
record: record
}, raw === true ? {
raw: this.state.rawBuffer.toString(encoding)
} : {}, info === true ? {
info: this.state.info
info: _objectSpread({}, this.info)
} : {}));

if (_err9) {
Expand Down
16 changes: 8 additions & 8 deletions lib/index.js
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
15 changes: 15 additions & 0 deletions test/api.info.coffee
Expand Up @@ -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
2 changes: 1 addition & 1 deletion test/option.info.coffee
Expand Up @@ -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

0 comments on commit 7d84199

Please sign in to comment.