csv-parse
Advanced tools
Comparing version 0.0.2 to 0.0.3
133
lib/index.js
// Generated by CoffeeScript 1.7.1 | ||
var Parser, stream, util; | ||
module.exports = function() { | ||
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) { | ||
if (typeof arguments[0] === 'string') { | ||
data = arguments[0]; | ||
} else { | ||
options = 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 ? {} : []; | ||
if (data) { | ||
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; | ||
}; | ||
stream = require('stream'); | ||
@@ -8,23 +62,2 @@ | ||
/* | ||
`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 as an array, a user defined callback accepting the first line and returning the column names or true if autodiscovered in the first CSV line, default to null, affect the result data set in the sense that records will be objects instead of arrays. | ||
* `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. | ||
* `auto_parse` If true, the parser will attempt to convert read data types to native types | ||
*/ | ||
Parser = function(options) { | ||
@@ -87,2 +120,4 @@ var _base, _base1, _base10, _base2, _base3, _base4, _base5, _base6, _base7, _base8, _base9; | ||
module.exports.Parser = Parser; | ||
Parser.prototype._transform = function(chunk, encoding, callback) { | ||
@@ -271,57 +306,1 @@ var err; | ||
}; | ||
/* | ||
`parse([options])` | ||
`parse(data, [options], callback)` | ||
*/ | ||
module.exports = function() { | ||
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.2", | ||
"version": "0.0.3", | ||
"name": "csv-parse", | ||
@@ -4,0 +4,0 @@ "description": "CSV parsing implementing the Node.js `stream.Transform` API", |
[![Build Status](https://secure.travis-ci.org/wdavidw/node-csv-parse.png)](http://travis-ci.org/wdavidw/node-csv-parse) | ||
This project is part of the [CSV module](https://github.com/wdavidw/node-csv) | ||
and is a parser converting input CSV text into arrays or objects and | ||
implementing the Node.js `stream.Transform` API. It is also providing a simple | ||
callback-base API for converniency. It is both extremely easy to use and | ||
powerfull. It was released since 2010 and is tested against very large dataset | ||
by a large community. | ||
Part of the [CSV module](https://github.com/wdavidw/node-csv), this project is a | ||
parser converting CSV text input into arrays or objects. It implements the | ||
Node.js `stream.Transform` API. It also provides a simple callback-base API for | ||
converniency. It is both extremely easy to use and powerfull. It was first | ||
released in 2010 and is used against big datasets by a large community. | ||
@@ -15,3 +14,3 @@ [The full documentation of the CSV parser is available here](http://www.adaltas.com/projects/node-csv/). | ||
This module is to be considered in alpha stage. It is part of an ongoing effort | ||
This module is to be considered in beta stage. It is part of an ongoing effort | ||
to split the current CSV module into complementary modules with a cleaner design | ||
@@ -30,2 +29,5 @@ and the latest stream implementation. However, the code has been imported with | ||
For examples, refer to [the "samples" folder][csv-samples], | ||
the documentation or [the "test" folder][csv-test]. | ||
### Using the callback API | ||
@@ -138,3 +140,5 @@ | ||
[csv]: https://github.com/wdavidw/node-csv | ||
[csv-samples]: https://github.com/wdavidw/node-csv-parse/tree/master/samples | ||
[csv-test]: https://github.com/wdavidw/node-csv-parse/tree/master/test | ||
[travis]: https://travis-ci.org/#!/wdavidw/node-csv-parse | ||
var fs = require('fs'); | ||
var parse = require('../lib'); | ||
var transform = require('../../csv-transform'); | ||
var transform = require('../../stream-transform'); | ||
@@ -6,0 +6,0 @@ output = []; |
Sorry, the diff of this file is not supported yet
52037
24
142
344
2