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

Commit

Permalink
refactor: context and info reorganization
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed May 20, 2021
1 parent 7d84199 commit dbce1d1
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 127 deletions.
71 changes: 40 additions & 31 deletions lib/browser/index.js
Expand Up @@ -624,7 +624,6 @@ 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),
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 @@ -753,10 +752,7 @@ 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++;
this.state.wasRowDelimiter = false;
}

Expand Down Expand Up @@ -830,7 +826,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
pos += quote.length - 1;
continue;
} else if (relax === false) {
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()));
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()));

if (err !== undefined) return err;
} else {
Expand All @@ -843,7 +839,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
if (this.state.field.length !== 0) {
// In relax mode, treat opening quote preceded by chrs as regular
if (relax === false) {
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(), {
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(), {
field: this.state.field
}));

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

if (this.state.commenting === false) {
if (max_record_size !== 0 && this.state.record_length + this.state.field.length > max_record_size) {
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()));
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()));

if (_err2 !== undefined) return _err2;
}
Expand All @@ -944,7 +940,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
if (lappend === true && rappend === true) {
this.state.field.append(chr);
} else if (rtrim === true && !this.__isCharTrimable(chr)) {
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()));
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()));

if (_err3 !== undefined) return _err3;
}
Expand All @@ -953,7 +949,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
if (end === true) {
// Ensure we are not ending in a quoting state
if (this.state.quoting === true) {
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()));
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()));

if (_err4 !== undefined) return _err4;
} else {
Expand Down Expand Up @@ -1023,12 +1019,12 @@ var Parser = /*#__PURE__*/function (_Transform) {
if (recordLength !== this.state.expectedRecordLength) {
var err = columns === false ? // Todo: rename CSV_INCONSISTENT_RECORD_LENGTH to
// CSV_RECORD_INCONSISTENT_FIELDS_LENGTH
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(), {
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(), {
record: record
}) : // Todo: rename CSV_RECORD_DONT_MATCH_COLUMNS_LENGTH to
// CSV_RECORD_INCONSISTENT_COLUMNS
new CsvError('CSV_RECORD_DONT_MATCH_COLUMNS_LENGTH', ['Invalid Record Length:', "columns length is ".concat(columns.length, ","), // rename columns
"got ".concat(recordLength, " on line ").concat(this.info.lines)], this.options, this.__context(), {
"got ".concat(recordLength, " on line ").concat(this.info.lines)], this.options, this.__infoField(), {
record: record
});

Expand Down Expand Up @@ -1085,7 +1081,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
}, raw === true ? {
raw: this.state.rawBuffer.toString(encoding)
} : {}, info === true ? {
info: _objectSpread({}, this.info)
info: this.__infoRecord()
} : {}));

if (_err5) {
Expand All @@ -1106,7 +1102,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
}, raw === true ? {
raw: this.state.rawBuffer.toString(encoding)
} : {}, info === true ? {
info: _objectSpread({}, this.info)
info: this.__infoRecord()
} : {}));

if (_err7) {
Expand All @@ -1128,7 +1124,7 @@ var Parser = /*#__PURE__*/function (_Transform) {
}, raw === true ? {
raw: this.state.rawBuffer.toString(encoding)
} : {}, info === true ? {
info: _objectSpread({}, this.info)
info: this.__infoRecord()
} : {}));

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

if (!Array.isArray(headers)) {
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(), {
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(), {
headers: headers
}));
}
Expand Down Expand Up @@ -1195,7 +1191,6 @@ var Parser = /*#__PURE__*/function (_Transform) {
wasQuoting = _this$state3.wasQuoting; // Short circuit for the from_line options

if (enabled === false) {
/* this.options.columns !== true && */
return this.__resetField();
}

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

if (on_record !== undefined) {
var context = this.__context();
var info = this.__infoRecord();

try {
record = on_record.call(null, record, context);
record = on_record.call(null, record, info);
} catch (err) {
return err;
}
Expand All @@ -1265,11 +1260,11 @@ var Parser = /*#__PURE__*/function (_Transform) {
return [undefined, undefined];
}

var context = this.__context();

if (this.state.castField !== null) {
try {
return [undefined, this.state.castField.call(null, field, context)];
var info = this.__infoField();

return [undefined, this.state.castField.call(null, field, info)];
} catch (err) {
return [err];
}
Expand All @@ -1278,7 +1273,9 @@ var Parser = /*#__PURE__*/function (_Transform) {
if (this.__isFloat(field)) {
return [undefined, parseFloat(field)];
} else if (this.options.cast_date !== false) {
return [undefined, this.options.cast_date.call(null, field, context)];
var _info = this.__infoField();

return [undefined, this.options.cast_date.call(null, field, _info)];
}

return [undefined, field];
Expand Down Expand Up @@ -1453,18 +1450,30 @@ var Parser = /*#__PURE__*/function (_Transform) {
}
}
}, {
key: "__context",
value: function __context() {
key: "__infoDataSet",
value: function __infoDataSet() {
return _objectSpread({}, this.info);
}
}, {
key: "__infoRecord",
value: function __infoRecord() {
var columns = this.options.columns;
var isColumns = Array.isArray(columns);
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,
return _objectSpread(_objectSpread({}, this.__infoDataSet()), {}, {
error: this.state.error,
header: columns === true,
quoting: this.state.wasQuoting,
index: this.state.record.length
});
}
}, {
key: "__infoField",
value: function __infoField() {
var columns = this.options.columns;
var isColumns = Array.isArray(columns);
return _objectSpread(_objectSpread({}, this.__infoRecord()), {}, {
column: isColumns === true ? columns.length > this.state.record.length ? columns[this.state.record.length].name : null : this.state.record.length,
quoting: this.state.wasQuoting
});
}
}]);

return Parser;
Expand Down Expand Up @@ -1505,10 +1514,10 @@ var parse = function parse() {
}
});
parser.on('error', function (err) {
callback(err, undefined, parser.info);
callback(err, undefined, parser.__infoDataSet());
});
parser.on('end', function () {
callback(undefined, records, parser.info);
callback(undefined, records, parser.__infoDataSet());
});
}

Expand Down

0 comments on commit dbce1d1

Please sign in to comment.