csv-parse
Advanced tools
Comparing version 0.0.0 to 0.0.1
174
lib/index.js
@@ -1,2 +0,2 @@ | ||
// Generated by CoffeeScript 1.6.3 | ||
// Generated by CoffeeScript 1.7.1 | ||
var Parser, stream, util; | ||
@@ -8,4 +8,24 @@ | ||
/* | ||
`Parser([options])` | ||
------------------- | ||
Options may include: | ||
* `delimiter` Set the field delimiter. One character only, defaults to comma. | ||
* `rowDelimiter` String used to delimit record rows or a special value; special values are 'auto', 'unix', 'mac', 'windows', 'unicode'; defaults to 'auto' (discovered in source or 'unix' if no source is specified). | ||
* `quote` Optionnal character surrounding a field, one character only, defaults to double quotes. | ||
* `escape` Set the escape character, one character only, defaults to double quotes. | ||
* `columns` List of fields or true if autodiscovered in the first CSV line, default to null. Impact the `transform` argument and the `data` event by providing an object instead of an array, order matters, see the transform and the columns sections for more details. | ||
* `comment` Treat all the characteres after this one as a comment, default to '#' | ||
* `objname` Name of header-record title to name objects by. | ||
* `trim` If true, ignore whitespace immediately around the delimiter, defaults to false. | ||
* `ltrim` If true, ignore whitespace immediately following the delimiter (i.e. left-trim all fields), defaults to false. | ||
* `rtrim` If true, ignore whitespace immediately preceding the delimiter (i.e. right-trim all fields), defaults to false. | ||
*/ | ||
Parser = function(options) { | ||
var _base, _base1, _base10, _base2, _base3, _base4, _base5, _base6, _base7, _base8, _base9; | ||
var _base, _base1, _base2, _base3, _base4, _base5, _base6, _base7, _base8, _base9; | ||
if (options == null) { | ||
@@ -33,19 +53,16 @@ options = {}; | ||
if ((_base5 = this.options).comment == null) { | ||
_base5.comment = '#'; | ||
_base5.comment = ''; | ||
} | ||
if ((_base6 = this.options).flags == null) { | ||
_base6.flags = 'r'; | ||
if ((_base6 = this.options).objname == null) { | ||
_base6.objname = false; | ||
} | ||
if ((_base7 = this.options).encoding == null) { | ||
_base7.encoding = 'utf8'; | ||
if ((_base7 = this.options).trim == null) { | ||
_base7.trim = false; | ||
} | ||
if ((_base8 = this.options).trim == null) { | ||
_base8.trim = false; | ||
if ((_base8 = this.options).ltrim == null) { | ||
_base8.ltrim = false; | ||
} | ||
if ((_base9 = this.options).ltrim == null) { | ||
_base9.ltrim = false; | ||
if ((_base9 = this.options).rtrim == null) { | ||
_base9.rtrim = false; | ||
} | ||
if ((_base10 = this.options).rtrim == null) { | ||
_base10.rtrim = false; | ||
} | ||
this.lines = 0; | ||
@@ -67,28 +84,63 @@ this.buf = ''; | ||
Parser.prototype._transform = function(chunk, encoding, callback) { | ||
var err; | ||
if (chunk instanceof Buffer) { | ||
chunk = chunk.toString(); | ||
} | ||
this.__write(chunk); | ||
return callback(); | ||
try { | ||
this.__write(chunk, false); | ||
return callback(); | ||
} catch (_error) { | ||
err = _error; | ||
return this.emit('error', err); | ||
} | ||
}; | ||
Parser.prototype._flush = function(callback) { | ||
this.__write('', true); | ||
if (this.quoting) { | ||
return this.error(new Error("Quoted field not terminated at line " + (this.lines + 1))); | ||
var err; | ||
try { | ||
this.__write('', true); | ||
if (this.quoting) { | ||
this.emit('error', new Error("Quoted field not terminated at line " + (this.lines + 1))); | ||
return; | ||
} | ||
if (this.field || this.lastC === this.options.delimiter || this.lastC === this.options.quote) { | ||
if (this.options.trim || this.options.rtrim) { | ||
this.field = this.field.trimRight(); | ||
} | ||
this.line.push(this.field); | ||
this.field = ''; | ||
} | ||
if (this.line.length > 0) { | ||
this.__push(this.line); | ||
} | ||
return callback(); | ||
} catch (_error) { | ||
err = _error; | ||
return this.emit('error', err); | ||
} | ||
if (this.field || this.lastC === this.options.delimiter || this.lastC === this.options.quote) { | ||
if (this.options.trim || this.options.rtrim) { | ||
this.field = this.field.trimRight(); | ||
}; | ||
Parser.prototype.__push = function(line) { | ||
var field, i, lineAsColumns, _i, _len; | ||
if (this.options.columns === true) { | ||
this.options.columns = line; | ||
return; | ||
} | ||
if (this.options.columns != null) { | ||
lineAsColumns = {}; | ||
for (i = _i = 0, _len = line.length; _i < _len; i = ++_i) { | ||
field = line[i]; | ||
lineAsColumns[this.options.columns[i]] = field; | ||
} | ||
this.line.push(this.field); | ||
this.field = ''; | ||
if (this.options.objname) { | ||
return this.push([lineAsColumns[this.options.objname], lineAsColumns]); | ||
} else { | ||
return this.push(lineAsColumns); | ||
} | ||
} else { | ||
return this.push(line); | ||
} | ||
if (this.line.length > 0) { | ||
this.push(this.line); | ||
} | ||
return callback(); | ||
}; | ||
Parser.prototype.__write = function(chars, end) { | ||
Parser.prototype.__write = function(chars, end, callback) { | ||
var areNextCharsRowDelimiters, char, delimLength, escapeIsQuote, i, isDelimiter, isEscape, isQuote, isRowDelimiter, l, ltrim, nextNextCharPas, nextNextCharPos, rowDelimiter, rtrim, _results; | ||
@@ -101,3 +153,3 @@ ltrim = this.options.trim || this.options.ltrim; | ||
i = 0; | ||
if (this.lines === 0 && this.options.encoding === 'utf8' && 0xFEFF === chars.charCodeAt(0)) { | ||
if (this.lines === 0 && 0xFEFF === chars.charCodeAt(0)) { | ||
i++; | ||
@@ -116,3 +168,3 @@ } | ||
if (this.options.rowDelimiter == null) { | ||
if ((this.line.length === 0 && this.field === '') && (char === '\n' || char === '\r')) { | ||
if ((this.field === '') && (char === '\n' || char === '\r')) { | ||
rowDelimiter = char; | ||
@@ -148,4 +200,4 @@ nextNextCharPos = i + 1; | ||
areNextCharsRowDelimiters = this.options.rowDelimiter && chars.substr(i + 1, this.options.rowDelimiter.length) === this.options.rowDelimiter; | ||
if (this.nextChar && !areNextCharsRowDelimiters && this.nextChar !== this.options.delimiter && this.nextChar !== this.options.comment) { | ||
return this.error(new Error("Invalid closing quote at line " + (this.lines + 1) + "; found " + (JSON.stringify(this.nextChar)) + " instead of delimiter " + (JSON.stringify(this.options.delimiter)))); | ||
if (!this.options.relax && this.nextChar && !areNextCharsRowDelimiters && this.nextChar !== this.options.delimiter && this.nextChar !== this.options.comment) { | ||
throw new Error("Invalid closing quote at line " + (this.lines + 1) + "; found " + (JSON.stringify(this.nextChar)) + " instead of delimiter " + (JSON.stringify(this.options.delimiter))); | ||
} | ||
@@ -160,2 +212,4 @@ this.quoting = false; | ||
continue; | ||
} else if (this.field && !this.options.relax) { | ||
throw new Error("Invalid opening quote at line " + (this.lines + 1)); | ||
} | ||
@@ -187,3 +241,3 @@ } | ||
if (isRowDelimiter) { | ||
this.push(this.line); | ||
this.__push(this.line); | ||
this.line = []; | ||
@@ -212,6 +266,56 @@ i += this.options.rowDelimiter.length; | ||
/* | ||
`parse([options])` | ||
`parse(data, [options], callback)` | ||
*/ | ||
module.exports = function() { | ||
return new Parser(); | ||
var callback, called, chunks, data, options, parser; | ||
if (arguments.length === 3) { | ||
data = arguments[0]; | ||
options = arguments[1]; | ||
callback = arguments[2]; | ||
} else if (arguments.length === 2) { | ||
data = arguments[0]; | ||
callback = arguments[1]; | ||
} else if (arguments.length === 1) { | ||
options = arguments[0]; | ||
} | ||
if (options == null) { | ||
options = {}; | ||
} | ||
parser = new Parser(options); | ||
if (data && callback) { | ||
called = false; | ||
chunks = options.objname ? {} : []; | ||
process.nextTick(function() { | ||
parser.write(data); | ||
return parser.end(); | ||
}); | ||
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('finish', function() { | ||
if (!called) { | ||
return callback(null, chunks); | ||
} | ||
}); | ||
} | ||
return parser; | ||
}; | ||
module.exports.Parser = Parser; |
{ | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"name": "csv-parse", | ||
@@ -9,3 +9,5 @@ "description": "CSV parsing implementing the Node.js `stream.Transform` API", | ||
}, | ||
"dependencies": {}, | ||
"dependencies": { | ||
"csv-generate": "latest" | ||
}, | ||
"devDependencies": { | ||
@@ -17,3 +19,6 @@ "coffee-script": "latest", | ||
"optionalDependencies": {}, | ||
"main": "./lib" | ||
"main": "./lib", | ||
"scripts": { | ||
"test": "make test" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
45017
19
334
0
138
1
1
+ Addedcsv-generate@latest
+ Addedcsv-generate@4.4.1(transitive)