csv-parse
Advanced tools
Comparing version 4.5.0 to 4.6.0
@@ -10,2 +10,10 @@ | ||
## Version 4.6.0 | ||
* skip_lines_with_empty_values: handle non string value | ||
* errors: add context information | ||
* tests: new error assertion framework | ||
* buffer: serialize to json as string | ||
* errors: expose INVALID_OPENING_QUOTE | ||
## Version 4.5.0 | ||
@@ -12,0 +20,0 @@ |
@@ -617,3 +617,3 @@ "use strict"; | ||
} 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, row delimiter, trimable character', '(if activated) or comment'], {})); | ||
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, row delimiter, trimable character', '(if activated) or comment'], this.__context())); | ||
@@ -631,3 +631,5 @@ if (err !== undefined) return err; | ||
if (relax === false) { | ||
var _err = this.__error("Invalid opening quote at line ".concat(this.info.lines)); | ||
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.__context(), { | ||
field: this.state.field | ||
})); | ||
@@ -738,3 +740,3 @@ if (_err !== undefined) return _err; | ||
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)])); | ||
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.__context())); | ||
@@ -801,3 +803,3 @@ if (_err4 !== undefined) return _err4; | ||
if (columns === false) { | ||
var err = this.__error(new CsvError('CSV_INVALID_RECORD_LENGTH_DONT_PREVIOUS_RECORDS', ['Invalid Record Length:', "expect ".concat(this.state.expectedRecordLength, ","), "got ".concat(recordLength, " on line ").concat(this.info.lines)], { | ||
var err = this.__error(new CsvError('CSV_INVALID_RECORD_LENGTH_DONT_PREVIOUS_RECORDS', ['Invalid Record Length:', "expect ".concat(this.state.expectedRecordLength, ","), "got ".concat(recordLength, " on line ").concat(this.info.lines)], this.__context(), { | ||
record: record | ||
@@ -808,3 +810,3 @@ })); | ||
} else { | ||
var _err5 = this.__error(new CsvError('CSV_INVALID_RECORD_LENGTH_DONT_MATCH_COLUMNS', ['Invalid Record Length:', "header length is ".concat(columns.length, ","), "got ".concat(recordLength, " on line ").concat(this.info.lines)], { | ||
var _err5 = this.__error(new CsvError('CSV_INVALID_RECORD_LENGTH_DONT_MATCH_COLUMNS', ['Invalid Record Length:', "header length is ".concat(columns.length, ","), "got ".concat(recordLength, " on line ").concat(this.info.lines)], this.__context(), { | ||
record: record | ||
@@ -825,3 +827,3 @@ })); | ||
if (record.every(function (field) { | ||
return field.trim() === ''; | ||
return field == null || field.toString && field.toString().trim() === ''; | ||
})) { | ||
@@ -905,3 +907,3 @@ this.__resetRow(); | ||
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))], { | ||
return this.__error(new CsvError('CSV_INVALID_COLUMN_MAPPING', ['Invalid Column Mapping:', 'expect an array from column function,', "got ".concat(JSON.stringify(headers))], this.__context(), { | ||
headers: headers | ||
@@ -994,12 +996,3 @@ })); | ||
var context = { | ||
column: isColumns === true ? columns[this.state.record.length].name : this.state.record.length, | ||
empty_lines: this.info.empty_lines, | ||
header: columns === true, | ||
index: this.state.record.length, | ||
invalid_field_length: this.info.invalid_field_length, | ||
quoting: this.state.wasQuoting, | ||
lines: this.info.lines, | ||
records: this.info.records | ||
}; | ||
var context = this.__context(); | ||
@@ -1145,2 +1138,18 @@ if (this.state.castField !== null) { | ||
} | ||
}, { | ||
key: "__context", | ||
value: function __context() { | ||
var columns = this.options.columns; | ||
var isColumns = Array.isArray(columns); | ||
return { | ||
column: isColumns === true ? columns[this.state.record.length].name : this.state.record.length, | ||
empty_lines: this.info.empty_lines, | ||
header: columns === true, | ||
index: this.state.record.length, | ||
invalid_field_length: this.info.invalid_field_length, | ||
quoting: this.state.wasQuoting, | ||
lines: this.info.lines, | ||
records: this.info.records | ||
}; | ||
} | ||
}]); | ||
@@ -1206,3 +1215,3 @@ | ||
function CsvError(code, message, context) { | ||
function CsvError(code, message) { | ||
var _this2; | ||
@@ -1217,6 +1226,15 @@ | ||
for (var key in context) { | ||
_this2[key] = context[key]; | ||
for (var _len = arguments.length, contexts = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) { | ||
contexts[_key - 2] = arguments[_key]; | ||
} | ||
for (var _i2 = 0, _contexts = contexts; _i2 < _contexts.length; _i2++) { | ||
context = _contexts[_i2]; | ||
for (var key in context) { | ||
var value = Buffer.isBuffer(context[key]) ? context[key].toString() : context[key]; | ||
_this2[key] = JSON.parse(JSON.stringify(value)); | ||
} | ||
} | ||
return _this2; | ||
@@ -1243,3 +1261,2 @@ } | ||
var normalizeColumnsArray = function normalizeColumnsArray(columns) { | ||
// console.log('columns', columns) | ||
var normalizedColumns = []; | ||
@@ -1267,6 +1284,5 @@ | ||
} | ||
} // console.log(normalizedColumns) | ||
} | ||
return normalizedColumns; | ||
}; |
@@ -66,2 +66,7 @@ "use strict"; | ||
}, { | ||
key: "toJSON", | ||
value: function toJSON() { | ||
return this.toString(); | ||
} | ||
}, { | ||
key: "reset", | ||
@@ -68,0 +73,0 @@ value: function reset() { |
@@ -473,3 +473,3 @@ | ||
'(if activated) or comment', | ||
], {}) | ||
], this.__context()) | ||
) | ||
@@ -487,3 +487,10 @@ if(err !== undefined) return err | ||
if( relax === false ){ | ||
const err = this.__error(`Invalid opening quote at line ${this.info.lines}`) | ||
const err = this.__error( | ||
new CsvError('INVALID_OPENING_QUOTE', [ | ||
'Invalid Opening Quote:', | ||
`a quote is found inside a field at line ${this.info.lines}`, | ||
], this.__context(), { | ||
field: this.state.field, | ||
}) | ||
) | ||
if(err !== undefined) return err | ||
@@ -575,3 +582,3 @@ } | ||
`the parsing is finished with an opening quote at line ${this.info.lines}`, | ||
]) | ||
], this.__context()) | ||
) | ||
@@ -625,3 +632,3 @@ if(err !== undefined) return err | ||
`got ${recordLength} on line ${this.info.lines}`, | ||
], { | ||
], this.__context(), { | ||
record: record, | ||
@@ -637,3 +644,3 @@ }) | ||
`got ${recordLength} on line ${this.info.lines}`, | ||
], { | ||
], this.__context(), { | ||
record: record, | ||
@@ -651,3 +658,3 @@ }) | ||
if(skip_lines_with_empty_values === true){ | ||
if(record.every( (field) => field.trim() === '' )){ | ||
if(record.every( (field) => field == null || field.toString && field.toString().trim() === '' )){ | ||
this.__resetRow() | ||
@@ -717,3 +724,3 @@ return | ||
`got ${JSON.stringify(headers)}` | ||
], { | ||
], this.__context(), { | ||
headers: headers, | ||
@@ -777,14 +784,3 @@ }) | ||
} | ||
const context = { | ||
column: isColumns === true ? | ||
columns[this.state.record.length].name : | ||
this.state.record.length, | ||
empty_lines: this.info.empty_lines, | ||
header: columns === true, | ||
index: this.state.record.length, | ||
invalid_field_length: this.info.invalid_field_length, | ||
quoting: this.state.wasQuoting, | ||
lines: this.info.lines, | ||
records: this.info.records | ||
} | ||
const context = this.__context() | ||
if(this.state.castField !== null){ | ||
@@ -901,2 +897,18 @@ try{ | ||
} | ||
__context(){ | ||
const {columns} = this.options | ||
const isColumns = Array.isArray(columns) | ||
return { | ||
column: isColumns === true ? | ||
columns[this.state.record.length].name : | ||
this.state.record.length, | ||
empty_lines: this.info.empty_lines, | ||
header: columns === true, | ||
index: this.state.record.length, | ||
invalid_field_length: this.info.invalid_field_length, | ||
quoting: this.state.wasQuoting, | ||
lines: this.info.lines, | ||
records: this.info.records | ||
} | ||
} | ||
} | ||
@@ -947,3 +959,3 @@ | ||
class CsvError extends Error { | ||
constructor(code, message, context) { | ||
constructor(code, message, ...contexts) { | ||
if(Array.isArray(message)) message = message.join(' ') | ||
@@ -953,4 +965,7 @@ super([message]) | ||
this.code = code | ||
for(let key in context){ | ||
this[key] = context[key] | ||
for(context of contexts){ | ||
for(let key in context){ | ||
const value = Buffer.isBuffer(context[key]) ? context[key].toString() : context[key] | ||
this[key] = JSON.parse(JSON.stringify(value)) | ||
} | ||
} | ||
@@ -977,3 +992,2 @@ } | ||
const normalizeColumnsArray = function(columns){ | ||
// console.log('columns', columns) | ||
const normalizedColumns = []; | ||
@@ -996,4 +1010,3 @@ | ||
} | ||
// console.log(normalizedColumns) | ||
return normalizedColumns; | ||
} |
@@ -38,2 +38,5 @@ | ||
} | ||
toJSON(){ | ||
return this.toString() | ||
} | ||
reset(){ | ||
@@ -40,0 +43,0 @@ this.length = 0 |
{ | ||
"version": "4.5.0", | ||
"version": "4.6.0", | ||
"name": "csv-parse", | ||
@@ -4,0 +4,0 @@ "description": "CSV parsing implementing the Node.js `stream.Transform` API", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
121590
2529