Comparing version 0.2.5 to 0.3.0
var fs = require("fs"), | ||
util = require("util"), | ||
extended = require("./extended"), | ||
@@ -6,2 +7,3 @@ isUndefinedOrNull = extended.isUndefinedOrNull, | ||
stream = require("stream"), | ||
Transform = stream.Transform, | ||
LINE_BREAK = extended.LINE_BREAK; | ||
@@ -77,3 +79,3 @@ | ||
headersLength = 0, i = -1, | ||
writerWrite = writer.push, headers, | ||
writerWrite = writer.write, headers, | ||
buffer = [], | ||
@@ -87,3 +89,3 @@ totalCount = 0, | ||
if (!parsedHeaders) { | ||
totalCount++ | ||
totalCount++; | ||
parsedHeaders = true; | ||
@@ -123,3 +125,3 @@ if (isHash) { | ||
} | ||
writerWrite.call(writer, null); | ||
writer.end(); | ||
} | ||
@@ -130,7 +132,22 @@ }; | ||
function CsvTransformStream(opts) { | ||
Transform.call(this, opts); | ||
wrapWriter(this, opts); | ||
} | ||
util.inherits(CsvTransformStream, Transform); | ||
extended(CsvTransformStream).extend({ | ||
_transform: function (str, encoding, cb) { | ||
cb(null, str); | ||
}, | ||
_flush: function (cb) { | ||
this.write(null); | ||
cb(null); | ||
} | ||
}); | ||
function createWriteStream(options) { | ||
var writer = new stream.Readable(); | ||
writer._read = function () { | ||
}; | ||
return wrapWriter(writer, options); | ||
return new CsvTransformStream(options); | ||
} | ||
@@ -137,0 +154,0 @@ |
var extended = require("./extended"), | ||
isUndefined = extended.isUndefined, | ||
EventEmitter = require("events").EventEmitter, | ||
util = require("util"), | ||
@@ -8,8 +7,30 @@ out = process.stdout, | ||
EMPTY = /^\s*(?:''|"")?\s*(?:,\s*(?:''|"")?\s*)*$/, | ||
VALUE = /([^,'"\s\\]*(?:\s+[^,'"\s\\]+)*)/, | ||
LINE_SPLIT = /[\r\n]+/, | ||
DEFAULT_DELIMITER = ",", | ||
createParser = require("./parser"); | ||
function spreadArgs(f, args, scope) { | ||
var ret; | ||
switch ((args || []).length) { | ||
case 0: | ||
ret = f.call(scope); | ||
break; | ||
case 1: | ||
ret = f.call(scope, args[0]); | ||
break; | ||
case 2: | ||
ret = f.call(scope, args[0], args[1]); | ||
break; | ||
case 3: | ||
ret = f.call(scope, args[0], args[1], args[2]); | ||
break; | ||
default: | ||
ret = f.apply(scope, args); | ||
} | ||
return ret; | ||
} | ||
function ParserStream(options) { | ||
options = options || {}; | ||
options.objectMode = extended.has(options, "objectMode") ? options.objectMode : true | ||
stream.Transform.call(this, options); | ||
@@ -20,3 +41,2 @@ this.lines = ""; | ||
this._emitData = false; | ||
options = options || {}; | ||
var delimiter; | ||
@@ -36,2 +56,3 @@ if (extended.has(options, "delimiter")) { | ||
this._ignoreEmpty = options.ignoreEmpty; | ||
this.__objectMode = options.objectMode; | ||
this.__buffered = []; | ||
@@ -45,6 +66,7 @@ return this; | ||
origPause = ParserStream.prototype.pause, | ||
origResume = ParserStream.prototype.resume; | ||
origResume = ParserStream.prototype.resume, | ||
origEmit = ParserStream.prototype.emit; | ||
function pause() { | ||
origPause.apply(this, arguments); | ||
spreadArgs(origPause, arguments, this); | ||
this.paused = true; | ||
@@ -55,3 +77,3 @@ this.pause = pause; | ||
function resume() { | ||
origResume.apply(this, arguments); | ||
spreadArgs(origResume, arguments, this); | ||
this.paused = false; | ||
@@ -68,2 +90,6 @@ if (this.__pausedDone) { | ||
__endEmitted: false, | ||
__emittedData: false, | ||
__handleLine: function __parseLineData(line, index, ignore) { | ||
@@ -138,3 +164,3 @@ var ignoreEmpty = this._ignoreEmpty; | ||
if (this._emitData) { | ||
this.push(JSON.stringify(dataRow)); | ||
this.push(this.__objectMode ? dataRow : JSON.stringify(dataRow)); | ||
} | ||
@@ -162,3 +188,3 @@ }, | ||
//increment row count so we aren't 0 based | ||
this.emit("end", ++this._rowCount); | ||
this.emit("end"); | ||
callback(); | ||
@@ -181,2 +207,13 @@ }, | ||
emit: function (event) { | ||
if (event === "end") { | ||
if (!this.__endEmitted) { | ||
this.__endEmitted = true; | ||
spreadArgs(origEmit, ["end", ++this._rowCount], this); | ||
} | ||
} else { | ||
spreadArgs(origEmit, arguments, this); | ||
} | ||
}, | ||
resume: function () { | ||
@@ -211,3 +248,3 @@ if (this.paused) { | ||
} | ||
origOn.apply(this, arguments); | ||
spreadArgs(origOn, arguments, this); | ||
return this; | ||
@@ -214,0 +251,0 @@ }, |
{ | ||
"name": "fast-csv", | ||
"version": "0.2.5", | ||
"version": "0.3.0", | ||
"description": "CSV parser and writer", | ||
@@ -28,13 +28,13 @@ "main": "index.js", | ||
"grunt": "~0.4.1", | ||
"grunt-contrib-jshint": "~0.4.3" | ||
"grunt-contrib-jshint": "~0.10.0" | ||
}, | ||
"dependencies": { | ||
"is-extended": "0.0.8", | ||
"object-extended": "0.0.5", | ||
"extended": "0.0.4", | ||
"string-extended": "0.0.7" | ||
}, | ||
"engines": { | ||
"node": ">=0.10" | ||
}, | ||
"dependencies": { | ||
"is-extended": "0.0.10", | ||
"object-extended": "0.0.7", | ||
"extended": "0.0.6", | ||
"string-extended": "0.0.8" | ||
} | ||
} |
@@ -1,5 +0,2 @@ | ||
<a name="top"></a> | ||
[![build status](https://secure.travis-ci.org/C2FO/fast-csv.png)](http://travis-ci.org/C2FO/fast-csv) | ||
[![build status](https://secure.travis-ci.org/C2FO/fast-csv.png)](http://travis-ci.org/C2FO/fast-csv) | ||
# Fast-csv | ||
@@ -21,2 +18,3 @@ | ||
* `objectMode=true`: Ensure that `data` events have an object emitted rather than the stringified version set to false to have a stringified buffer. | ||
* `headers=false`: Ste to true if you expect the first line of your `CSV` to contain headers, alternatly you can specify an array of headers to use. | ||
@@ -37,3 +35,2 @@ * `ignoreEmpty=false`: If you wish to ignore empty rows. | ||
`parse-error`: Emitted if there was an error parsing a row. | ||
`record`: Emitted when a record is parsed. | ||
@@ -61,3 +58,3 @@ `data-invalid`: Emitted if there was invalid row encounted, **only emitted if the `validate` function is used**. | ||
**`.fromPath(path[, options])** | ||
**`.fromPath(path[, options])`** | ||
@@ -79,3 +76,3 @@ This method parses a file from the specified path. | ||
**`.fromString(string[, options])** | ||
**`.fromString(string[, options])`** | ||
@@ -101,3 +98,3 @@ This method parses a string | ||
**`.fromStream(stream[, options])** | ||
**`.fromStream(stream[, options])`** | ||
@@ -231,3 +228,3 @@ This accepted a readable stream to parse data from. | ||
var csvStream = csv.createWriteStream({headers: true}), | ||
writableStream = fs.createWritableStream("my.csv"); | ||
writableStream = fs.createWriteStream("my.csv"); | ||
@@ -342,2 +339,33 @@ writableStream.on("finish", function(){ | ||
## Piping from Parser to Writer | ||
You can use `fast-csv` to pipe the output from a parsed CSV to a transformed CSV by setting the parser to `objectMode` and using `createWriteStream`. | ||
```javascript | ||
csv | ||
.fromPath("in.csv", {headers: true}) | ||
.pipe(csv.createWriteStream({headers: true})) | ||
.pipe(fs.createWriteStream("out.csv", {encoding: "utf8"})); | ||
``` | ||
When piping from a parser to a formatter the transforms are maintained also. | ||
```javascript | ||
csv | ||
.fromPath("in.csv", {headers: true}) | ||
.transform(function(obj){ | ||
return { | ||
name: obj.Name, | ||
address: obj.Address, | ||
emailAddress: obj.Email_Address, | ||
verified: obj.Verified | ||
}; | ||
}) | ||
.pipe(csv.createWriteStream({headers: true})) | ||
.pipe(fs.createWriteStream("out.csv", {encoding: "utf8"})); | ||
``` | ||
The output will contain formatted result from the transform function. | ||
## Benchmarks | ||
@@ -381,9 +409,1 @@ | ||
* Twitter: [http://twitter.com/c2fo](http://twitter.com/c2fo) - 877.465.4045 | ||
##Namespaces | ||
##Classes |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
100090
591
402
+ Addedextended@0.0.6(transitive)
+ Addedis-extended@0.0.10(transitive)
+ Addedobject-extended@0.0.7(transitive)
+ Addedstring-extended@0.0.8(transitive)
- Removedabbrev@1.1.1(transitive)
- Removedargparse@0.1.16(transitive)
- Removedarray-extended@0.0.4(transitive)
- Removedasync@0.1.22(transitive)
- Removedcoffee-script@1.3.3(transitive)
- Removedcolors@0.6.2(transitive)
- Removeddateformat@1.0.2-1.2.3(transitive)
- Removedesprima@1.0.4(transitive)
- Removedeventemitter2@0.4.14(transitive)
- Removedexit@0.1.2(transitive)
- Removedextended@0.0.4(transitive)
- Removedfindup-sync@0.1.3(transitive)
- Removedgetobject@0.1.0(transitive)
- Removedglob@3.1.213.2.11(transitive)
- Removedgraceful-fs@1.2.3(transitive)
- Removedgrunt@0.4.5(transitive)
- Removedgrunt-legacy-log@0.1.3(transitive)
- Removedgrunt-legacy-log-utils@0.1.1(transitive)
- Removedgrunt-legacy-util@0.2.0(transitive)
- Removedhooker@0.2.3(transitive)
- Removediconv-lite@0.2.11(transitive)
- Removedinherits@1.0.22.0.4(transitive)
- Removedis-extended@0.0.8(transitive)
- Removedjs-yaml@2.0.5(transitive)
- Removedlodash@0.9.22.4.2(transitive)
- Removedlru-cache@2.7.3(transitive)
- Removedminimatch@0.2.140.3.0(transitive)
- Removednopt@1.0.10(transitive)
- Removedobject-extended@0.0.5(transitive)
- Removedrimraf@2.2.8(transitive)
- Removedsigmund@1.0.1(transitive)
- Removedstring-extended@0.0.7(transitive)
- Removedunderscore@1.7.0(transitive)
- Removedunderscore.string@2.2.12.3.32.4.0(transitive)
- Removedwhich@1.0.9(transitive)
Updatedextended@0.0.6
Updatedis-extended@0.0.10
Updatedobject-extended@0.0.7
Updatedstring-extended@0.0.8