csv-parse = function () { var callback, called, chunks, data, options, parser; if (arguments.length === 3) { data = arguments[0]; options = arguments[1]; callback = arguments[2]; if (typeof callback !== 'function') { throw Error("Invalid callback argument: " + (JSON.stringify(callback))); } if (!(typeof data === 'string' || Buffer.isBuffer(arguments[0]))) { return callback(Error("Invalid data argument: " + (JSON.stringify(data)))); } } else if (arguments.length === 2) { if (typeof arguments[0] === 'string' || Buffer.isBuffer(arguments[0])) { data = arguments[0]; } else { options = arguments[0]; } if (typeof arguments[1] === 'function') { callback = arguments[1]; } else { options = arguments[1]; } } else if (arguments.length === 1) { if (typeof arguments[0] === 'function') { callback = arguments[0]; } else { options = arguments[0]; } } if (options == null) { options = {}; } parser = new Parser(options); if (data != null) { process.nextTick(function() { parser.write(data); return parser.end(); }); } if (callback) { called = false; chunks = options.objname ? {} : []; parser.on('readable', function() { var chunk, results; results = []; while (chunk = parser.read()) { if (options.objname) { results.push(chunks[chunk[0]] = chunk[1]); } else { results.push(chunks.push(chunk)); } } return results; }); parser.on('error', function(err) { called = true; return callback(err); }); parser.on('end', function() { if (!called) { return callback(null, chunks); } }); } return parser; }
n/a
Parser = function (options) { var base, base1, base10, base11, base12, base13, base14, base15, base16, base2, base3, base4, base5, base6, base7, base8, base9 , k, v; if (options == null) { options = {}; } options.objectMode = true; this.options = {}; for (k in options) { v = options[k]; this.options[k] = v; } stream.Transform.call(this, this.options); if ((base = this.options).rowDelimiter == null) { base.rowDelimiter = null; } if (typeof this.options.rowDelimiter === 'string') { this.options.rowDelimiter = [this.options.rowDelimiter]; } if ((base1 = this.options).delimiter == null) { base1.delimiter = ','; } if ((base2 = this.options).quote == null) { base2.quote = '"'; } if ((base3 = this.options).escape == null) { base3.escape = '"'; } if ((base4 = this.options).columns == null) { base4.columns = null; } if ((base5 = this.options).comment == null) { base5.comment = ''; } if ((base6 = this.options).objname == null) { base6.objname = false; } if ((base7 = this.options).trim == null) { base7.trim = false; } if ((base8 = this.options).ltrim == null) { base8.ltrim = false; } if ((base9 = this.options).rtrim == null) { base9.rtrim = false; } if ((base10 = this.options).auto_parse == null) { base10.auto_parse = false; } if ((base11 = this.options).auto_parse_date == null) { base11.auto_parse_date = false; } if ((base12 = this.options).relax == null) { base12.relax = false; } if ((base13 = this.options).relax_column_count == null) { base13.relax_column_count = false; } if ((base14 = this.options).skip_empty_lines == null) { base14.skip_empty_lines = false; } if ((base15 = this.options).max_limit_on_data_read == null) { base15.max_limit_on_data_read = 128000; } if ((base16 = this.options).skip_lines_with_empty_values == null) { base16.skip_lines_with_empty_values = false; } this.lines = 0; this.count = 0; this.skipped_line_count = 0; this.empty_line_count = 0; this.is_int = /^(\-|\+)?([1-9]+[0-9]*)$/; this.is_float = function(value) { return (value - parseFloat(value) + 1) >= 0; }; 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) { this._.rowDelimiterLength = Math.max.apply(Math, this.options.rowDelimiter.map(function(v) { return v.length; })); } return this; }
...
options = {};
}
records = options.objname ? {} : [];
if (data instanceof Buffer) {
decoder = new StringDecoder();
data = decoder.write(data);
}
parser = new parse.Parser(options);
parser.push = function(record) {
if (options.objname) {
return records[record[0]] = record[1];
} else {
return records.push(record);
}
};
...
Parser = function (options) { var base, base1, base10, base11, base12, base13, base14, base15, base16, base2, base3, base4, base5, base6, base7, base8, base9 , k, v; if (options == null) { options = {}; } options.objectMode = true; this.options = {}; for (k in options) { v = options[k]; this.options[k] = v; } stream.Transform.call(this, this.options); if ((base = this.options).rowDelimiter == null) { base.rowDelimiter = null; } if (typeof this.options.rowDelimiter === 'string') { this.options.rowDelimiter = [this.options.rowDelimiter]; } if ((base1 = this.options).delimiter == null) { base1.delimiter = ','; } if ((base2 = this.options).quote == null) { base2.quote = '"'; } if ((base3 = this.options).escape == null) { base3.escape = '"'; } if ((base4 = this.options).columns == null) { base4.columns = null; } if ((base5 = this.options).comment == null) { base5.comment = ''; } if ((base6 = this.options).objname == null) { base6.objname = false; } if ((base7 = this.options).trim == null) { base7.trim = false; } if ((base8 = this.options).ltrim == null) { base8.ltrim = false; } if ((base9 = this.options).rtrim == null) { base9.rtrim = false; } if ((base10 = this.options).auto_parse == null) { base10.auto_parse = false; } if ((base11 = this.options).auto_parse_date == null) { base11.auto_parse_date = false; } if ((base12 = this.options).relax == null) { base12.relax = false; } if ((base13 = this.options).relax_column_count == null) { base13.relax_column_count = false; } if ((base14 = this.options).skip_empty_lines == null) { base14.skip_empty_lines = false; } if ((base15 = this.options).max_limit_on_data_read == null) { base15.max_limit_on_data_read = 128000; } if ((base16 = this.options).skip_lines_with_empty_values == null) { base16.skip_lines_with_empty_values = false; } this.lines = 0; this.count = 0; this.skipped_line_count = 0; this.empty_line_count = 0; this.is_int = /^(\-|\+)?([1-9]+[0-9]*)$/; this.is_float = function(value) { return (value - parseFloat(value) + 1) >= 0; }; 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) { this._.rowDelimiterLength = Math.max.apply(Math, this.options.rowDelimiter.map(function(v) { return v.length; })); } return this; }
...
options = {};
}
records = options.objname ? {} : [];
if (data instanceof Buffer) {
decoder = new StringDecoder();
data = decoder.write(data);
}
parser = new parse.Parser(options);
parser.push = function(record) {
if (options.objname) {
return records[record[0]] = record[1];
} else {
return records.push(record);
}
};
...
function Transform(options) { if (!(this instanceof Transform)) return new Transform(options); Duplex.call(this, options); this._transformState = new TransformState(this); var stream = this; // start out asking for a readable event once data is transformed. this._readableState.needReadable = true; // we have implemented the _read method, and done the other things // that Readable wants before the first _read call, so unset the // sync guard flag. this._readableState.sync = false; if (options) { if (typeof options.transform === 'function') this._transform = options.transform; if (typeof options.flush === 'function') this._flush = options.flush; } // When the writable side finishes, then flush out anything remaining. this.once('prefinish', function() { if (typeof this._flush === 'function') this._flush(function(er) { done(stream, er); }); else done(stream); }); }
n/a
__push = function (line) { 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) { this.options.columns = line; rawBuf = ''; return; } else if (typeof this.options.columns === 'function') { this.options.columns = this.options.columns(line); rawBuf = ''; return; } 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) { if (this.options.relax_column_count) { this.skipped_line_count++; } else if (this.options.columns != null) { throw Error("Number of columns on line " + this.lines + " does not match header"); } else { throw Error("Number of columns is inconsistent on line " + this.lines); } } else { this.count++; } if (this.options.columns != null) { lineAsColumns = {}; for (i = j = 0, len = line.length; j < len; i = ++j) { field = line[i]; if (this.options.columns[i] === false) { continue; } lineAsColumns[this.options.columns[i]] = field; } if (this.options.objname) { row = [lineAsColumns[this.options.objname], lineAsColumns]; } else { row = lineAsColumns; } } else { row = line; } if (this.count < this.options.from) { return; } if (this.count > this.options.to) { return; } if (this.options.raw) { this.push({ raw: this._.rawBuf, row: row }); return this._.rawBuf = ''; } else { return this.push(row); } }
...
try {
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);
}
return callback();
} catch (error) {
err = error;
return this.emit('error', err);
}
};
...
__write = function (chars, end) { 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) { return function(value) { if (typeof _this.is_int === 'function') { return _this.is_int(value); } else { return _this.is_int.test(value); } }; })(this); is_float = (function(_this) { return function(value) { if (typeof _this.is_float === 'function') { return _this.is_float(value); } else { return _this.is_float.test(value); } }; })(this); auto_parse = (function(_this) { return function(value) { var m; if (!_this.options.auto_parse) { return value; } if (is_int(value)) { value = parseInt(value); } else if (is_float(value)) { value = parseFloat(value); } else if (_this.options.auto_parse_date) { m = Date.parse(value); if (!isNaN(m)) { value = new Date(m); } } return value; }; })(this); ltrim = this.options.trim || this.options.ltrim; rtrim = this.options.trim || this.options.rtrim; chars = this._.buf + chars; l = chars.length; i = 0; if (this.lines === 0 && 0xFEFF === chars.charCodeAt(0)) { i++; } while (i < l) { if (!end) { remainingBuffer = chars.substr(i, l - i); 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) { 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; } } char = this._.nextChar ? this._.nextChar : chars.charAt(i); this._.nextChar = l > i + 1 ? chars.charAt(i + 1) : ''; if (this.options.raw) { this._.rawBuf += char; } if (this.options.rowDelimiter == null) { nextCharPos = i; rowDelimiter = null; if (!this._.quoting && (char === '\n' || char === '\r')) { rowDelimiter = char; 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; } } if (rowDelimiter) { if (rowDelimiter === '\r' && chars.charAt(nextCharPos) === '\n') { rowDelimiter += '\n'; } this.options.rowDelimiter = [rowDelimiter]; this._.rowDelimiterLength = rowDelimiter.length; } } 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)) { i++; char = this._.nextChar; this._.nextChar = chars.charAt(i + 1); if (this._.field == null) { this._.field = ''; } this._.field += char; if (this.options.raw) { this._.rawBuf += char; } i++; continue; } } ...
...
Parser.prototype._transform = function(chunk, encoding, callback) {
var err, error;
if (chunk instanceof Buffer) {
chunk = this._.decoder.write(chunk);
}
try {
this.__write(chunk, false);
return callback();
} catch (error) {
err = error;
return this.emit('error', err);
}
};
...
_flush = function (callback) { var err, error; try { 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); } return callback(); } catch (error) { err = error; return this.emit('error', err); } }
...
return records.push(record);
}
};
parser.__write(data, false);
if (data instanceof Buffer) {
parser.__write(data.end(), true);
}
parser._flush((function() {}));
return records;
};
...
_transform = function (chunk, encoding, callback) { var err, error; if (chunk instanceof Buffer) { chunk = this._.decoder.write(chunk); } try { this.__write(chunk, false); return callback(); } catch (error) { err = error; return this.emit('error', err); } }
n/a