csv-parse
Advanced tools
Comparing version 4.0.1 to 4.1.0
# Changelog | ||
## Version 4.1.0 | ||
New features: | ||
* options: accept camelize and underscore forms | ||
* cast: dont call cast for non column-mappable fields | ||
Fix: | ||
* cast: ensure column is a string and not an array | ||
* stream: handle empty input streams | ||
* cast: function may return non-string values | ||
* stream: pass stream options without modification | ||
## Version 4.0.1 | ||
@@ -5,0 +19,0 @@ |
@@ -21,2 +21,6 @@ "use strict"; | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; } | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
@@ -82,11 +86,7 @@ | ||
var options = {}; | ||
_this = _possibleConstructorReturn(this, _getPrototypeOf(Parser).call(this, _objectSpread({}, { | ||
readableObjectMode: true | ||
}, opts))); | ||
var options = {}; // Import default options | ||
for (var i in opts) { | ||
options[i] = opts[i]; | ||
} | ||
options.readableObjectMode = true; | ||
_this = _possibleConstructorReturn(this, _getPrototypeOf(Parser).call(this, options)); // Import default options | ||
for (var k in default_options) { | ||
@@ -96,2 +96,7 @@ if (options[k] === undefined) { | ||
} | ||
} // Merge with user options | ||
for (var opt in opts) { | ||
options[underscore(opt)] = opts[opt]; | ||
} // Normalize option `cast` | ||
@@ -317,4 +322,9 @@ | ||
if (previousBuf === undefined && nextBuf !== undefined) { | ||
buf = nextBuf; | ||
if (previousBuf === undefined) { | ||
if (nextBuf === undefined) { | ||
this.push(null); | ||
return; | ||
} else { | ||
buf = nextBuf; | ||
} | ||
} else if (previousBuf !== undefined && nextBuf === undefined) { | ||
@@ -354,5 +364,5 @@ buf = previousBuf; | ||
if (this.state.quoting === false && record_delimiter.length === 0) { | ||
var recordDelimiterCount = this.__autoDiscoverRowDelimiter(buf, pos); | ||
var record_delimiterCount = this.__autoDiscoverRowDelimiter(buf, pos); | ||
if (recordDelimiterCount) { | ||
if (record_delimiterCount) { | ||
record_delimiter = this.options.record_delimiter; | ||
@@ -719,3 +729,4 @@ } | ||
cast = _this$options3.cast, | ||
rtrim = _this$options3.rtrim; | ||
rtrim = _this$options3.rtrim, | ||
max_record_size = _this$options3.max_record_size; | ||
var _this$state3 = this.state, | ||
@@ -745,5 +756,8 @@ enabled = _this$state3.enabled, | ||
this.state.record.push(field); | ||
this.state.record_length += field.length; | ||
this.state.record.push(field); // Increment record length if record size must not exceed a limit | ||
if (max_record_size !== 0 && typeof field === 'string') { | ||
this.state.record_length += field.length; | ||
} | ||
this.__resetField(); | ||
@@ -760,4 +774,10 @@ } | ||
value: function __cast(field) { | ||
var isColumns = Array.isArray(this.options.columns); // Dont loose time calling cast if the field wont be part of the final record | ||
if (isColumns === true && this.options.columns.length <= this.state.record.length) { | ||
return [undefined, undefined]; | ||
} | ||
var context = { | ||
column: Array.isArray(this.options.columns) === true ? this.options.columns[this.state.record.length] : this.state.record.length, | ||
column: isColumns === true ? this.options.columns[this.state.record.length].name : this.state.record.length, | ||
empty_lines: this.info.empty_lines, | ||
@@ -970,2 +990,8 @@ header: this.options.columns === true, | ||
var underscore = function underscore(str) { | ||
return str.replace(/([A-Z])/g, function (_, match, index) { | ||
return '_' + match.toLowerCase(); | ||
}); | ||
}; | ||
var isObject = function isObject(obj) { | ||
@@ -972,0 +998,0 @@ return _typeof(obj) === 'object' && obj !== null && !Array.isArray(obj); |
@@ -35,8 +35,4 @@ | ||
constructor(opts = {}){ | ||
super({...{readableObjectMode: true}, ...opts}) | ||
const options = {} | ||
for(let i in opts){ | ||
options[i] = opts[i] | ||
} | ||
options.readableObjectMode = true | ||
super(options) | ||
// Import default options | ||
@@ -48,2 +44,6 @@ for(let k in default_options){ | ||
} | ||
// Merge with user options | ||
for(let opt in opts){ | ||
options[underscore(opt)] = opts[opt] | ||
} | ||
// Normalize option `cast` | ||
@@ -215,4 +215,9 @@ let fnCastField = null | ||
let buf | ||
if(previousBuf === undefined && nextBuf !== undefined){ | ||
buf = nextBuf | ||
if(previousBuf === undefined){ | ||
if(nextBuf === undefined){ | ||
this.push(null) | ||
return | ||
}else{ | ||
buf = nextBuf | ||
} | ||
}else if(previousBuf !== undefined && nextBuf === undefined){ | ||
@@ -246,4 +251,4 @@ buf = previousBuf | ||
if(this.state.quoting === false && record_delimiter.length === 0){ | ||
const recordDelimiterCount = this.__autoDiscoverRowDelimiter(buf, pos) | ||
if(recordDelimiterCount){ | ||
const record_delimiterCount = this.__autoDiscoverRowDelimiter(buf, pos) | ||
if(record_delimiterCount){ | ||
record_delimiter = this.options.record_delimiter | ||
@@ -444,3 +449,3 @@ } | ||
} | ||
if( skip_lines_with_empty_values === true){ | ||
if(skip_lines_with_empty_values === true){ | ||
if(record.map( (field) => field.trim() ).join('') === ''){ | ||
@@ -525,3 +530,3 @@ this.__resetRow() | ||
__onField(){ | ||
const {cast, rtrim} = this.options | ||
const {cast, rtrim, max_record_size} = this.options | ||
const {enabled, wasQuoting} = this.state | ||
@@ -542,3 +547,6 @@ // Deal with from_to options | ||
this.state.record.push(field) | ||
this.state.record_length += field.length | ||
// Increment record length if record size must not exceed a limit | ||
if(max_record_size !== 0 && typeof field === 'string'){ | ||
this.state.record_length += field.length | ||
} | ||
this.__resetField() | ||
@@ -551,4 +559,11 @@ } | ||
__cast(field){ | ||
const isColumns = Array.isArray(this.options.columns) | ||
// Dont loose time calling cast if the field wont be part of the final record | ||
if( isColumns === true && this.options.columns.length <= this.state.record.length ){ | ||
return [undefined, undefined] | ||
} | ||
const context = { | ||
column: Array.isArray(this.options.columns) === true ? this.options.columns[this.state.record.length] : this.state.record.length, | ||
column: isColumns === true ? | ||
this.options.columns[this.state.record.length].name : | ||
this.state.record.length, | ||
empty_lines: this.info.empty_lines, | ||
@@ -720,2 +735,8 @@ header: this.options.columns === true, | ||
const underscore = function(str){ | ||
return str.replace(/([A-Z])/g, function(_, match, index){ | ||
return '_' + match.toLowerCase() | ||
}) | ||
} | ||
const isObject = function(obj){ | ||
@@ -722,0 +743,0 @@ return (typeof obj === 'object' && obj !== null && !Array.isArray(obj)) |
{ | ||
"version": "4.0.1", | ||
"version": "4.1.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
94371
32
2188