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

Commit dbce1d1

Browse files
committed
refactor: context and info reorganization
1 parent 7d84199 commit dbce1d1

File tree

6 files changed

+202
-127
lines changed

6 files changed

+202
-127
lines changed

lib/browser/index.js

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,6 @@ 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),
628627
needMoreDataSize: Math.max.apply(Math, [// Skip if the remaining buffer smaller than comment
629628
options.comment !== null ? options.comment.length : 0].concat(_toConsumableArray(options.delimiter.map(function (delimiter) {
630629
return delimiter.length;
@@ -753,10 +752,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
753752
}
754753

755754
if (this.state.wasRowDelimiter === true) {
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-
// }
759-
755+
this.info.lines++;
760756
this.state.wasRowDelimiter = false;
761757
}
762758

@@ -830,7 +826,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
830826
pos += quote.length - 1;
831827
continue;
832828
} else if (relax === false) {
833-
var err = this.__error(new CsvError('CSV_INVALID_CLOSING_QUOTE', ['Invalid Closing Quote:', "got \"".concat(String.fromCharCode(nextChr), "\""), "at line ".concat(this.info.lines), 'instead of delimiter, record delimiter, trimable character', '(if activated) or comment'], this.options, this.__context()));
829+
var err = this.__error(new CsvError('CSV_INVALID_CLOSING_QUOTE', ['Invalid Closing Quote:', "got \"".concat(String.fromCharCode(nextChr), "\""), "at line ".concat(this.info.lines), 'instead of delimiter, record delimiter, trimable character', '(if activated) or comment'], this.options, this.__infoField()));
834830

835831
if (err !== undefined) return err;
836832
} else {
@@ -843,7 +839,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
843839
if (this.state.field.length !== 0) {
844840
// In relax mode, treat opening quote preceded by chrs as regular
845841
if (relax === false) {
846-
var _err = this.__error(new CsvError('INVALID_OPENING_QUOTE', ['Invalid Opening Quote:', "a quote is found inside a field at line ".concat(this.info.lines)], this.options, this.__context(), {
842+
var _err = this.__error(new CsvError('INVALID_OPENING_QUOTE', ['Invalid Opening Quote:', "a quote is found inside a field at line ".concat(this.info.lines)], this.options, this.__infoField(), {
847843
field: this.state.field
848844
}));
849845

@@ -931,7 +927,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
931927

932928
if (this.state.commenting === false) {
933929
if (max_record_size !== 0 && this.state.record_length + this.state.field.length > max_record_size) {
934-
var _err2 = this.__error(new CsvError('CSV_MAX_RECORD_SIZE', ['Max Record Size:', 'record exceed the maximum number of tolerated bytes', "of ".concat(max_record_size), "at line ".concat(this.info.lines)], this.options, this.__context()));
930+
var _err2 = this.__error(new CsvError('CSV_MAX_RECORD_SIZE', ['Max Record Size:', 'record exceed the maximum number of tolerated bytes', "of ".concat(max_record_size), "at line ".concat(this.info.lines)], this.options, this.__infoField()));
935931

936932
if (_err2 !== undefined) return _err2;
937933
}
@@ -944,7 +940,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
944940
if (lappend === true && rappend === true) {
945941
this.state.field.append(chr);
946942
} else if (rtrim === true && !this.__isCharTrimable(chr)) {
947-
var _err3 = this.__error(new CsvError('CSV_NON_TRIMABLE_CHAR_AFTER_CLOSING_QUOTE', ['Invalid Closing Quote:', 'found non trimable byte after quote', "at line ".concat(this.info.lines)], this.options, this.__context()));
943+
var _err3 = this.__error(new CsvError('CSV_NON_TRIMABLE_CHAR_AFTER_CLOSING_QUOTE', ['Invalid Closing Quote:', 'found non trimable byte after quote', "at line ".concat(this.info.lines)], this.options, this.__infoField()));
948944

949945
if (_err3 !== undefined) return _err3;
950946
}
@@ -953,7 +949,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
953949
if (end === true) {
954950
// Ensure we are not ending in a quoting state
955951
if (this.state.quoting === true) {
956-
var _err4 = this.__error(new CsvError('CSV_QUOTE_NOT_CLOSED', ['Quote Not Closed:', "the parsing is finished with an opening quote at line ".concat(this.info.lines)], this.options, this.__context()));
952+
var _err4 = this.__error(new CsvError('CSV_QUOTE_NOT_CLOSED', ['Quote Not Closed:', "the parsing is finished with an opening quote at line ".concat(this.info.lines)], this.options, this.__infoField()));
957953

958954
if (_err4 !== undefined) return _err4;
959955
} else {
@@ -1023,12 +1019,12 @@ var Parser = /*#__PURE__*/function (_Transform) {
10231019
if (recordLength !== this.state.expectedRecordLength) {
10241020
var err = columns === false ? // Todo: rename CSV_INCONSISTENT_RECORD_LENGTH to
10251021
// CSV_RECORD_INCONSISTENT_FIELDS_LENGTH
1026-
new CsvError('CSV_INCONSISTENT_RECORD_LENGTH', ['Invalid Record Length:', "expect ".concat(this.state.expectedRecordLength, ","), "got ".concat(recordLength, " on line ").concat(this.info.lines)], this.options, this.__context(), {
1022+
new CsvError('CSV_INCONSISTENT_RECORD_LENGTH', ['Invalid Record Length:', "expect ".concat(this.state.expectedRecordLength, ","), "got ".concat(recordLength, " on line ").concat(this.info.lines)], this.options, this.__infoField(), {
10271023
record: record
10281024
}) : // Todo: rename CSV_RECORD_DONT_MATCH_COLUMNS_LENGTH to
10291025
// CSV_RECORD_INCONSISTENT_COLUMNS
10301026
new CsvError('CSV_RECORD_DONT_MATCH_COLUMNS_LENGTH', ['Invalid Record Length:', "columns length is ".concat(columns.length, ","), // rename columns
1031-
"got ".concat(recordLength, " on line ").concat(this.info.lines)], this.options, this.__context(), {
1027+
"got ".concat(recordLength, " on line ").concat(this.info.lines)], this.options, this.__infoField(), {
10321028
record: record
10331029
});
10341030

@@ -1085,7 +1081,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
10851081
}, raw === true ? {
10861082
raw: this.state.rawBuffer.toString(encoding)
10871083
} : {}, info === true ? {
1088-
info: _objectSpread({}, this.info)
1084+
info: this.__infoRecord()
10891085
} : {}));
10901086

10911087
if (_err5) {
@@ -1106,7 +1102,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
11061102
}, raw === true ? {
11071103
raw: this.state.rawBuffer.toString(encoding)
11081104
} : {}, info === true ? {
1109-
info: _objectSpread({}, this.info)
1105+
info: this.__infoRecord()
11101106
} : {}));
11111107

11121108
if (_err7) {
@@ -1128,7 +1124,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
11281124
}, raw === true ? {
11291125
raw: this.state.rawBuffer.toString(encoding)
11301126
} : {}, info === true ? {
1131-
info: _objectSpread({}, this.info)
1127+
info: this.__infoRecord()
11321128
} : {}));
11331129

11341130
if (_err9) {
@@ -1155,7 +1151,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
11551151
var headers = firstLineToHeaders === undefined ? record : firstLineToHeaders.call(null, record);
11561152

11571153
if (!Array.isArray(headers)) {
1158-
return this.__error(new CsvError('CSV_INVALID_COLUMN_MAPPING', ['Invalid Column Mapping:', 'expect an array from column function,', "got ".concat(JSON.stringify(headers))], this.options, this.__context(), {
1154+
return this.__error(new CsvError('CSV_INVALID_COLUMN_MAPPING', ['Invalid Column Mapping:', 'expect an array from column function,', "got ".concat(JSON.stringify(headers))], this.options, this.__infoField(), {
11591155
headers: headers
11601156
}));
11611157
}
@@ -1195,7 +1191,6 @@ var Parser = /*#__PURE__*/function (_Transform) {
11951191
wasQuoting = _this$state3.wasQuoting; // Short circuit for the from_line options
11961192

11971193
if (enabled === false) {
1198-
/* this.options.columns !== true && */
11991194
return this.__resetField();
12001195
}
12011196

@@ -1235,10 +1230,10 @@ var Parser = /*#__PURE__*/function (_Transform) {
12351230
var on_record = this.options.on_record;
12361231

12371232
if (on_record !== undefined) {
1238-
var context = this.__context();
1233+
var info = this.__infoRecord();
12391234

12401235
try {
1241-
record = on_record.call(null, record, context);
1236+
record = on_record.call(null, record, info);
12421237
} catch (err) {
12431238
return err;
12441239
}
@@ -1265,11 +1260,11 @@ var Parser = /*#__PURE__*/function (_Transform) {
12651260
return [undefined, undefined];
12661261
}
12671262

1268-
var context = this.__context();
1269-
12701263
if (this.state.castField !== null) {
12711264
try {
1272-
return [undefined, this.state.castField.call(null, field, context)];
1265+
var info = this.__infoField();
1266+
1267+
return [undefined, this.state.castField.call(null, field, info)];
12731268
} catch (err) {
12741269
return [err];
12751270
}
@@ -1278,7 +1273,9 @@ var Parser = /*#__PURE__*/function (_Transform) {
12781273
if (this.__isFloat(field)) {
12791274
return [undefined, parseFloat(field)];
12801275
} else if (this.options.cast_date !== false) {
1281-
return [undefined, this.options.cast_date.call(null, field, context)];
1276+
var _info = this.__infoField();
1277+
1278+
return [undefined, this.options.cast_date.call(null, field, _info)];
12821279
}
12831280

12841281
return [undefined, field];
@@ -1453,18 +1450,30 @@ var Parser = /*#__PURE__*/function (_Transform) {
14531450
}
14541451
}
14551452
}, {
1456-
key: "__context",
1457-
value: function __context() {
1453+
key: "__infoDataSet",
1454+
value: function __infoDataSet() {
1455+
return _objectSpread({}, this.info);
1456+
}
1457+
}, {
1458+
key: "__infoRecord",
1459+
value: function __infoRecord() {
14581460
var columns = this.options.columns;
1459-
var isColumns = Array.isArray(columns);
1460-
return _objectSpread(_objectSpread({}, this.info), {
1461-
column: isColumns === true ? columns.length > this.state.record.length ? columns[this.state.record.length].name : null : this.state.record.length,
1461+
return _objectSpread(_objectSpread({}, this.__infoDataSet()), {}, {
14621462
error: this.state.error,
14631463
header: columns === true,
1464-
quoting: this.state.wasQuoting,
14651464
index: this.state.record.length
14661465
});
14671466
}
1467+
}, {
1468+
key: "__infoField",
1469+
value: function __infoField() {
1470+
var columns = this.options.columns;
1471+
var isColumns = Array.isArray(columns);
1472+
return _objectSpread(_objectSpread({}, this.__infoRecord()), {}, {
1473+
column: isColumns === true ? columns.length > this.state.record.length ? columns[this.state.record.length].name : null : this.state.record.length,
1474+
quoting: this.state.wasQuoting
1475+
});
1476+
}
14681477
}]);
14691478

14701479
return Parser;
@@ -1505,10 +1514,10 @@ var parse = function parse() {
15051514
}
15061515
});
15071516
parser.on('error', function (err) {
1508-
callback(err, undefined, parser.info);
1517+
callback(err, undefined, parser.__infoDataSet());
15091518
});
15101519
parser.on('end', function () {
1511-
callback(undefined, records, parser.info);
1520+
callback(undefined, records, parser.__infoDataSet());
15121521
});
15131522
}
15141523

0 commit comments

Comments
 (0)