Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fast-csv

Package Overview
Dependencies
Maintainers
1
Versions
74
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fast-csv - npm Package Compare versions

Comparing version 0.2.5 to 0.3.0

31

lib/formatter.js
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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc