Comparing version 0.3.0 to 0.3.1
@@ -46,11 +46,19 @@ var fs = require("fs"), | ||
function defaultTransform(row) { | ||
return row; | ||
} | ||
function __write(writer, arr, options) { | ||
options = options || {}; | ||
var formatter = createFormatter(options), | ||
transformer = extended.has(options, "transform") ? options.transform : defaultTransform, | ||
hasHeaders = extended.has(options, "headers") ? options.headers : true, | ||
headersLength = 0, | ||
i = -1, | ||
j = -1, | ||
l = arr.length, | ||
ret = []; | ||
if (l) { | ||
var item = arr[0], isHash = !extended.isArray(item), headers; | ||
var item = arr[0], isHash = !extended.isArray(item), headers, vals; | ||
item = transformer(item); | ||
if (hasHeaders) { | ||
@@ -63,7 +71,21 @@ if (isHash) { | ||
} | ||
headersLength = headers.length; | ||
ret.push(formatter(headers)); | ||
} | ||
while (++i < l) { | ||
item = arr[i]; | ||
ret.push(formatter(isHash ? hash.values(item) : item)); | ||
item = transformer(arr[i]); | ||
vals = item; | ||
if (hasHeaders && isHash) { | ||
j = -1; | ||
vals = []; | ||
while (++j < headersLength) { | ||
vals[j] = item[headers[j]]; | ||
} | ||
} else if (isHash) { | ||
j = -1; | ||
while (++j < headersLength) { | ||
vals[j] = item[j]; | ||
} | ||
} | ||
ret.push(formatter(vals)); | ||
} | ||
@@ -74,26 +96,40 @@ writer.push(ret.join(LINE_BREAK)); | ||
function wrapWriter(writer, options) { | ||
var transformStreamWrite = Transform.prototype.write; | ||
function CsvTransformStream(options) { | ||
options = options || {}; | ||
var formatter = createFormatter(options), | ||
hasHeaders = extended.has(options, "headers") ? options.headers : true, | ||
parsedHeaders = hasHeaders ? false : true, | ||
headersLength = 0, i = -1, | ||
writerWrite = writer.write, headers, | ||
buffer = [], | ||
totalCount = 0, | ||
MAX_BUFFER_SIZE = options.maxBuffer || 100000; | ||
Transform.call(this, options); | ||
this.formatter = createFormatter(options); | ||
var hasHeaders = this.hasHeaders = extended.has(options, "headers") ? options.headers : true; | ||
this.parsedHeaders = hasHeaders ? false : true; | ||
this.buffer = []; | ||
this.maxBufferSize = options.maxBuffer || 100000; | ||
extended.has(options, "transform") && this.transform(options.transform); | ||
} | ||
writer.write = function (item) { | ||
util.inherits(CsvTransformStream, Transform); | ||
extended(CsvTransformStream).extend({ | ||
headers: null, | ||
headersLength: 0, | ||
totalCount: 0, | ||
write: function (item) { | ||
var buffer = this.buffer, headers = this.headers; | ||
if (item) { | ||
item = this.__transform(item); | ||
var isHash = !extended.isArray(item), vals; | ||
if (!parsedHeaders) { | ||
totalCount++; | ||
parsedHeaders = true; | ||
if (!this.parsedHeaders) { | ||
this.totalCount++; | ||
this.parsedHeaders = true; | ||
if (isHash) { | ||
headers = hash.keys(item); | ||
headers = this.headers = hash.keys(item); | ||
} else { | ||
headers = item; | ||
headers = this.headers = item; | ||
} | ||
headersLength = headers.length; | ||
buffer.push(formatter(headers)); | ||
this.headersLength = headers.length; | ||
buffer.push(this.formatter(headers)); | ||
if (!isHash) { | ||
@@ -103,7 +139,7 @@ return; | ||
} | ||
if (totalCount++) { | ||
if (this.totalCount++) { | ||
buffer.push("\n"); | ||
} | ||
if (isHash) { | ||
i = -1; | ||
var i = -1, headersLength = this.headersLength; | ||
vals = []; | ||
@@ -116,5 +152,5 @@ while (++i < headersLength) { | ||
} | ||
buffer.push(formatter(vals)); | ||
if (buffer.length >= MAX_BUFFER_SIZE) { | ||
writerWrite.call(writer, new Buffer(buffer.join("")).toString("utf8")); | ||
buffer.push(this.formatter(vals)); | ||
if (buffer.length >= this.maxBufferSize) { | ||
transformStreamWrite.call(this, new Buffer(buffer.join("")).toString("utf8")); | ||
buffer.length = 0; | ||
@@ -124,20 +160,19 @@ } | ||
if (buffer.length) { | ||
writerWrite.call(writer, new Buffer(buffer.join("")).toString("utf8")); | ||
transformStreamWrite.call(this, new Buffer(buffer.join("")).toString("utf8")); | ||
buffer.length = 0; | ||
} | ||
writer.end(); | ||
this.end(); | ||
} | ||
}; | ||
return writer; | ||
} | ||
}, | ||
function CsvTransformStream(opts) { | ||
Transform.call(this, opts); | ||
wrapWriter(this, opts); | ||
} | ||
__transform: defaultTransform, | ||
util.inherits(CsvTransformStream, Transform); | ||
transform: function (cb) { | ||
if (!extended.isFunction(cb)) { | ||
throw new TypeError("fast-csv.Parser#transform requires a function"); | ||
} | ||
this.__transform = cb; | ||
return this; | ||
}, | ||
extended(CsvTransformStream).extend({ | ||
_transform: function (str, encoding, cb) { | ||
@@ -177,4 +212,3 @@ cb(null, str); | ||
var stream = fs.createWriteStream(path, {encoding: "utf8"}); | ||
write(arr, options).pipe(stream); | ||
return stream; | ||
return write(arr, options).pipe(stream); | ||
} | ||
@@ -184,11 +218,6 @@ | ||
createFormatter.write = write; | ||
createFormatter.createWriteStream = createWriteStream; | ||
createFormatter.writeToString = writeToString; | ||
createFormatter.writeToPath = writeToPath; | ||
createFormatter.writeToStream = writeToStream; | ||
module.exports = createFormatter; |
@@ -34,3 +34,3 @@ var extended = require("./extended"), | ||
options = options || {}; | ||
options.objectMode = extended.has(options, "objectMode") ? options.objectMode : true | ||
options.objectMode = extended.has(options, "objectMode") ? options.objectMode : true; | ||
stream.Transform.call(this, options); | ||
@@ -37,0 +37,0 @@ this.lines = ""; |
{ | ||
"name": "fast-csv", | ||
"version": "0.3.0", | ||
"description": "CSV parser and writer", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "grunt" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:C2FO/fast-csv.git" | ||
}, | ||
"keywords": [ | ||
"csv", | ||
"parser", | ||
"fast", | ||
"writer", | ||
"csv writer", | ||
"CSV" | ||
], | ||
"homepage": "http://c2fo.github.com/fast-csv/index.html", | ||
"author": "Doug Martin", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"it": "~0.2.6", | ||
"grunt-it": "~0.3.1", | ||
"grunt": "~0.4.1", | ||
"grunt-contrib-jshint": "~0.10.0" | ||
}, | ||
"engines": { | ||
"node": ">=0.10" | ||
}, | ||
"dependencies": { | ||
"is-extended": "0.0.10", | ||
"object-extended": "0.0.7", | ||
"extended": "0.0.6", | ||
"string-extended": "0.0.8" | ||
} | ||
"name": "fast-csv", | ||
"version": "0.3.1", | ||
"description": "CSV parser and writer", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "grunt jshint it" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git@github.com:C2FO/fast-csv.git" | ||
}, | ||
"keywords": [ | ||
"csv", | ||
"parser", | ||
"fast", | ||
"writer", | ||
"csv writer", | ||
"CSV" | ||
], | ||
"homepage": "http://c2fo.github.com/fast-csv/index.html", | ||
"author": "Doug Martin", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"it": "~0.2.6", | ||
"grunt-it": "~0.3.1", | ||
"grunt": "~0.4.1", | ||
"grunt-contrib-jshint": "~0.10.0", | ||
"grunt-exec": "^0.4.5" | ||
}, | ||
"engines": { | ||
"node": ">=0.10" | ||
}, | ||
"dependencies": { | ||
"is-extended": "0.0.10", | ||
"object-extended": "0.0.7", | ||
"extended": "0.0.6", | ||
"string-extended": "0.0.8" | ||
} | ||
} |
136
README.md
@@ -34,5 +34,5 @@ [![build status](https://secure.travis-ci.org/C2FO/fast-csv.png)](http://travis-ci.org/C2FO/fast-csv) | ||
`record`: Emitted when a record is parsed. | ||
`data-invalid`: Emitted if there was invalid row encounted, **only emitted if the `validate` function is used**. | ||
`data`: Emitted with the `stringified` version of a record. | ||
* `record`: Emitted when a record is parsed. | ||
* `data-invalid`: Emitted if there was invalid row encounted, **only emitted if the `validate` function is used**. | ||
* `data`: Emitted with the object or `stringified` version if the `objectMode` is set to `false`. | ||
@@ -216,4 +216,6 @@ **([options])** | ||
Formatting accepts the same options as parsing. | ||
Formatting accepts the same options as parsing with an additional `transform` option. | ||
* `transform(row)`: A function that accepts a row and returns a transformed one to be written. | ||
**`createWriteStream(options)`** | ||
@@ -240,2 +242,28 @@ | ||
If you wish to transform rows as writing then you can use the `.transform` method. | ||
```javascript | ||
var csvStream = csv | ||
.createWriteStream({headers: true}) | ||
.transform(function(row){ | ||
return { | ||
A: row.a, | ||
B: row.b | ||
}; | ||
}), | ||
writableStream = fs.createWriteStream("my.csv"); | ||
writableStream.on("finish", function(){ | ||
console.log("DONE!"); | ||
}); | ||
csvSream.pipe(writableStream); | ||
csvStream.write({a: "a0", b: "b0"}); | ||
csvStream.write({a: "a1", b: "b1"}); | ||
csvStream.write({a: "a2", b: "b2"}); | ||
csvStream.write({a: "a3", b: "b4"}); | ||
csvStream.write({a: "a3", b: "b4"}); | ||
csvStream.write(null); | ||
``` | ||
**Writing Data** | ||
@@ -250,3 +278,3 @@ | ||
```javascript | ||
var ws = fs.createWritableStream("my.csv"); | ||
var ws = fs.createWriteStream("my.csv"); | ||
csv | ||
@@ -262,3 +290,3 @@ .write([ | ||
```javascript | ||
var ws = fs.createWritableStream("my.csv"); | ||
var ws = fs.createWriteStream("my.csv"); | ||
csv | ||
@@ -272,2 +300,22 @@ .write([ | ||
```javascript | ||
var ws = fs.createWriteStream("my.csv"); | ||
csv | ||
.write([ | ||
{a: "a1", b: "b1"}, | ||
{a: "a2", b: "b2"} | ||
], { | ||
headers: true | ||
transform: function(row){ | ||
return { | ||
A: row.a, | ||
B: row.b | ||
}; | ||
} | ||
}) | ||
.pipe(ws); | ||
``` | ||
**`writeToStream(stream,arr[, options])`** | ||
@@ -279,3 +327,3 @@ | ||
csv | ||
.writeToStream(fs.createWritableStream("my.csv"), [ | ||
.writeToStream(fs.createWriteStream("my.csv"), [ | ||
["a", "b"], | ||
@@ -289,3 +337,3 @@ ["a1", "b1"], | ||
csv | ||
.writeToStream(fs.createWritableStream("my.csv"), [ | ||
.writeToStream(fs.createWriteStream("my.csv"), [ | ||
{a: "a1", b: "b1"}, | ||
@@ -297,2 +345,19 @@ {a: "a2", b: "b2"} | ||
```javascript | ||
csv | ||
.writeToStream(fs.createWriteStream("my.csv"), [ | ||
{a: "a1", b: "b1"}, | ||
{a: "a2", b: "b2"} | ||
], { | ||
headers: true, | ||
transform: function(row){ | ||
return { | ||
A: row.a, | ||
B: row.b | ||
}; | ||
} | ||
}) | ||
.pipe(ws); | ||
``` | ||
**`writeToPath(arr[, options])`** | ||
@@ -325,2 +390,21 @@ | ||
```javascript | ||
csv | ||
.writeToStream("my.csv", [ | ||
{a: "a1", b: "b1"}, | ||
{a: "a2", b: "b2"} | ||
], { | ||
headers: true, | ||
transform: function(row){ | ||
return { | ||
A: row.a, | ||
B: row.b | ||
}; | ||
} | ||
}) | ||
.on("finish", function(){ | ||
console.log("done!"); | ||
}); | ||
``` | ||
**`writeToString(arr[, options])`** | ||
@@ -343,2 +427,17 @@ | ||
```javascript | ||
csv.writeToString([ | ||
{a: "a1", b: "b1"}, | ||
{a: "a2", b: "b2"} | ||
], { | ||
headers: true, | ||
transform: function(row){ | ||
return { | ||
A: row.a, | ||
B: row.b | ||
}; | ||
} | ||
}); //"a,b\na1,b1\na2,b2\n" | ||
``` | ||
## Piping from Parser to Writer | ||
@@ -375,2 +474,23 @@ | ||
If you want to tranform on the formatting side | ||
```javascript | ||
var formatStream = csv | ||
.createWriteStream({headers: true}) | ||
.transform(function(){ | ||
return { | ||
name: obj.Name, | ||
address: obj.Address, | ||
emailAddress: obj.Email_Address, | ||
verified: obj.Verified | ||
}; | ||
}); | ||
csv | ||
.fromPath("in.csv", {headers: true}) | ||
.pipe(formatStream) | ||
.pipe(fs.createWriteStream("out.csv", {encoding: "utf8"})); | ||
``` | ||
## Benchmarks | ||
@@ -377,0 +497,0 @@ |
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
103115
619
522
5