csv-parse
Advanced tools
Comparing version 1.1.10 to 1.1.11
187
lib/index.js
@@ -93,3 +93,3 @@ // Generated by CoffeeScript 1.10.0 | ||
} | ||
if (typeof this.options.rowDelimiter === "string") { | ||
if (typeof this.options.rowDelimiter === 'string') { | ||
this.options.rowDelimiter = [this.options.rowDelimiter]; | ||
@@ -157,3 +157,3 @@ } | ||
this.commenting = false; | ||
this.field = ''; | ||
this.field = null; | ||
this.nextChar = null; | ||
@@ -164,3 +164,8 @@ this.closingQuote = 0; | ||
this.rawBuf = ''; | ||
this.rowDelimiter = null; | ||
this._ = {}; | ||
if (this.options.rowDelimiter) { | ||
this._.rowDelimiterLength = Math.max.apply(Math, this.options.rowDelimiter.map(function(v) { | ||
return v.length; | ||
})); | ||
} | ||
return this; | ||
@@ -206,6 +211,7 @@ }; | ||
Parser.prototype.__push = function(line) { | ||
var field, i, j, len, lineAsColumns, rawBuf; | ||
var field, i, j, len, lineAsColumns, rawBuf, row; | ||
if (this.options.skip_lines_with_empty_values && line.join('').trim() === '') { | ||
return; | ||
} | ||
row = null; | ||
if (this.options.columns === true) { | ||
@@ -246,6 +252,8 @@ this.options.columns = line; | ||
if (this.options.objname) { | ||
line = [lineAsColumns[this.options.objname], lineAsColumns]; | ||
row = [lineAsColumns[this.options.objname], lineAsColumns]; | ||
} else { | ||
line = lineAsColumns; | ||
row = lineAsColumns; | ||
} | ||
} else { | ||
row = line; | ||
} | ||
@@ -255,7 +263,7 @@ if (this.options.raw) { | ||
raw: this.rawBuf, | ||
row: line | ||
row: row | ||
}); | ||
return this.rawBuf = ''; | ||
} else { | ||
return this.push(line); | ||
return this.push(row); | ||
} | ||
@@ -265,3 +273,3 @@ }; | ||
Parser.prototype.__write = function(chars, end) { | ||
var areNextCharsDelimiter, areNextCharsRowDelimiters, auto_parse, char, escapeIsQuote, i, isDelimiter, isEscape, isNextCharAComment, isQuote, isRowDelimiter, is_float, is_int, is_rem_buf_rowDelimiter, is_rem_buf_rowDelimiter_following_closing_quote, l, ltrim, match_chars_with_row_delimiters, matched, nextCharPos, ref, ref1, ref2, remainingBuffer, results, rowDelimiter, rtrim, rtrimed, wasCommenting; | ||
var areNextCharsDelimiter, areNextCharsRowDelimiters, auto_parse, char, escapeIsQuote, i, isDelimiter, isEscape, isNextCharAComment, isQuote, isRowDelimiter, isRowDelimiterLength, is_float, is_int, l, ltrim, nextCharPos, ref, ref1, ref2, ref3, ref4, remainingBuffer, results, rowDelimiter, rtrim, wasCommenting; | ||
is_int = (function(_this) { | ||
@@ -301,75 +309,2 @@ return function(value) { | ||
})(this); | ||
is_rem_buf_rowDelimiter = (function(_this) { | ||
return function(remainingBuffer, remainingBufferLength) { | ||
var j, len, ref, rowDelimiter, rowDelimiterMatched; | ||
rowDelimiterMatched = false; | ||
if (_this.rowDelimiter != null) { | ||
if (remainingBufferLength < _this.rowDelimiter.length && _this.rowDelimiter.substr(0, remainingBufferLength) === remainingBuffer) { | ||
rowDelimiterMatched = true; | ||
} | ||
} else if (_this.options.rowDelimiter != null) { | ||
ref = _this.options.rowDelimiter; | ||
for (j = 0, len = ref.length; j < len; j++) { | ||
rowDelimiter = ref[j]; | ||
if (remainingBufferLength < rowDelimiter.length && rowDelimiter.substr(0, remainingBufferLength) === remainingBuffer) { | ||
rowDelimiterMatched = true; | ||
break; | ||
} | ||
} | ||
} | ||
return rowDelimiterMatched; | ||
}; | ||
})(this); | ||
is_rem_buf_rowDelimiter_following_closing_quote = (function(_this) { | ||
return function(remainingBuffer, remainingBufferLength) { | ||
var j, len, ref, rowDelimiter, rowDelimiterFollowingClosingQuoteMatched; | ||
rowDelimiterFollowingClosingQuoteMatched = false; | ||
if (_this.rowDelimiter != null) { | ||
if (_this.quoting && remainingBufferLength < (_this.options.quote.length + _this.rowDelimiter.length) && (_this.options.quote + _this.rowDelimiter).substr(0, remainingBufferLength) === remainingBuffer) { | ||
rowDelimiterFollowingClosingQuoteMatched = true; | ||
} | ||
} else if (_this.options.rowDelimiter != null) { | ||
ref = _this.options.rowDelimiter; | ||
for (j = 0, len = ref.length; j < len; j++) { | ||
rowDelimiter = ref[j]; | ||
if (_this.quoting && remainingBufferLength < (_this.options.quote.length + rowDelimiter.length) && (_this.options.quote + rowDelimiter).substr(0, remainingBufferLength) === remainingBuffer) { | ||
rowDelimiterFollowingClosingQuoteMatched = true; | ||
break; | ||
} | ||
} | ||
} | ||
return rowDelimiterFollowingClosingQuoteMatched; | ||
}; | ||
})(this); | ||
match_chars_with_row_delimiters = (function(_this) { | ||
return function(chars, index) { | ||
var j, len, matchedRowDelimiter, ref, rowDelimiter; | ||
matchedRowDelimiter = null; | ||
if (_this.rowDelimiter != null) { | ||
if (chars.substr(index, _this.rowDelimiter.length) === _this.rowDelimiter) { | ||
matchedRowDelimiter = _this.rowDelimiter; | ||
} | ||
} else if (_this.options.rowDelimiter != null) { | ||
ref = _this.options.rowDelimiter; | ||
for (j = 0, len = ref.length; j < len; j++) { | ||
rowDelimiter = ref[j]; | ||
if (chars.substr(index, rowDelimiter.length) === rowDelimiter) { | ||
matchedRowDelimiter = rowDelimiter; | ||
break; | ||
} | ||
} | ||
} | ||
if (matchedRowDelimiter != null) { | ||
return { | ||
rowDelimiter: matchedRowDelimiter, | ||
matched: true | ||
}; | ||
} else { | ||
return { | ||
rowDelimiter: matchedRowDelimiter, | ||
matched: false | ||
}; | ||
} | ||
}; | ||
})(this); | ||
ltrim = this.options.trim || this.options.ltrim; | ||
@@ -386,3 +321,9 @@ rtrim = this.options.trim || this.options.rtrim; | ||
remainingBuffer = chars.substr(i, l - i); | ||
if ((!this.commenting && l - i < this.options.comment.length && this.options.comment.substr(0, l - i) === remainingBuffer) || is_rem_buf_rowDelimiter(remainingBuffer, l - i) || is_rem_buf_rowDelimiter_following_closing_quote(remainingBuffer, l - i) || (l - i <= this.options.delimiter.length && this.options.delimiter.substr(0, l - i) === remainingBuffer) || (l - i <= this.options.escape.length && this.options.escape.substr(0, l - i) === remainingBuffer)) { | ||
if ((!this.commenting && l - i < this.options.comment.length && this.options.comment.substr(0, l - i) === remainingBuffer) || (this.options.rowDelimiter && l - i < this._.rowDelimiterLength && this.options.rowDelimiter.some(function(rd) { | ||
return rd.substr(0, l - i) === remainingBuffer; | ||
})) || (this.options.rowDelimiter && this.quoting && l - i < (this.options.quote.length + this._.rowDelimiterLength) && this.options.rowDelimiter.some((function(_this) { | ||
return function(rd) { | ||
return (_this.options.quote + rd).substr(0, l - i) === remainingBuffer; | ||
}; | ||
})(this))) || (l - i <= this.options.delimiter.length && this.options.delimiter.substr(0, l - i) === remainingBuffer) || (l - i <= this.options.escape.length && this.options.escape.substr(0, l - i) === remainingBuffer)) { | ||
break; | ||
@@ -396,3 +337,3 @@ } | ||
} | ||
if (!(((ref = this.options.rowDelimiter) != null ? ref.length : void 0) > 0)) { | ||
if (this.options.rowDelimiter == null) { | ||
if ((!this.quoting) && (char === '\n' || char === '\r')) { | ||
@@ -412,3 +353,4 @@ rowDelimiter = char; | ||
} | ||
this.rowDelimiter = rowDelimiter; | ||
this.options.rowDelimiter = [rowDelimiter]; | ||
this._.rowDelimiterLength = rowDelimiter.length; | ||
} | ||
@@ -420,6 +362,9 @@ } | ||
isQuote = this.nextChar === this.options.quote; | ||
if (!(escapeIsQuote && !this.field && !this.quoting) && (isEscape || isQuote)) { | ||
if (!(escapeIsQuote && (this.field == null) && !this.quoting) && (isEscape || isQuote)) { | ||
i++; | ||
char = this.nextChar; | ||
this.nextChar = chars.charAt(i + 1); | ||
if (this.field == null) { | ||
this.field = ''; | ||
} | ||
this.field += char; | ||
@@ -435,4 +380,5 @@ if (this.options.raw) { | ||
if (this.quoting) { | ||
ref1 = match_chars_with_row_delimiters(chars, i + 1), matched = ref1.matched, rowDelimiter = ref1.rowDelimiter; | ||
areNextCharsRowDelimiters = matched; | ||
areNextCharsRowDelimiters = this.options.rowDelimiter && this.options.rowDelimiter.some(function(rd) { | ||
return chars.substr(i + 1, rd.length) === rd; | ||
}); | ||
areNextCharsDelimiter = chars.substr(i + 1, this.options.delimiter.length) === this.options.delimiter; | ||
@@ -452,4 +398,4 @@ isNextCharAComment = this.nextChar === this.options.comment; | ||
if (end && i === l) { | ||
this.line.push(auto_parse(this.field)); | ||
this.field = ''; | ||
this.line.push(auto_parse(this.field || '')); | ||
this.field = null; | ||
} | ||
@@ -462,8 +408,14 @@ continue; | ||
continue; | ||
} else if (this.field && !this.options.relax) { | ||
} else if ((this.field != null) && !this.options.relax) { | ||
throw Error("Invalid opening quote at line " + (this.lines + 1)); | ||
} | ||
} | ||
ref2 = match_chars_with_row_delimiters(chars, i), matched = ref2.matched, rowDelimiter = ref2.rowDelimiter; | ||
isRowDelimiter = matched; | ||
isRowDelimiter = this.options.rowDelimiter && this.options.rowDelimiter.some(function(rd) { | ||
return chars.substr(i, rd.length) === rd; | ||
}); | ||
if (isRowDelimiter) { | ||
isRowDelimiterLength = this.options.rowDelimiter.filter(function(rd) { | ||
return chars.substr(i, rd.length) === rd; | ||
})[0].length; | ||
} | ||
if (isRowDelimiter || (end && i === l - 1)) { | ||
@@ -481,7 +433,5 @@ this.lines++; | ||
if (!this.commenting && !this.quoting && (isDelimiter || isRowDelimiter)) { | ||
if (isRowDelimiter && this.line.length === 0 && this.field === '') { | ||
if (isRowDelimiter && this.line.length === 0 && (this.field == null)) { | ||
if (wasCommenting || this.options.skip_empty_lines) { | ||
if (rowDelimiter != null) { | ||
i += rowDelimiter.length; | ||
} | ||
i += isRowDelimiterLength; | ||
this.nextChar = chars.charAt(i); | ||
@@ -493,8 +443,8 @@ continue; | ||
if (!this.closingQuote) { | ||
this.field = this.field.trimRight(); | ||
this.field = (ref = this.field) != null ? ref.trimRight() : void 0; | ||
} | ||
} | ||
this.line.push(auto_parse(this.field)); | ||
this.line.push(auto_parse(this.field || '')); | ||
this.closingQuote = 0; | ||
this.field = ''; | ||
this.field = null; | ||
if (isDelimiter) { | ||
@@ -511,5 +461,3 @@ i += this.options.delimiter.length; | ||
this.line = []; | ||
if (rowDelimiter != null) { | ||
i += rowDelimiter.length; | ||
} | ||
i += isRowDelimiterLength; | ||
this.nextChar = chars.charAt(i); | ||
@@ -519,12 +467,13 @@ continue; | ||
} else if (!this.commenting && !this.quoting && (char === ' ' || char === '\t')) { | ||
if (this.field == null) { | ||
this.field = ''; | ||
} | ||
if (!(ltrim && !this.field)) { | ||
this.field += char; | ||
} | ||
if (end && i + 1 === l) { | ||
if (this.options.trim || this.options.rtrim) { | ||
this.field = this.field.trimRight(); | ||
} | ||
} | ||
i++; | ||
} else if (!this.commenting) { | ||
if (this.field == null) { | ||
this.field = ''; | ||
} | ||
this.field += char; | ||
@@ -535,6 +484,6 @@ i++; | ||
} | ||
if (!this.commenting && this.field.length > this.options.max_limit_on_data_read) { | ||
if (!this.commenting && ((ref1 = this.field) != null ? ref1.length : void 0) > this.options.max_limit_on_data_read) { | ||
throw Error("Delimiter not found in the file " + (JSON.stringify(this.options.delimiter))); | ||
} | ||
if (!this.commenting && this.line.length > this.options.max_limit_on_data_read) { | ||
if (!this.commenting && ((ref2 = this.line) != null ? ref2.length : void 0) > this.options.max_limit_on_data_read) { | ||
throw Error("Row delimiter not found in the file " + (JSON.stringify(this.options.rowDelimiter))); | ||
@@ -544,14 +493,12 @@ } | ||
if (end) { | ||
rtrimed = false; | ||
if (rtrim && !this.closingQuote && !isDelimiter) { | ||
this.field = this.field.trimRight(); | ||
rtrimed = true; | ||
} | ||
if (this.field !== '' || rtrimed) { | ||
if (!(this.field === '' && this.options.skip_empty_lines)) { | ||
this.line.push(auto_parse(this.field)); | ||
if (this.field != null) { | ||
if (rtrim) { | ||
if (!this.closingQuote) { | ||
this.field = (ref3 = this.field) != null ? ref3.trimRight() : void 0; | ||
} | ||
} | ||
this.field = ''; | ||
this.line.push(auto_parse(this.field || '')); | ||
this.field = null; | ||
} | ||
if (this.field.length > this.options.max_limit_on_data_read) { | ||
if (((ref4 = this.field) != null ? ref4.length : void 0) > this.options.max_limit_on_data_read) { | ||
throw Error("Delimiter not found in the file " + (JSON.stringify(this.options.delimiter))); | ||
@@ -558,0 +505,0 @@ } |
{ | ||
"version": "1.1.10", | ||
"version": "1.1.11", | ||
"name": "csv-parse", | ||
@@ -4,0 +4,0 @@ "description": "CSV parsing implementing the Node.js `stream.Transform` API", |
18
25463
630