csv-parse
Advanced tools
Comparing version 2.0.4 to 2.1.0
# Changelog | ||
## Version 2.1.0 | ||
* skip_lines_with_error: DRYed implementation | ||
* skip_lines_with_error: Go process the next line on error | ||
* events: register and write not blocking | ||
* test: prefix names by group membership | ||
* events: emit record | ||
* raw: test to ensure it preserve columns | ||
* package: latest dependencies (28 march 2018) | ||
* raw: ensure tests call and success | ||
* package: ignore npm and yarn lock files | ||
* sync: handle errors on last line | ||
## Version 2.0.4 | ||
@@ -5,0 +18,0 @@ |
@@ -9,3 +9,3 @@ 'use strict'; | ||
// Generated by CoffeeScript 2.0.3 | ||
// Generated by CoffeeScript 2.2.3 | ||
// # CSV Parser | ||
@@ -126,3 +126,3 @@ | ||
var base, base1, base10, base11, base12, base13, base14, base15, base16, base2, base3, base4, base5, base6, base7, base8, base9, k, v; | ||
var base, base1, base10, base11, base12, base13, base14, base15, base16, base17, base2, base3, base4, base5, base6, base7, base8, base9, k, v; | ||
// @options = options | ||
@@ -203,2 +203,5 @@ this.options = {}; | ||
} | ||
if ((base17 = this.options).skip_lines_with_error == null) { | ||
base17.skip_lines_with_error = false; | ||
} | ||
// Counters | ||
@@ -231,3 +234,4 @@ // lines = count + skipped_line_count + empty_line_count | ||
return v.length; | ||
}))) : void 0 | ||
}))) : void 0, | ||
lineHasError: false | ||
}; | ||
@@ -262,38 +266,42 @@ return this; | ||
Parser.prototype._transform = function (chunk, encoding, callback) { | ||
var err; | ||
if (chunk instanceof Buffer) { | ||
chunk = this._.decoder.write(chunk); | ||
} | ||
err = this.__write(chunk, false); | ||
if (err) { | ||
return this.emit('error', err); | ||
} | ||
return callback(); | ||
var _this = this; | ||
return setImmediate(function () { | ||
var err; | ||
if (chunk instanceof Buffer) { | ||
chunk = _this._.decoder.write(chunk); | ||
} | ||
err = _this.__write(chunk, false); | ||
if (err) { | ||
return _this.emit('error', err); | ||
} | ||
return callback(); | ||
}); | ||
}; | ||
Parser.prototype._flush = function (callback) { | ||
return callback(this.__flush()); | ||
}; | ||
Parser.prototype.__flush = function () { | ||
var err; | ||
err = this.__write(this._.decoder.end(), true); | ||
if (err) { | ||
return this.emit('error', err); | ||
return err; | ||
} | ||
if (this._.quoting) { | ||
this.emit('error', new Error('Quoted field not terminated at line ' + (this.lines + 1))); | ||
return; | ||
err = this.error('Quoted field not terminated at line ' + (this.lines + 1)); | ||
return err; | ||
} | ||
if (this._.line.length > 0) { | ||
err = this.__push(this._.line); | ||
if (err) { | ||
return callback(err); | ||
} | ||
return this.__push(this._.line); | ||
} | ||
return callback(); | ||
}; | ||
Parser.prototype.__push = function (line) { | ||
var call_column_udf, columns, err, field, i, j, len, lineAsColumns, row; | ||
var call_column_udf, columns, err, field, i, j, len, lineAsColumns, record; | ||
if (this.options.skip_lines_with_empty_values && line.join('').trim() === '') { | ||
return; | ||
} | ||
row = null; | ||
record = null; | ||
if (this.options.columns === true) { | ||
@@ -339,5 +347,8 @@ this.options.columns = line; | ||
} else if (this.options.columns != null) { | ||
return Error('Number of columns on line ' + this.lines + ' does not match header'); | ||
// Suggest: Inconsistent header and column numbers: header is 1 and number of columns is 1 on line 1 | ||
err = this.error('Number of columns on line ' + this.lines + ' does not match header'); | ||
return err; | ||
} else { | ||
return Error('Number of columns is inconsistent on line ' + this.lines); | ||
err = this.error('Number of columns is inconsistent on line ' + this.lines); | ||
return err; | ||
} | ||
@@ -357,8 +368,8 @@ } else { | ||
if (this.options.objname) { | ||
row = [lineAsColumns[this.options.objname], lineAsColumns]; | ||
record = [lineAsColumns[this.options.objname], lineAsColumns]; | ||
} else { | ||
row = lineAsColumns; | ||
record = lineAsColumns; | ||
} | ||
} else { | ||
row = line; | ||
record = line; | ||
} | ||
@@ -374,8 +385,11 @@ if (this.count < this.options.from) { | ||
raw: this._.rawBuf, | ||
row: row | ||
row: record | ||
}); | ||
this._.rawBuf = ''; | ||
} else { | ||
this.push(row); | ||
this.push(record); | ||
} | ||
if (this.listenerCount('record')) { | ||
this.emit('record', record); | ||
} | ||
return null; | ||
@@ -385,25 +399,25 @@ }; | ||
Parser.prototype.__write = function (chars, end) { | ||
var _this = this; | ||
var _this2 = this; | ||
var areNextCharsDelimiter, areNextCharsRowDelimiters, auto_parse, char, err, escapeIsQuote, i, isDelimiter, isEscape, isNextCharAComment, isQuote, isRowDelimiter, isRowDelimiterLength, is_float, is_int, l, ltrim, nextCharPos, ref, ref1, ref2, ref3, ref4, ref5, remainingBuffer, rowDelimiter, rtrim, wasCommenting; | ||
is_int = function is_int(value) { | ||
if (typeof _this.is_int === 'function') { | ||
return _this.is_int(value); | ||
if (typeof _this2.is_int === 'function') { | ||
return _this2.is_int(value); | ||
} else { | ||
return _this.is_int.test(value); | ||
return _this2.is_int.test(value); | ||
} | ||
}; | ||
is_float = function is_float(value) { | ||
if (typeof _this.is_float === 'function') { | ||
return _this.is_float(value); | ||
if (typeof _this2.is_float === 'function') { | ||
return _this2.is_float(value); | ||
} else { | ||
return _this.is_float.test(value); | ||
return _this2.is_float.test(value); | ||
} | ||
}; | ||
auto_parse = function auto_parse(value) { | ||
if (!_this.options.auto_parse) { | ||
if (!_this2.options.auto_parse) { | ||
return value; | ||
} | ||
if (typeof _this.options.auto_parse === 'function') { | ||
return _this.options.auto_parse(value); | ||
if (typeof _this2.options.auto_parse === 'function') { | ||
return _this2.options.auto_parse(value); | ||
} | ||
@@ -415,4 +429,4 @@ // auto_parse == true | ||
value = parseFloat(value); | ||
} else if (_this.options.auto_parse_date) { | ||
value = _this.options.auto_parse_date(value); | ||
} else if (_this2.options.auto_parse_date) { | ||
value = _this2.options.auto_parse_date(value); | ||
} | ||
@@ -441,3 +455,3 @@ return value; | ||
}) || this.options.rowDelimiter && this._.quoting && l - i < this.options.quote.length + this._.rowDelimiterLength && this.options.rowDelimiter.some(function (rd) { | ||
return (_this.options.quote + rd).substr(0, l - i) === remainingBuffer; | ||
return (_this2.options.quote + rd).substr(0, l - i) === remainingBuffer; | ||
// Skip if the remaining buffer can be delimiter | ||
@@ -520,3 +534,5 @@ // Skip if the remaining buffer can be escape sequence | ||
} else { | ||
return Error('Invalid closing quote at line ' + (this.lines + 1) + '; found ' + JSON.stringify(this._.nextChar) + ' instead of delimiter ' + JSON.stringify(this.options.delimiter)); | ||
if (err = this.error('Invalid closing quote at line ' + (this.lines + 1) + '; found ' + JSON.stringify(this._.nextChar) + ' instead of delimiter ' + JSON.stringify(this.options.delimiter))) { | ||
return err; | ||
} | ||
} | ||
@@ -538,3 +554,5 @@ } else { | ||
} else if (this._.field != null && !this.options.relax) { | ||
return Error('Invalid opening quote at line ' + (this.lines + 1)); | ||
if (err = this.error('Invalid opening quote at line ' + (this.lines + 1))) { | ||
return err; | ||
} | ||
} | ||
@@ -590,7 +608,13 @@ } | ||
if (isRowDelimiter) { | ||
err = this.__push(this._.line); | ||
if (err) { | ||
return err; | ||
// End of record | ||
if (!this._.lineHasError) { | ||
err = this.__push(this._.line); | ||
if (err) { | ||
return err; | ||
} | ||
} | ||
// Some cleanup for the next row | ||
if (this._.lineHasError) { | ||
this._.lineHasError = false; | ||
} | ||
// Some cleanup for the next record | ||
this._.line = []; | ||
@@ -652,2 +676,16 @@ i += isRowDelimiterLength; | ||
Parser.prototype.error = function (msg) { | ||
var err; | ||
err = Error(msg); | ||
if (!this.options.skip_lines_with_error) { | ||
return err; | ||
} else { | ||
if (!this._.lineHasError) { | ||
this._.lineHasError = true; | ||
this.emit('skip', err); | ||
} | ||
} | ||
return null; | ||
}; | ||
// ## Utils | ||
@@ -654,0 +692,0 @@ isObjLiteral = function isObjLiteral(_obj) { |
'use strict'; | ||
// Generated by CoffeeScript 2.0.3 | ||
// Generated by CoffeeScript 2.2.3 | ||
// # CSV Parse Sync | ||
@@ -45,4 +45,7 @@ | ||
} | ||
parser._flush(function () {}); | ||
err = parser.__flush(); | ||
if (err) { | ||
throw err; | ||
} | ||
return records; | ||
}; |
107
lib/index.js
@@ -1,2 +0,2 @@ | ||
// Generated by CoffeeScript 2.0.3 | ||
// Generated by CoffeeScript 2.2.3 | ||
// # CSV Parser | ||
@@ -115,3 +115,3 @@ | ||
Parser = function(options = {}) { | ||
var base, base1, base10, base11, base12, base13, base14, base15, base16, base2, base3, base4, base5, base6, base7, base8, base9, k, v; | ||
var base, base1, base10, base11, base12, base13, base14, base15, base16, base17, base2, base3, base4, base5, base6, base7, base8, base9, k, v; | ||
// @options = options | ||
@@ -192,2 +192,5 @@ this.options = {}; | ||
} | ||
if ((base17 = this.options).skip_lines_with_error == null) { | ||
base17.skip_lines_with_error = false; | ||
} | ||
// Counters | ||
@@ -220,3 +223,4 @@ // lines = count + skipped_line_count + empty_line_count | ||
return v.length; | ||
})) : void 0 | ||
})) : void 0, | ||
lineHasError: false | ||
}; | ||
@@ -251,38 +255,40 @@ return this; | ||
Parser.prototype._transform = function(chunk, encoding, callback) { | ||
var err; | ||
if (chunk instanceof Buffer) { | ||
chunk = this._.decoder.write(chunk); | ||
} | ||
err = this.__write(chunk, false); | ||
if (err) { | ||
return this.emit('error', err); | ||
} | ||
return callback(); | ||
return setImmediate(() => { | ||
var err; | ||
if (chunk instanceof Buffer) { | ||
chunk = this._.decoder.write(chunk); | ||
} | ||
err = this.__write(chunk, false); | ||
if (err) { | ||
return this.emit('error', err); | ||
} | ||
return callback(); | ||
}); | ||
}; | ||
Parser.prototype._flush = function(callback) { | ||
return callback(this.__flush()); | ||
}; | ||
Parser.prototype.__flush = function() { | ||
var err; | ||
err = this.__write(this._.decoder.end(), true); | ||
if (err) { | ||
return this.emit('error', err); | ||
return err; | ||
} | ||
if (this._.quoting) { | ||
this.emit('error', new Error(`Quoted field not terminated at line ${this.lines + 1}`)); | ||
return; | ||
err = this.error(`Quoted field not terminated at line ${this.lines + 1}`); | ||
return err; | ||
} | ||
if (this._.line.length > 0) { | ||
err = this.__push(this._.line); | ||
if (err) { | ||
return callback(err); | ||
} | ||
return this.__push(this._.line); | ||
} | ||
return callback(); | ||
}; | ||
Parser.prototype.__push = function(line) { | ||
var call_column_udf, columns, err, field, i, j, len, lineAsColumns, row; | ||
var call_column_udf, columns, err, field, i, j, len, lineAsColumns, record; | ||
if (this.options.skip_lines_with_empty_values && line.join('').trim() === '') { | ||
return; | ||
} | ||
row = null; | ||
record = null; | ||
if (this.options.columns === true) { | ||
@@ -321,5 +327,8 @@ this.options.columns = line; | ||
} else if (this.options.columns != null) { | ||
return Error(`Number of columns on line ${this.lines} does not match header`); | ||
// Suggest: Inconsistent header and column numbers: header is 1 and number of columns is 1 on line 1 | ||
err = this.error(`Number of columns on line ${this.lines} does not match header`); | ||
return err; | ||
} else { | ||
return Error(`Number of columns is inconsistent on line ${this.lines}`); | ||
err = this.error(`Number of columns is inconsistent on line ${this.lines}`); | ||
return err; | ||
} | ||
@@ -339,8 +348,8 @@ } else { | ||
if (this.options.objname) { | ||
row = [lineAsColumns[this.options.objname], lineAsColumns]; | ||
record = [lineAsColumns[this.options.objname], lineAsColumns]; | ||
} else { | ||
row = lineAsColumns; | ||
record = lineAsColumns; | ||
} | ||
} else { | ||
row = line; | ||
record = line; | ||
} | ||
@@ -356,8 +365,11 @@ if (this.count < this.options.from) { | ||
raw: this._.rawBuf, | ||
row: row | ||
row: record | ||
}); | ||
this._.rawBuf = ''; | ||
} else { | ||
this.push(row); | ||
this.push(record); | ||
} | ||
if (this.listenerCount('record')) { | ||
this.emit('record', record); | ||
} | ||
return null; | ||
@@ -497,3 +509,5 @@ }; | ||
} else { | ||
return Error(`Invalid closing quote at line ${this.lines + 1}; found ${JSON.stringify(this._.nextChar)} instead of delimiter ${JSON.stringify(this.options.delimiter)}`); | ||
if (err = this.error(`Invalid closing quote at line ${this.lines + 1}; found ${JSON.stringify(this._.nextChar)} instead of delimiter ${JSON.stringify(this.options.delimiter)}`)) { | ||
return err; | ||
} | ||
} | ||
@@ -515,3 +529,5 @@ } else { | ||
} else if ((this._.field != null) && !this.options.relax) { | ||
return Error(`Invalid opening quote at line ${this.lines + 1}`); | ||
if (err = this.error(`Invalid opening quote at line ${this.lines + 1}`)) { | ||
return err; | ||
} | ||
} | ||
@@ -565,8 +581,13 @@ } | ||
} | ||
if (isRowDelimiter) { | ||
err = this.__push(this._.line); | ||
if (err) { | ||
return err; | ||
if (isRowDelimiter) { // End of record | ||
if (!this._.lineHasError) { | ||
err = this.__push(this._.line); | ||
if (err) { | ||
return err; | ||
} | ||
} | ||
// Some cleanup for the next row | ||
if (this._.lineHasError) { | ||
this._.lineHasError = false; | ||
} | ||
// Some cleanup for the next record | ||
this._.line = []; | ||
@@ -628,2 +649,16 @@ i += isRowDelimiterLength; | ||
Parser.prototype.error = function(msg) { | ||
var err; | ||
err = Error(msg); | ||
if (!this.options.skip_lines_with_error) { | ||
return err; | ||
} else { | ||
if (!this._.lineHasError) { | ||
this._.lineHasError = true; | ||
this.emit('skip', err); | ||
} | ||
} | ||
return null; | ||
}; | ||
// ## Utils | ||
@@ -630,0 +665,0 @@ isObjLiteral = function(_obj) { |
@@ -1,2 +0,2 @@ | ||
// Generated by CoffeeScript 2.0.3 | ||
// Generated by CoffeeScript 2.2.3 | ||
// # CSV Parse Sync | ||
@@ -38,4 +38,7 @@ | ||
} | ||
parser._flush((function() {})); | ||
err = parser.__flush(); | ||
if (err) { | ||
throw err; | ||
} | ||
return records; | ||
}; |
{ | ||
"version": "2.0.4", | ||
"version": "2.1.0", | ||
"name": "csv-parse", | ||
@@ -37,8 +37,8 @@ "description": "CSV parsing implementing the Node.js `stream.Transform` API", | ||
"babel-preset-es2015": "^6.24.1", | ||
"coffeescript": "~2.0.1", | ||
"csv-generate": "~2.0.0", | ||
"coffeescript": "~2.2.3", | ||
"csv-generate": "~2.0.2", | ||
"csv-spectrum": "~1.0.0", | ||
"each": "~0.6.1", | ||
"mocha": "~4.0.1", | ||
"should": "~13.1.2" | ||
"each": "~1.0.0", | ||
"mocha": "~5.0.5", | ||
"should": "~13.2.1" | ||
}, | ||
@@ -45,0 +45,0 @@ "optionalDependencies": {}, |
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
1603
66438
21