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

Commit 7d84199

Browse files
committed
fix: info print the number of encountered line when emited
1 parent bda1a17 commit 7d84199

File tree

6 files changed

+63
-48
lines changed

6 files changed

+63
-48
lines changed

lib/browser/index.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
624624
expectedRecordLength: Array.isArray(options.columns) ? options.columns.length : undefined,
625625
field: new ResizeableBuffer(20),
626626
firstLineToHeaders: fnFirstLineToHeaders,
627-
info: Object.assign({}, this.info),
627+
// info: Object.assign({}, this.info),
628628
needMoreDataSize: Math.max.apply(Math, [// Skip if the remaining buffer smaller than comment
629629
options.comment !== null ? options.comment.length : 0].concat(_toConsumableArray(options.delimiter.map(function (delimiter) {
630630
return delimiter.length;
@@ -682,7 +682,6 @@ var Parser = /*#__PURE__*/function (_Transform) {
682682
comment = _this$options.comment,
683683
escape = _this$options.escape,
684684
from_line = _this$options.from_line,
685-
info = _this$options.info,
686685
ltrim = _this$options.ltrim,
687686
max_record_size = _this$options.max_record_size,
688687
quote = _this$options.quote,
@@ -754,11 +753,9 @@ var Parser = /*#__PURE__*/function (_Transform) {
754753
}
755754

756755
if (this.state.wasRowDelimiter === true) {
757-
this.info.lines++;
758-
759-
if (info === true && this.state.record.length === 0 && this.state.field.length === 0 && this.state.wasQuoting === false) {
760-
this.state.info = Object.assign({}, this.info);
761-
}
756+
this.info.lines++; // if(info === true && this.state.record.length === 0 && this.state.field.length === 0 && this.state.wasQuoting === false){
757+
// this.state.info = Object.assign({}, this.info)
758+
// }
762759

763760
this.state.wasRowDelimiter = false;
764761
}
@@ -1061,6 +1058,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
10611058
this.info.records++;
10621059

10631060
if (from === 1 || this.info.records >= from) {
1061+
// With columns, records are object
10641062
if (columns !== false) {
10651063
var obj = {}; // Transform record array to an object
10661064

@@ -1078,7 +1076,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
10781076
}
10791077
}
10801078

1081-
var objname = this.options.objname;
1079+
var objname = this.options.objname; // Without objname (default)
10821080

10831081
if (objname === undefined) {
10841082
if (raw === true || info === true) {
@@ -1087,7 +1085,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
10871085
}, raw === true ? {
10881086
raw: this.state.rawBuffer.toString(encoding)
10891087
} : {}, info === true ? {
1090-
info: this.state.info
1088+
info: _objectSpread({}, this.info)
10911089
} : {}));
10921090

10931091
if (_err5) {
@@ -1099,15 +1097,16 @@ var Parser = /*#__PURE__*/function (_Transform) {
10991097
if (_err6) {
11001098
return _err6;
11011099
}
1102-
}
1100+
} // With objname (default)
1101+
11031102
} else {
11041103
if (raw === true || info === true) {
11051104
var _err7 = this.__push(Object.assign({
11061105
record: [obj[objname], obj]
11071106
}, raw === true ? {
11081107
raw: this.state.rawBuffer.toString(encoding)
11091108
} : {}, info === true ? {
1110-
info: this.state.info
1109+
info: _objectSpread({}, this.info)
11111110
} : {}));
11121111

11131112
if (_err7) {
@@ -1120,15 +1119,16 @@ var Parser = /*#__PURE__*/function (_Transform) {
11201119
return _err8;
11211120
}
11221121
}
1123-
}
1122+
} // Without columns, records are array
1123+
11241124
} else {
11251125
if (raw === true || info === true) {
11261126
var _err9 = this.__push(Object.assign({
11271127
record: record
11281128
}, raw === true ? {
11291129
raw: this.state.rawBuffer.toString(encoding)
11301130
} : {}, info === true ? {
1131-
info: this.state.info
1131+
info: _objectSpread({}, this.info)
11321132
} : {}));
11331133

11341134
if (_err9) {

lib/browser/sync.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
624624
expectedRecordLength: Array.isArray(options.columns) ? options.columns.length : undefined,
625625
field: new ResizeableBuffer(20),
626626
firstLineToHeaders: fnFirstLineToHeaders,
627-
info: Object.assign({}, this.info),
627+
// info: Object.assign({}, this.info),
628628
needMoreDataSize: Math.max.apply(Math, [// Skip if the remaining buffer smaller than comment
629629
options.comment !== null ? options.comment.length : 0].concat(_toConsumableArray(options.delimiter.map(function (delimiter) {
630630
return delimiter.length;
@@ -682,7 +682,6 @@ var Parser = /*#__PURE__*/function (_Transform) {
682682
comment = _this$options.comment,
683683
escape = _this$options.escape,
684684
from_line = _this$options.from_line,
685-
info = _this$options.info,
686685
ltrim = _this$options.ltrim,
687686
max_record_size = _this$options.max_record_size,
688687
quote = _this$options.quote,
@@ -754,11 +753,9 @@ var Parser = /*#__PURE__*/function (_Transform) {
754753
}
755754

756755
if (this.state.wasRowDelimiter === true) {
757-
this.info.lines++;
758-
759-
if (info === true && this.state.record.length === 0 && this.state.field.length === 0 && this.state.wasQuoting === false) {
760-
this.state.info = Object.assign({}, this.info);
761-
}
756+
this.info.lines++; // if(info === true && this.state.record.length === 0 && this.state.field.length === 0 && this.state.wasQuoting === false){
757+
// this.state.info = Object.assign({}, this.info)
758+
// }
762759

763760
this.state.wasRowDelimiter = false;
764761
}
@@ -1061,6 +1058,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
10611058
this.info.records++;
10621059

10631060
if (from === 1 || this.info.records >= from) {
1061+
// With columns, records are object
10641062
if (columns !== false) {
10651063
var obj = {}; // Transform record array to an object
10661064

@@ -1078,7 +1076,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
10781076
}
10791077
}
10801078

1081-
var objname = this.options.objname;
1079+
var objname = this.options.objname; // Without objname (default)
10821080

10831081
if (objname === undefined) {
10841082
if (raw === true || info === true) {
@@ -1087,7 +1085,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
10871085
}, raw === true ? {
10881086
raw: this.state.rawBuffer.toString(encoding)
10891087
} : {}, info === true ? {
1090-
info: this.state.info
1088+
info: _objectSpread({}, this.info)
10911089
} : {}));
10921090

10931091
if (_err5) {
@@ -1099,15 +1097,16 @@ var Parser = /*#__PURE__*/function (_Transform) {
10991097
if (_err6) {
11001098
return _err6;
11011099
}
1102-
}
1100+
} // With objname (default)
1101+
11031102
} else {
11041103
if (raw === true || info === true) {
11051104
var _err7 = this.__push(Object.assign({
11061105
record: [obj[objname], obj]
11071106
}, raw === true ? {
11081107
raw: this.state.rawBuffer.toString(encoding)
11091108
} : {}, info === true ? {
1110-
info: this.state.info
1109+
info: _objectSpread({}, this.info)
11111110
} : {}));
11121111

11131112
if (_err7) {
@@ -1120,15 +1119,16 @@ var Parser = /*#__PURE__*/function (_Transform) {
11201119
return _err8;
11211120
}
11221121
}
1123-
}
1122+
} // Without columns, records are array
1123+
11241124
} else {
11251125
if (raw === true || info === true) {
11261126
var _err9 = this.__push(Object.assign({
11271127
record: record
11281128
}, raw === true ? {
11291129
raw: this.state.rawBuffer.toString(encoding)
11301130
} : {}, info === true ? {
1131-
info: this.state.info
1131+
info: _objectSpread({}, this.info)
11321132
} : {}));
11331133

11341134
if (_err9) {

lib/es5/index.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
516516
expectedRecordLength: Array.isArray(options.columns) ? options.columns.length : undefined,
517517
field: new ResizeableBuffer(20),
518518
firstLineToHeaders: fnFirstLineToHeaders,
519-
info: Object.assign({}, this.info),
519+
// info: Object.assign({}, this.info),
520520
needMoreDataSize: Math.max.apply(Math, [// Skip if the remaining buffer smaller than comment
521521
options.comment !== null ? options.comment.length : 0].concat(_toConsumableArray(options.delimiter.map(function (delimiter) {
522522
return delimiter.length;
@@ -574,7 +574,6 @@ var Parser = /*#__PURE__*/function (_Transform) {
574574
comment = _this$options.comment,
575575
escape = _this$options.escape,
576576
from_line = _this$options.from_line,
577-
info = _this$options.info,
578577
ltrim = _this$options.ltrim,
579578
max_record_size = _this$options.max_record_size,
580579
quote = _this$options.quote,
@@ -646,11 +645,9 @@ var Parser = /*#__PURE__*/function (_Transform) {
646645
}
647646

648647
if (this.state.wasRowDelimiter === true) {
649-
this.info.lines++;
650-
651-
if (info === true && this.state.record.length === 0 && this.state.field.length === 0 && this.state.wasQuoting === false) {
652-
this.state.info = Object.assign({}, this.info);
653-
}
648+
this.info.lines++; // if(info === true && this.state.record.length === 0 && this.state.field.length === 0 && this.state.wasQuoting === false){
649+
// this.state.info = Object.assign({}, this.info)
650+
// }
654651

655652
this.state.wasRowDelimiter = false;
656653
}
@@ -953,6 +950,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
953950
this.info.records++;
954951

955952
if (from === 1 || this.info.records >= from) {
953+
// With columns, records are object
956954
if (columns !== false) {
957955
var obj = {}; // Transform record array to an object
958956

@@ -970,7 +968,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
970968
}
971969
}
972970

973-
var objname = this.options.objname;
971+
var objname = this.options.objname; // Without objname (default)
974972

975973
if (objname === undefined) {
976974
if (raw === true || info === true) {
@@ -979,7 +977,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
979977
}, raw === true ? {
980978
raw: this.state.rawBuffer.toString(encoding)
981979
} : {}, info === true ? {
982-
info: this.state.info
980+
info: _objectSpread({}, this.info)
983981
} : {}));
984982

985983
if (_err5) {
@@ -991,15 +989,16 @@ var Parser = /*#__PURE__*/function (_Transform) {
991989
if (_err6) {
992990
return _err6;
993991
}
994-
}
992+
} // With objname (default)
993+
995994
} else {
996995
if (raw === true || info === true) {
997996
var _err7 = this.__push(Object.assign({
998997
record: [obj[objname], obj]
999998
}, raw === true ? {
1000999
raw: this.state.rawBuffer.toString(encoding)
10011000
} : {}, info === true ? {
1002-
info: this.state.info
1001+
info: _objectSpread({}, this.info)
10031002
} : {}));
10041003

10051004
if (_err7) {
@@ -1012,15 +1011,16 @@ var Parser = /*#__PURE__*/function (_Transform) {
10121011
return _err8;
10131012
}
10141013
}
1015-
}
1014+
} // Without columns, records are array
1015+
10161016
} else {
10171017
if (raw === true || info === true) {
10181018
var _err9 = this.__push(Object.assign({
10191019
record: record
10201020
}, raw === true ? {
10211021
raw: this.state.rawBuffer.toString(encoding)
10221022
} : {}, info === true ? {
1023-
info: this.state.info
1023+
info: _objectSpread({}, this.info)
10241024
} : {}));
10251025

10261026
if (_err9) {

lib/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,6 @@ class Parser extends Transform {
443443
expectedRecordLength: Array.isArray(options.columns) ? options.columns.length : undefined,
444444
field: new ResizeableBuffer(20),
445445
firstLineToHeaders: fnFirstLineToHeaders,
446-
info: Object.assign({}, this.info),
447446
needMoreDataSize: Math.max(
448447
// Skip if the remaining buffer smaller than comment
449448
options.comment !== null ? options.comment.length : 0,
@@ -486,7 +485,7 @@ class Parser extends Transform {
486485
}
487486
// Central parser implementation
488487
__parse(nextBuf, end){
489-
const {bom, comment, escape, from_line, info, ltrim, max_record_size, quote, raw, relax, rtrim, skip_empty_lines, to, to_line} = this.options
488+
const {bom, comment, escape, from_line, ltrim, max_record_size, quote, raw, relax, rtrim, skip_empty_lines, to, to_line} = this.options
490489
let {record_delimiter} = this.options
491490
const {bomSkipped, previousBuf, rawBuffer, escapeIsQuote} = this.state
492491
let buf
@@ -537,9 +536,6 @@ class Parser extends Transform {
537536
}
538537
if(this.state.wasRowDelimiter === true){
539538
this.info.lines++
540-
if(info === true && this.state.record.length === 0 && this.state.field.length === 0 && this.state.wasQuoting === false){
541-
this.state.info = Object.assign({}, this.info)
542-
}
543539
this.state.wasRowDelimiter = false
544540
}
545541
if(to_line !== -1 && this.info.lines > to_line){
@@ -813,6 +809,7 @@ class Parser extends Transform {
813809
}
814810
this.info.records++
815811
if(from === 1 || this.info.records >= from){
812+
// With columns, records are object
816813
if(columns !== false){
817814
const obj = {}
818815
// Transform record array to an object
@@ -830,12 +827,13 @@ class Parser extends Transform {
830827
}
831828
}
832829
const {objname} = this.options
830+
// Without objname (default)
833831
if(objname === undefined){
834832
if(raw === true || info === true){
835833
const err = this.__push(Object.assign(
836834
{record: obj},
837835
(raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {}),
838-
(info === true ? {info: this.state.info}: {})
836+
(info === true ? {info: {...this.info}}: {})
839837
))
840838
if(err){
841839
return err
@@ -846,12 +844,13 @@ class Parser extends Transform {
846844
return err
847845
}
848846
}
847+
// With objname (default)
849848
}else{
850849
if(raw === true || info === true){
851850
const err = this.__push(Object.assign(
852851
{record: [obj[objname], obj]},
853852
raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {},
854-
info === true ? {info: this.state.info}: {}
853+
info === true ? {info: {...this.info}}: {}
855854
))
856855
if(err){
857856
return err
@@ -863,12 +862,13 @@ class Parser extends Transform {
863862
}
864863
}
865864
}
865+
// Without columns, records are array
866866
}else{
867867
if(raw === true || info === true){
868868
const err = this.__push(Object.assign(
869869
{record: record},
870870
raw === true ? {raw: this.state.rawBuffer.toString(encoding)}: {},
871-
info === true ? {info: this.state.info}: {}
871+
info === true ? {info: {...this.info}}: {}
872872
))
873873
if(err){
874874
return err

test/api.info.coffee

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,18 @@ describe 'API info', ->
2828
lines: 2
2929
records: 2
3030
next err
31+
32+
it 'with multiline records', (next) ->
33+
parse '''
34+
a,b,c
35+
d,"e
36+
",f
37+
g,h,i
38+
''', (err, data, info) ->
39+
info.should.eql
40+
comment_lines: 0
41+
empty_lines: 0
42+
invalid_field_length: 0
43+
lines: 4
44+
records: 3
45+
next err

test/option.info.coffee

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,6 @@ describe 'Option `info`', ->
6060
''', info: true, (err, records) ->
6161
records.map(
6262
({info}) -> info.lines
63-
).should.eql [1, 2, 4] unless err
63+
).should.eql [1, 3, 4] unless err
6464
next err
6565

0 commit comments

Comments
 (0)