Comparing version 0.0.3 to 0.1.0
445
lib/index.js
/** | ||
* @projectName fast-csv | ||
* @github https://github.com/C2FO/fast-csv | ||
* @includeDoc [Test Coverage] [../docs-md/coverage.html] | ||
* @header | ||
* | ||
* [![build status](https://secure.travis-ci.org/C2FO/fast-csv.png)](http://travis-ci.org/C2FO/fast-csv) | ||
@@ -19,9 +17,46 @@ * # Fast-csv | ||
* | ||
* To parse a file. | ||
* ### Parsing | ||
* | ||
* All methods accept the following `options` | ||
* | ||
* * `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. | ||
* * `ignoreEmpty=false`: If you wish to ignore empty rows. | ||
* * `delimiter=','`: If your data uses an alternate delimiter such as `;` or `\t`. | ||
* * **NOTE** When specifying an alternate `delimiter` you may only pass in a single character delimeter | ||
* | ||
* **events** | ||
* | ||
* `parse-error`: Emitted if there was an error parsing a row. | ||
* `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. | ||
* | ||
* **([options])** | ||
* | ||
* If you use `fast-csv` as a function it returns a transform stream that can be piped into. | ||
* | ||
* ```javascript | ||
* var stream = fs.createReadStream("my.csv"); | ||
* | ||
* var csvStream = csv() | ||
* .on("record", function(data){ | ||
* console.log(data): | ||
* }) | ||
* .on("end", function(){ | ||
* console.log("done"); | ||
* }); | ||
* | ||
* stream.pipe(csvStream); | ||
* ``` | ||
* | ||
* **`.fromPath(path[, options])** | ||
* | ||
* This method parses a file from the specified path. | ||
* | ||
* ```javascript | ||
* var csv = require("fast-csv"); | ||
* | ||
* csv("my.csv") | ||
* .on("data", function(data){ | ||
* csv | ||
* .fromPath("my.csv") | ||
* .on("record", function(data){ | ||
* console.log(data): | ||
@@ -31,13 +66,36 @@ * }) | ||
* console.log("done"); | ||
* }); | ||
* ``` | ||
* | ||
* **`.fromString(string[, options])** | ||
* | ||
* This method parses a string | ||
* | ||
* ```javascript | ||
* var csv = require("fast-csv"); | ||
* | ||
* var CSV_STRING = 'a,b\n' + | ||
* 'a1,b1\n' + | ||
* 'a2,b2\n'; | ||
* | ||
* csv | ||
* .fromPath(CSV_STRING, {headers: true}) | ||
* .on("record", function(data){ | ||
* console.log(data): | ||
* }) | ||
* .parse(); | ||
* .on("end", function(){ | ||
* console.log("done"); | ||
* }); | ||
* ``` | ||
* | ||
* You may also parse a stream. | ||
* **`.fromStream(stream[, options])** | ||
* | ||
* This accepted a readable stream to parse data from. | ||
* | ||
* ```javascript | ||
* var stream = fs.createReadStream("my.csv"); | ||
* | ||
* csv(stream) | ||
* .on("data", function(data){ | ||
* csv() | ||
* .fromStream(stream) | ||
* .on("record", function(data){ | ||
* console.log(data): | ||
@@ -47,5 +105,3 @@ * }) | ||
* console.log("done"); | ||
* }) | ||
* .parse(); | ||
* | ||
* }); | ||
* ``` | ||
@@ -59,4 +115,5 @@ * | ||
* | ||
* csv(stream, {headers : true}) | ||
* .on("data", function(data){ | ||
* csv() | ||
* .fromStream(stream, {headers : true}) | ||
* .on("record", function(data){ | ||
* console.log(data): | ||
@@ -66,4 +123,3 @@ * }) | ||
* console.log("done"); | ||
* }) | ||
* .parse(); | ||
* }); | ||
* | ||
@@ -78,4 +134,5 @@ * ``` | ||
* | ||
* csv(stream, {headers : ["firstName", "lastName", "address"]}) | ||
* .on("data", function(data){ | ||
* csv | ||
* .fromStream(stream, {headers : ["firstName", "lastName", "address"]}) | ||
* .on("record", function(data){ | ||
* console.log(data): | ||
@@ -85,4 +142,3 @@ * }) | ||
* console.log("done"); | ||
* }) | ||
* .parse(); | ||
* }); | ||
* | ||
@@ -99,4 +155,5 @@ * ``` | ||
* | ||
* csv(stream, {ignoreEmpty: true}) | ||
* .on("data", function(data){ | ||
* csv | ||
* .fromStream(stream, {ignoreEmpty: true}) | ||
* .on("record", function(data){ | ||
* console.log(data): | ||
@@ -106,4 +163,3 @@ * }) | ||
* console.log("done"); | ||
* }) | ||
* .parse(); | ||
* }); | ||
* | ||
@@ -120,3 +176,4 @@ * ``` | ||
* | ||
* csv(stream, {headers : true}) | ||
* csv( | ||
* .fromStream(stream, {headers : true}) | ||
* .validate(function(data){ | ||
@@ -128,3 +185,3 @@ * return data.age < 50; //all persons must be under the age of 50 | ||
* }) | ||
* .on("data", function(data){ | ||
* .on("record", function(data){ | ||
* console.log(data): | ||
@@ -134,4 +191,3 @@ * }) | ||
* console.log("done"); | ||
* }) | ||
* .parse(); | ||
* }); | ||
* | ||
@@ -148,7 +204,8 @@ * ``` | ||
* | ||
* csv(stream) | ||
* csv | ||
* .fromStream(stream) | ||
* .transform(function(data){ | ||
* return data.reverse(); //reverse each row. | ||
* }) | ||
* .on("data", function(data){ | ||
* .on("record", function(data){ | ||
* console.log(data): | ||
@@ -158,8 +215,141 @@ * }) | ||
* console.log("done"); | ||
* }) | ||
* .parse(); | ||
* }); | ||
* | ||
* ``` | ||
* @footer | ||
* | ||
* ### Formatting | ||
* | ||
* `fast-csv` also allows to you to create create a `CSV` from data. | ||
* | ||
* In addition to the options for parsing you can specify the following additional options. | ||
* | ||
* * `quote='"'`: The character to use to escape values that contain a delimeter. | ||
* * `escape='"'`: The character to use when escaping a value that is `quoted` and constains a `quote` character. | ||
* * `i.e`: 'First,"Name"' => '"First,""name"""' | ||
* | ||
* **Writing Data** | ||
* | ||
* Each of the following methods accept an array of values to be written, however each value must be an `array` of `array`s or `object`s. | ||
* | ||
* **`write(arr[, options])`** | ||
* | ||
* Create a readable stream to read data from. | ||
* | ||
* ```javascript | ||
* var ws = fs.createWritableStream("my.csv"); | ||
* csv | ||
* .write([ | ||
* ["a", "b"], | ||
* ["a1", "b1"], | ||
* ["a2", "b2"] | ||
* ], {headers: true}) | ||
* .pipe(ws); | ||
* ``` | ||
* | ||
* ```javascript | ||
* var ws = fs.createWritableStream("my.csv"); | ||
* csv | ||
* .write([ | ||
* {a: "a1", b: "b1"}, | ||
* {a: "a2", b: "b2"} | ||
* ], {headers: true}) | ||
* .pipe(ws); | ||
* ``` | ||
* | ||
* **`writeToStream(stream,arr[, options])`** | ||
* | ||
* Write an array of values to a `WritableStream` | ||
* | ||
* ```javascript | ||
* csv | ||
* .writeToStream(fs.createWritableStream("my.csv"), [ | ||
* ["a", "b"], | ||
* ["a1", "b1"], | ||
* ["a2", "b2"] | ||
* ], {headers: true}); | ||
* ``` | ||
* | ||
* ```javascript | ||
* csv | ||
* .writeToStream(fs.createWritableStream("my.csv"), [ | ||
* {a: "a1", b: "b1"}, | ||
* {a: "a2", b: "b2"} | ||
* ], {headers: true}) | ||
* .pipe(ws); | ||
* ``` | ||
* | ||
* **`writeToPath(arr[, options])`** | ||
* | ||
* Write an array of values to the specified path | ||
* | ||
* ```javascript | ||
* csv | ||
* .writeToPath("my.csv", [ | ||
* ["a", "b"], | ||
* ["a1", "b1"], | ||
* ["a2", "b2"] | ||
* ], {headers: true}) | ||
* .on("finish", function(){ | ||
* console.log("done!"); | ||
* }); | ||
* ``` | ||
* | ||
* ```javascript | ||
* csv | ||
* .writeToStream("my.csv", [ | ||
* {a: "a1", b: "b1"}, | ||
* {a: "a2", b: "b2"} | ||
* ], {headers: true}) | ||
* .on("finish", function(){ | ||
* console.log("done!"); | ||
* }); | ||
* ``` | ||
* | ||
* **`writeToString(arr[, options])`** | ||
* | ||
* ```javascript | ||
* csv.writeToString([ | ||
* ["a", "b"], | ||
* ["a1", "b1"], | ||
* ["a2", "b2"] | ||
* ], {headers: true}); //"a,b\na1,b1\na2,b2\n" | ||
* ``` | ||
* | ||
* ```javascript | ||
* csv.writeToString([ | ||
* {a: "a1", b: "b1"}, | ||
* {a: "a2", b: "b2"} | ||
* ], {headers: true}); //"a,b\na1,b1\na2,b2\n" | ||
* ``` | ||
* | ||
* ## Benchmarks | ||
* | ||
* `Parsing 20000 records AVG over 3 runs` | ||
* | ||
* ``` | ||
* fast-csv: 198.67ms | ||
* csv: 525.33ms | ||
* ``` | ||
* | ||
* `Parsing 50000 records AVG over 3 runs` | ||
* | ||
* ``` | ||
* fast-csv: 441.33ms | ||
* csv: 1291ms | ||
* ``` | ||
* | ||
* `Parsing 100000 records AVG over 3 runs` | ||
* | ||
* ``` | ||
* fast-csv: 866ms | ||
* csv: 2773.33ms | ||
* ``` | ||
* | ||
* `Parsing 1000000 records AVG over 3 runs` | ||
* | ||
* ``` | ||
* fast-csv: 8562.67ms | ||
* csv: 30030.67ms | ||
* ``` | ||
* | ||
* ## License | ||
@@ -173,167 +363,44 @@ * | ||
* * Twitter: [http://twitter.com/c2fo](http://twitter.com/c2fo) - 877.465.4045 | ||
* | ||
*/ | ||
var fs = require("fs"), | ||
_ = require("extended")().register(require("is-extended")).register(require("object-extended")), | ||
EventEmitter = require("events").EventEmitter, | ||
util = require("util"), | ||
out = process.stdout, | ||
Stream = require("stream").Stream; | ||
extended = require("./extended"), | ||
hash = extended.hash, | ||
ParserStream = require("./parser_stream"), | ||
stream = require("stream"), | ||
formatter = require("./formatter"); | ||
function parse(options) { | ||
return new ParserStream(options); | ||
} | ||
var VALIDATE = /^\s*(?:'[^'\\]*(?:\\[\S\s][^'\\]*)*'|"[^"\\]*(?:\\[\S\s][^"\\]*)*"|[^,'"\s\\]*(?:\s+[^,'"\s\\]+)*)\s*(?:,\s*(?:'[^'\\]*(?:\\[\S\s][^'\\]*)*'|"[^"\\]*(?:\\[\S\s][^"\\]*)*"|[^,'"\s\\]*(?:\s+[^,'"\s\\]+)*)\s*)*$/; | ||
var EMPTY = /^\s*(?:''|"")?\s*(?:,\s*(?:''|"")?\s*)*$/; | ||
var VALUE = /(?!\s*$)\s*(?:'([^'\\]*(?:\\[\S\s][^'\\]*)*)'|"([^"\\]*(?:\\[\S\s][^"\\]*)*)"|([^,'"\s\\]*(?:\s+[^,'"\s\\]+)*))\s*(?:,|$)/g; | ||
var LINE_SPLIT = /[\r\n|\r|\n]/; | ||
parse.fromStream = function (stream, options) { | ||
return stream.pipe(new ParserStream(options)); | ||
}; | ||
function Parser(options) { | ||
EventEmitter.call(this); | ||
this._parsedHeaders = false; | ||
this._rowCount = 0; | ||
options = options || {}; | ||
this._headers = options.headers; | ||
this._ignoreEmpty = options.ignoreEmpty; | ||
} | ||
util.inherits(Parser, EventEmitter); | ||
parse.fromPath = function (location, options) { | ||
return fs.createReadStream(location).pipe(new ParserStream(options)); | ||
}; | ||
_(Parser).extend({ | ||
__parseLine: function __parseLineData(data, index, ignore) { | ||
// Return NULL if input string is not well formed CSV string. | ||
//regexp based on http://stackoverflow.com/a/8497474 | ||
var ignoreEmpty = this._ignoreEmpty; | ||
if (!VALIDATE.test(data)) { | ||
this.emit("error", new Error("Invalid row " + data)); | ||
return null; | ||
} else if (_.isBoolean(ignoreEmpty) && ignoreEmpty && EMPTY.test(data)) { | ||
return null; | ||
} | ||
var a = []; | ||
data.replace(VALUE, function lineReplace(m0, m1, m2, m3) { | ||
var item; | ||
if (m1 !== undefined) { | ||
item = m1.replace(/\\'/g, "'"); | ||
} else if (m2 !== undefined) { | ||
item = m2.replace(/\\"/g, '"'); | ||
} else if (m3 !== undefined) { | ||
item = m3; | ||
} | ||
a.push(item); | ||
return ''; // Return empty string. | ||
}.bind(this)); | ||
// Handle special case of empty last value. | ||
if (/,\s*$/.test(data)) { | ||
a.push(''); | ||
} | ||
if (!ignore) { | ||
a = this._transform(a, index); | ||
if (this._validate(a, index)) { | ||
return a; | ||
} else { | ||
this.emit("data-invalid", a, index); | ||
} | ||
} else { | ||
return a; | ||
} | ||
}, | ||
parse.fromString = function (string, options) { | ||
var rs = new stream.Readable(); | ||
rs.push(string); | ||
rs.push(null); | ||
return rs.pipe(new ParserStream(options)); | ||
}; | ||
_parse: function _parseLine(data) { | ||
var row, parseLine = this.__parseLine.bind(this), emitRow = this.emit.bind(this, "data"), count = 0; | ||
if (!this._parsedHeaders) { | ||
var headers = this._headers; | ||
if (_.isBoolean(headers) && headers) { | ||
headers = parseLine(data.shift(), 0, true); | ||
} | ||
if (_.isArray(headers)) { | ||
var headersLength = headers.length, | ||
orig = this._transform.bind(this); | ||
this._transform = function (data, index) { | ||
var ret = {}; | ||
for (var i = 0; i < headersLength; i++) { | ||
ret[headers[i]] = data[i]; | ||
} | ||
return orig(ret, index); | ||
}; | ||
} | ||
this._parsedHeaders = true; | ||
} | ||
for (var i = 0, l = data.length; i < l; i++) { | ||
row = data[i]; | ||
if (row) { | ||
var dataRow = parseLine(row, count); | ||
if (dataRow) { | ||
count = this._rowCount++; | ||
emitRow(dataRow, count); | ||
} | ||
} | ||
} | ||
}, | ||
from: function _from(from) { | ||
this.__from = from; | ||
return this; | ||
}, | ||
parse: function _parse(from) { | ||
from = from || this.__from; | ||
if (_.isString(from)) { | ||
from = fs.createReadStream(from); | ||
from.on("end", function () { | ||
from.destroy(); | ||
}); | ||
} | ||
if (_.isObject(from) && from instanceof Stream) { | ||
var lines = "", parse = this._parse.bind(this), end = this.emit.bind(this, "end"); | ||
from.on("data", function streamOnData(data) { | ||
var lineData = (lines + data).split(LINE_SPLIT); | ||
if (lineData.length > 1) { | ||
lines = lineData.pop(); | ||
parse(lineData); | ||
} else { | ||
lines += data; | ||
} | ||
}); | ||
from.on("end", function streamOnEnd() { | ||
parse(lines.split(LINE_SPLIT)); | ||
end(this._rowCount); | ||
}.bind(this)); | ||
} else { | ||
throw new TypeError("fast-csv.Parser#parse from must be a path or ReadableStream"); | ||
} | ||
return this; | ||
}, | ||
parse.toCsvStream = function (arr, options) { | ||
return formatter.writeToStream(arr, options); | ||
}; | ||
_validate: function (data, index) { | ||
return true; | ||
}, | ||
_transform: function (data, index) { | ||
return data; | ||
}, | ||
validate: function (cb) { | ||
if (!_.isFunction(cb)) { | ||
throw new TypeError("fast-csv.Parser#validate requires a function"); | ||
} | ||
this._validate = cb; | ||
return this; | ||
}, | ||
transform: function (cb) { | ||
if (!_.isFunction(cb)) { | ||
throw new TypeError("fast-csv.Parser#transform requires a function"); | ||
} | ||
this._transform = cb; | ||
return this; | ||
} | ||
}); | ||
parse.write = formatter.write; | ||
/** | ||
* Entry point to `fast-csv`. `fast-csv` does not store rows its proccesses each row and emits it. If you wish to save | ||
* every row into an array you must store them yourself by using the `data` event. Once all rows are done processing | ||
* the `end` event is emitted. | ||
* | ||
* Invoke to parse a csv file. | ||
* | ||
* @name fast-csv | ||
* @param location | ||
* @param options | ||
* @return {*} | ||
*/ | ||
module.exports = function parse(location, options) { | ||
return new Parser(options).from(location); | ||
}; | ||
parse.writeToStream = formatter.writeToStream; | ||
parse.writeToString = formatter.writeToString; | ||
parse.writeToPath = formatter.writeToPath; | ||
module.exports = parse; | ||
{ | ||
"name": "fast-csv", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"description": "CSV parser for node.js", | ||
@@ -30,4 +30,8 @@ "main": "index.js", | ||
"object-extended": "0.0.5", | ||
"extended": "0.0.4" | ||
"extended": "0.0.4", | ||
"string-extended": "0.0.7" | ||
}, | ||
"engines": { | ||
"node": ">=0.10" | ||
} | ||
} |
267
README.md
@@ -16,9 +16,46 @@ <a name="top"></a> | ||
To parse a file. | ||
### Parsing | ||
All methods accept the following `options` | ||
* `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. | ||
* `ignoreEmpty=false`: If you wish to ignore empty rows. | ||
* `delimiter=','`: If your data uses an alternate delimiter such as `;` or `\t`. | ||
* **NOTE** When specifying an alternate `delimiter` you may only pass in a single character delimeter | ||
**events** | ||
`parse-error`: Emitted if there was an error parsing a row. | ||
`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. | ||
**([options])** | ||
If you use `fast-csv` as a function it returns a transform stream that can be piped into. | ||
```javascript | ||
var stream = fs.createReadStream("my.csv"); | ||
var csvStream = csv() | ||
.on("record", function(data){ | ||
console.log(data): | ||
}) | ||
.on("end", function(){ | ||
console.log("done"); | ||
}); | ||
stream.pipe(csvStream); | ||
``` | ||
**`.fromPath(path[, options])** | ||
This method parses a file from the specified path. | ||
```javascript | ||
var csv = require("fast-csv"); | ||
csv("my.csv") | ||
.on("data", function(data){ | ||
csv | ||
.fromPath("my.csv") | ||
.on("record", function(data){ | ||
console.log(data): | ||
@@ -28,13 +65,36 @@ }) | ||
console.log("done"); | ||
}); | ||
``` | ||
**`.fromString(string[, options])** | ||
This method parses a string | ||
```javascript | ||
var csv = require("fast-csv"); | ||
var CSV_STRING = 'a,b\n' + | ||
'a1,b1\n' + | ||
'a2,b2\n'; | ||
csv | ||
.fromPath(CSV_STRING, {headers: true}) | ||
.on("record", function(data){ | ||
console.log(data): | ||
}) | ||
.parse(); | ||
.on("end", function(){ | ||
console.log("done"); | ||
}); | ||
``` | ||
You may also parse a stream. | ||
**`.fromStream(stream[, options])** | ||
This accepted a readable stream to parse data from. | ||
```javascript | ||
var stream = fs.createReadStream("my.csv"); | ||
csv(stream) | ||
.on("data", function(data){ | ||
csv() | ||
.fromStream(stream) | ||
.on("record", function(data){ | ||
console.log(data): | ||
@@ -44,5 +104,3 @@ }) | ||
console.log("done"); | ||
}) | ||
.parse(); | ||
}); | ||
``` | ||
@@ -56,4 +114,5 @@ | ||
csv(stream, {headers : true}) | ||
.on("data", function(data){ | ||
csv() | ||
.fromStream(stream, {headers : true}) | ||
.on("record", function(data){ | ||
console.log(data): | ||
@@ -63,4 +122,3 @@ }) | ||
console.log("done"); | ||
}) | ||
.parse(); | ||
}); | ||
@@ -75,4 +133,5 @@ ``` | ||
csv(stream, {headers : ["firstName", "lastName", "address"]}) | ||
.on("data", function(data){ | ||
csv | ||
.fromStream(stream, {headers : ["firstName", "lastName", "address"]}) | ||
.on("record", function(data){ | ||
console.log(data): | ||
@@ -82,4 +141,3 @@ }) | ||
console.log("done"); | ||
}) | ||
.parse(); | ||
}); | ||
@@ -96,4 +154,5 @@ ``` | ||
csv(stream, {ignoreEmpty: true}) | ||
.on("data", function(data){ | ||
csv | ||
.fromStream(stream, {ignoreEmpty: true}) | ||
.on("record", function(data){ | ||
console.log(data): | ||
@@ -103,4 +162,3 @@ }) | ||
console.log("done"); | ||
}) | ||
.parse(); | ||
}); | ||
@@ -117,3 +175,4 @@ ``` | ||
csv(stream, {headers : true}) | ||
csv( | ||
.fromStream(stream, {headers : true}) | ||
.validate(function(data){ | ||
@@ -125,3 +184,3 @@ return data.age < 50; //all persons must be under the age of 50 | ||
}) | ||
.on("data", function(data){ | ||
.on("record", function(data){ | ||
console.log(data): | ||
@@ -131,4 +190,3 @@ }) | ||
console.log("done"); | ||
}) | ||
.parse(); | ||
}); | ||
@@ -145,7 +203,8 @@ ``` | ||
csv(stream) | ||
csv | ||
.fromStream(stream) | ||
.transform(function(data){ | ||
return data.reverse(); //reverse each row. | ||
}) | ||
.on("data", function(data){ | ||
.on("record", function(data){ | ||
console.log(data): | ||
@@ -155,7 +214,141 @@ }) | ||
console.log("done"); | ||
}) | ||
.parse(); | ||
}); | ||
``` | ||
### Formatting | ||
`fast-csv` also allows to you to create create a `CSV` from data. | ||
In addition to the options for parsing you can specify the following additional options. | ||
* `quote='"'`: The character to use to escape values that contain a delimeter. | ||
* `escape='"'`: The character to use when escaping a value that is `quoted` and constains a `quote` character. | ||
* `i.e`: 'First,"Name"' => '"First,""name"""' | ||
**Writing Data** | ||
Each of the following methods accept an array of values to be written, however each value must be an `array` of `array`s or `object`s. | ||
**`write(arr[, options])`** | ||
Create a readable stream to read data from. | ||
```javascript | ||
var ws = fs.createWritableStream("my.csv"); | ||
csv | ||
.write([ | ||
["a", "b"], | ||
["a1", "b1"], | ||
["a2", "b2"] | ||
], {headers: true}) | ||
.pipe(ws); | ||
``` | ||
```javascript | ||
var ws = fs.createWritableStream("my.csv"); | ||
csv | ||
.write([ | ||
{a: "a1", b: "b1"}, | ||
{a: "a2", b: "b2"} | ||
], {headers: true}) | ||
.pipe(ws); | ||
``` | ||
**`writeToStream(stream,arr[, options])`** | ||
Write an array of values to a `WritableStream` | ||
```javascript | ||
csv | ||
.writeToStream(fs.createWritableStream("my.csv"), [ | ||
["a", "b"], | ||
["a1", "b1"], | ||
["a2", "b2"] | ||
], {headers: true}); | ||
``` | ||
```javascript | ||
csv | ||
.writeToStream(fs.createWritableStream("my.csv"), [ | ||
{a: "a1", b: "b1"}, | ||
{a: "a2", b: "b2"} | ||
], {headers: true}) | ||
.pipe(ws); | ||
``` | ||
**`writeToPath(arr[, options])`** | ||
Write an array of values to the specified path | ||
```javascript | ||
csv | ||
.writeToPath("my.csv", [ | ||
["a", "b"], | ||
["a1", "b1"], | ||
["a2", "b2"] | ||
], {headers: true}) | ||
.on("finish", function(){ | ||
console.log("done!"); | ||
}); | ||
``` | ||
```javascript | ||
csv | ||
.writeToStream("my.csv", [ | ||
{a: "a1", b: "b1"}, | ||
{a: "a2", b: "b2"} | ||
], {headers: true}) | ||
.on("finish", function(){ | ||
console.log("done!"); | ||
}); | ||
``` | ||
**`writeToString(arr[, options])`** | ||
```javascript | ||
csv.writeToString([ | ||
["a", "b"], | ||
["a1", "b1"], | ||
["a2", "b2"] | ||
], {headers: true}); //"a,b\na1,b1\na2,b2\n" | ||
``` | ||
```javascript | ||
csv.writeToString([ | ||
{a: "a1", b: "b1"}, | ||
{a: "a2", b: "b2"} | ||
], {headers: true}); //"a,b\na1,b1\na2,b2\n" | ||
``` | ||
## Benchmarks | ||
`Parsing 20000 records AVG over 3 runs` | ||
``` | ||
fast-csv: 198.67ms | ||
csv: 525.33ms | ||
``` | ||
`Parsing 50000 records AVG over 3 runs` | ||
``` | ||
fast-csv: 441.33ms | ||
csv: 1291ms | ||
``` | ||
`Parsing 100000 records AVG over 3 runs` | ||
``` | ||
fast-csv: 866ms | ||
csv: 2773.33ms | ||
``` | ||
`Parsing 1000000 records AVG over 3 runs` | ||
``` | ||
fast-csv: 8562.67ms | ||
csv: 30030.67ms | ||
``` | ||
## License | ||
@@ -170,3 +363,17 @@ | ||
##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
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
59470331
36
854
365
4
2
1
+ Addedstring-extended@0.0.7
+ Addedarguments-extended@0.0.3(transitive)
+ Addedarray-extended@0.0.11(transitive)
+ Addeddate-extended@0.0.6(transitive)
+ Addedstring-extended@0.0.7(transitive)