csv-parse
Advanced tools
Comparing version 1.1.12 to 1.2.0
199
lib/index.js
@@ -152,13 +152,13 @@ // Generated by CoffeeScript 1.10.0 | ||
}; | ||
this.decoder = new StringDecoder(); | ||
this.buf = ''; | ||
this.quoting = false; | ||
this.commenting = false; | ||
this.field = null; | ||
this.nextChar = null; | ||
this.closingQuote = 0; | ||
this.line = []; | ||
this.chunks = []; | ||
this.rawBuf = ''; | ||
this._ = {}; | ||
this._.decoder = new StringDecoder(); | ||
this._.quoting = false; | ||
this._.commenting = false; | ||
this._.field = null; | ||
this._.nextChar = null; | ||
this._.closingQuote = 0; | ||
this._.line = []; | ||
this._.chunks = []; | ||
this._.rawBuf = ''; | ||
this._.buf = ''; | ||
if (this.options.rowDelimiter) { | ||
@@ -179,3 +179,3 @@ this._.rowDelimiterLength = Math.max.apply(Math, this.options.rowDelimiter.map(function(v) { | ||
if (chunk instanceof Buffer) { | ||
chunk = this.decoder.write(chunk); | ||
chunk = this._.decoder.write(chunk); | ||
} | ||
@@ -194,9 +194,9 @@ try { | ||
try { | ||
this.__write(this.decoder.end(), true); | ||
if (this.quoting) { | ||
this.__write(this._.decoder.end(), true); | ||
if (this._.quoting) { | ||
this.emit('error', new Error("Quoted field not terminated at line " + (this.lines + 1))); | ||
return; | ||
} | ||
if (this.line.length > 0) { | ||
this.__push(this.line); | ||
if (this._.line.length > 0) { | ||
this.__push(this._.line); | ||
} | ||
@@ -225,8 +225,8 @@ return callback(); | ||
} | ||
if (!this.line_length && line.length > 0) { | ||
this.line_length = this.options.columns ? this.options.columns.length : line.length; | ||
if (!this._.line_length && line.length > 0) { | ||
this._.line_length = this.options.columns ? this.options.columns.length : line.length; | ||
} | ||
if (line.length === 1 && line[0] === '') { | ||
this.empty_line_count++; | ||
} else if (line.length !== this.line_length) { | ||
} else if (line.length !== this._.line_length) { | ||
if (this.options.relax_column_count) { | ||
@@ -267,6 +267,6 @@ this.skipped_line_count++; | ||
this.push({ | ||
raw: this.rawBuf, | ||
raw: this._.rawBuf, | ||
row: row | ||
}); | ||
return this.rawBuf = ''; | ||
return this._.rawBuf = ''; | ||
} else { | ||
@@ -300,7 +300,10 @@ return this.push(row); | ||
var m; | ||
if (_this.options.auto_parse && is_int(value)) { | ||
if (!_this.options.auto_parse) { | ||
return value; | ||
} | ||
if (is_int(value)) { | ||
value = parseInt(value); | ||
} else if (_this.options.auto_parse && is_float(value)) { | ||
} else if (is_float(value)) { | ||
value = parseFloat(value); | ||
} else if (_this.options.auto_parse && _this.options.auto_parse_date) { | ||
} else if (_this.options.auto_parse_date) { | ||
m = Date.parse(value); | ||
@@ -316,3 +319,3 @@ if (!isNaN(m)) { | ||
rtrim = this.options.trim || this.options.rtrim; | ||
chars = this.buf + chars; | ||
chars = this._.buf + chars; | ||
l = chars.length; | ||
@@ -326,5 +329,5 @@ i = 0; | ||
remainingBuffer = chars.substr(i, l - i); | ||
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) { | ||
if ((!this.options.rowDelimiter && i + 3 > l) || (!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) { | ||
})) || (this.options.rowDelimiter && this._.quoting && l - i < (this.options.quote.length + this._.rowDelimiterLength) && this.options.rowDelimiter.some((function(_this) { | ||
return function(rd) { | ||
@@ -337,16 +340,18 @@ return (_this.options.quote + rd).substr(0, l - i) === remainingBuffer; | ||
} | ||
char = this.nextChar ? this.nextChar : chars.charAt(i); | ||
this.nextChar = l > i + 1 ? chars.charAt(i + 1) : ''; | ||
char = this._.nextChar ? this._.nextChar : chars.charAt(i); | ||
this._.nextChar = l > i + 1 ? chars.charAt(i + 1) : ''; | ||
if (this.options.raw) { | ||
this.rawBuf += char; | ||
this._.rawBuf += char; | ||
} | ||
if (this.options.rowDelimiter == null) { | ||
if ((!this.quoting) && (char === '\n' || char === '\r')) { | ||
nextCharPos = i; | ||
rowDelimiter = null; | ||
if (!this._.quoting && (char === '\n' || char === '\r')) { | ||
rowDelimiter = char; | ||
nextCharPos = i + 1; | ||
} else if (this.nextChar === '\n' || this.nextChar === '\r') { | ||
rowDelimiter = this.nextChar; | ||
nextCharPos = i + 2; | ||
nextCharPos += 1; | ||
} else if (!(!this._.quoting && char === this.options.quote) && (this._.nextChar === '\n' || this._.nextChar === '\r')) { | ||
rowDelimiter = this._.nextChar; | ||
nextCharPos += 2; | ||
if (this.raw) { | ||
rawBuf += this.nextChar; | ||
rawBuf += this._.nextChar; | ||
} | ||
@@ -362,16 +367,16 @@ } | ||
} | ||
if (!this.commenting && char === this.options.escape) { | ||
if (!this._.commenting && char === this.options.escape) { | ||
escapeIsQuote = this.options.escape === this.options.quote; | ||
isEscape = this.nextChar === this.options.escape; | ||
isQuote = this.nextChar === this.options.quote; | ||
if (!(escapeIsQuote && (this.field == null) && !this.quoting) && (isEscape || isQuote)) { | ||
isEscape = this._.nextChar === this.options.escape; | ||
isQuote = this._.nextChar === this.options.quote; | ||
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 = ''; | ||
char = this._.nextChar; | ||
this._.nextChar = chars.charAt(i + 1); | ||
if (this._.field == null) { | ||
this._.field = ''; | ||
} | ||
this.field += char; | ||
this._.field += char; | ||
if (this.options.raw) { | ||
this.rawBuf += char; | ||
this._.rawBuf += char; | ||
} | ||
@@ -382,4 +387,4 @@ i++; | ||
} | ||
if (!this.commenting && char === this.options.quote) { | ||
if (this.quoting) { | ||
if (!this._.commenting && char === this.options.quote) { | ||
if (this._.quoting) { | ||
areNextCharsRowDelimiters = this.options.rowDelimiter && this.options.rowDelimiter.some(function(rd) { | ||
@@ -389,25 +394,25 @@ return chars.substr(i + 1, rd.length) === rd; | ||
areNextCharsDelimiter = chars.substr(i + 1, this.options.delimiter.length) === this.options.delimiter; | ||
isNextCharAComment = this.nextChar === this.options.comment; | ||
if (this.nextChar && !areNextCharsRowDelimiters && !areNextCharsDelimiter && !isNextCharAComment) { | ||
isNextCharAComment = this._.nextChar === this.options.comment; | ||
if (this._.nextChar && !areNextCharsRowDelimiters && !areNextCharsDelimiter && !isNextCharAComment) { | ||
if (this.options.relax) { | ||
this.quoting = false; | ||
this.field = "" + this.options.quote + this.field; | ||
this._.quoting = false; | ||
this._.field = "" + this.options.quote + this._.field; | ||
} else { | ||
throw Error("Invalid closing quote at line " + (this.lines + 1) + "; found " + (JSON.stringify(this.nextChar)) + " instead of delimiter " + (JSON.stringify(this.options.delimiter))); | ||
throw Error("Invalid closing quote at line " + (this.lines + 1) + "; found " + (JSON.stringify(this._.nextChar)) + " instead of delimiter " + (JSON.stringify(this.options.delimiter))); | ||
} | ||
} else { | ||
this.quoting = false; | ||
this.closingQuote = this.options.quote.length; | ||
this._.quoting = false; | ||
this._.closingQuote = this.options.quote.length; | ||
i++; | ||
if (end && i === l) { | ||
this.line.push(auto_parse(this.field || '')); | ||
this.field = null; | ||
this._.line.push(auto_parse(this._.field || '')); | ||
this._.field = null; | ||
} | ||
continue; | ||
} | ||
} else if (!this.field) { | ||
this.quoting = true; | ||
} else if (!this._.field) { | ||
this._.quoting = true; | ||
i++; | ||
continue; | ||
} else if ((this.field != null) && !this.options.relax) { | ||
} else if ((this._.field != null) && !this.options.relax) { | ||
throw Error("Invalid opening quote at line " + (this.lines + 1)); | ||
@@ -428,14 +433,14 @@ } | ||
wasCommenting = false; | ||
if (!this.commenting && !this.quoting && this.options.comment && chars.substr(i, this.options.comment.length) === this.options.comment) { | ||
this.commenting = true; | ||
} else if (this.commenting && isRowDelimiter) { | ||
if (!this._.commenting && !this._.quoting && this.options.comment && chars.substr(i, this.options.comment.length) === this.options.comment) { | ||
this._.commenting = true; | ||
} else if (this._.commenting && isRowDelimiter) { | ||
wasCommenting = true; | ||
this.commenting = false; | ||
this._.commenting = false; | ||
} | ||
isDelimiter = chars.substr(i, this.options.delimiter.length) === this.options.delimiter; | ||
if (!this.commenting && !this.quoting && (isDelimiter || isRowDelimiter)) { | ||
if (isRowDelimiter && this.line.length === 0 && (this.field == null)) { | ||
if (!this._.commenting && !this._.quoting && (isDelimiter || isRowDelimiter)) { | ||
if (isRowDelimiter && this._.line.length === 0 && (this._.field == null)) { | ||
if (wasCommenting || this.options.skip_empty_lines) { | ||
i += isRowDelimiterLength; | ||
this.nextChar = chars.charAt(i); | ||
this._.nextChar = chars.charAt(i); | ||
continue; | ||
@@ -445,37 +450,37 @@ } | ||
if (rtrim) { | ||
if (!this.closingQuote) { | ||
this.field = (ref = this.field) != null ? ref.trimRight() : void 0; | ||
if (!this._.closingQuote) { | ||
this._.field = (ref = this._.field) != null ? ref.trimRight() : void 0; | ||
} | ||
} | ||
this.line.push(auto_parse(this.field || '')); | ||
this.closingQuote = 0; | ||
this.field = null; | ||
this._.line.push(auto_parse(this._.field || '')); | ||
this._.closingQuote = 0; | ||
this._.field = null; | ||
if (isDelimiter) { | ||
i += this.options.delimiter.length; | ||
this.nextChar = chars.charAt(i); | ||
if (end && !this.nextChar) { | ||
this._.nextChar = chars.charAt(i); | ||
if (end && !this._.nextChar) { | ||
isRowDelimiter = true; | ||
this.line.push(''); | ||
this._.line.push(''); | ||
} | ||
} | ||
if (isRowDelimiter) { | ||
this.__push(this.line); | ||
this.line = []; | ||
this.__push(this._.line); | ||
this._.line = []; | ||
i += isRowDelimiterLength; | ||
this.nextChar = chars.charAt(i); | ||
this._.nextChar = chars.charAt(i); | ||
continue; | ||
} | ||
} else if (!this.commenting && !this.quoting && (char === ' ' || char === '\t')) { | ||
if (this.field == null) { | ||
this.field = ''; | ||
} else if (!this._.commenting && !this._.quoting && (char === ' ' || char === '\t')) { | ||
if (this._.field == null) { | ||
this._.field = ''; | ||
} | ||
if (!(ltrim && !this.field)) { | ||
this.field += char; | ||
if (!(ltrim && !this._.field)) { | ||
this._.field += char; | ||
} | ||
i++; | ||
} else if (!this.commenting) { | ||
if (this.field == null) { | ||
this.field = ''; | ||
} else if (!this._.commenting) { | ||
if (this._.field == null) { | ||
this._.field = ''; | ||
} | ||
this.field += char; | ||
this._.field += char; | ||
i++; | ||
@@ -485,6 +490,6 @@ } else { | ||
} | ||
if (!this.commenting && ((ref1 = this.field) != null ? ref1.length : void 0) > 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 && ((ref2 = this.line) != null ? ref2.length : void 0) > 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))); | ||
@@ -494,12 +499,12 @@ } | ||
if (end) { | ||
if (this.field != null) { | ||
if (this._.field != null) { | ||
if (rtrim) { | ||
if (!this.closingQuote) { | ||
this.field = (ref3 = this.field) != null ? ref3.trimRight() : void 0; | ||
if (!this._.closingQuote) { | ||
this._.field = (ref3 = this._.field) != null ? ref3.trimRight() : void 0; | ||
} | ||
} | ||
this.line.push(auto_parse(this.field || '')); | ||
this.field = null; | ||
this._.line.push(auto_parse(this._.field || '')); | ||
this._.field = null; | ||
} | ||
if (((ref4 = this.field) != null ? ref4.length : void 0) > 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))); | ||
@@ -510,10 +515,10 @@ } | ||
} | ||
if (this.line.length > this.options.max_limit_on_data_read) { | ||
if (this._.line.length > this.options.max_limit_on_data_read) { | ||
throw Error("Row delimiter not found in the file " + (JSON.stringify(this.options.rowDelimiter))); | ||
} | ||
} | ||
this.buf = ''; | ||
this._.buf = ''; | ||
results = []; | ||
while (i < l) { | ||
this.buf += chars.charAt(i); | ||
this._.buf += chars.charAt(i); | ||
results.push(i++); | ||
@@ -520,0 +525,0 @@ } |
{ | ||
"version": "1.1.12", | ||
"version": "1.2.0", | ||
"name": "csv-parse", | ||
@@ -4,0 +4,0 @@ "description": "CSV parsing implementing the Node.js `stream.Transform` API", |
Sorry, the diff of this file is not supported yet
25640
641