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

csv-stream

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

csv-stream - npm Package Compare versions

Comparing version 0.1.3 to 0.2.0

11

examples/fs.js

@@ -13,11 +13,6 @@

}
var start = new Date();
var totalBytes = 0;
fs.createReadStream('/home/lbdremy/Downloads/productBrands.csv')
.on('data',function(data){
totalBytes += data.length;
})
.pipe(csv.createStream(options))
.on('data',function(data){
//console.log(data);
console.log(data);
})

@@ -32,6 +27,2 @@ .on('column',function(key,value){

console.log('close!');
var end = new Date();
var timeSpent = end - start;
console.log(timeSpent + ' ms');
console.log(totalBytes/timeSpent + ' bytes/ms');
})

@@ -12,14 +12,9 @@ /**

}
var numFound = 0;
request('http://www.zuneta.com/google_base_feed.txt')
request('http://www.zuneta.com/feeds/productBrands.txt')
.pipe(csv.createStream(options))
.on('data',function(data){
console.log(data);
numFound++;
})
.on('column',function(key,value){
//console.log('#' + key + '=' + value);
})
.on('end',function(){
console.log(numFound + ' number of products found.');
})

11

index.js

@@ -34,3 +34,3 @@ /*!

this._buffer = new Buffer(0);
this._encoding = '';
this._encoding = undefined; // Encoding needs to be undefined for Buffer.toString method

@@ -46,2 +46,5 @@ // CSV parser

});
this._parser.on('header',function(header){
self.emit('header',header);
});
this._parser.on('end',function(){

@@ -75,3 +78,3 @@ self._ended = true;

}
}
}
}

@@ -91,5 +94,5 @@

this._paused = false;
if(this._buffer.length > 0 && !this._endCallWhenPause) this.write();
if(this._buffer && this._buffer.length > 0 && !this._endCallWhenPause) this.write();
if(this._endCallWhenPause) this.end();
this.emit('drain');
}
}

@@ -23,2 +23,3 @@ /*!

this.escapeChar = options ? options.escapeChar || '' : '';
this.columnOffset = options ? options.columnOffset || 0 : 0;

@@ -62,3 +63,5 @@ this._defaultColumns = options ? !!options.columns : false;

}else{
if(this._index === 0 && !this._defaultColumns){
if(this._index < this.columnOffset){
//skip line
}else if(this._index === this.columnOffset && !this._defaultColumns){
this.columns[this._currentColumn] = this._text;

@@ -77,4 +80,7 @@ }else{

if(this._text[this._text.length -1] === '\r') this._text = this._text.slice(0,this._text.length - 1);
if(this._index === 0 && !this._defaultColumns){
if(this._index < this.columnOffset){
//skip line
}else if(this._index === this.columnOffset && !this._defaultColumns){
this.columns[this._currentColumn] = this._text;
this.emit('header',this.columns);
}else{

@@ -94,4 +100,4 @@ this.emit('column',this.columns[this._currentColumn],this._text);

}
}
}
}
{
"name": "csv-stream",
"version": "0.1.3",
"version": "0.2.0",
"description": "Simple CSV stream",
"main": "index.js",
"devDependencies" : {
"mocha" : "1.4.x",
"request" : "2.10.x",
"stream-spec" : "0.3.x",
"stream-tester" : "0.0.x",
"chai" : "1.2.x"
"devDependencies": {
"mocha": "1.4.x",
"request": "2.10.x",
"stream-spec": "0.3.x",
"stream-tester": "0.0.x",
"chai": "1.2.x"
},

@@ -13,0 +13,0 @@ "scripts": {

@@ -21,3 +21,4 @@ # csv-stream - Simple CSV stream for node.js

endLine : '\n', // default is \n,
columns : ['columnName1', 'columnName2'] // by default read the first line and use values found as columns
columns : ['columnName1', 'columnName2'], // by default read the first line and use values found as columns
columnOffset : 2, // default is 0
escapeChar : '"', // default is an empty string

@@ -32,2 +33,5 @@ enclosedChar : '"' // default is an empty string

})
.on('header', function(columns) {
console.log(columns);
})
.on('data',function(data){

@@ -39,3 +43,3 @@ // outputs an object containing a set of key/value pair representing a line found in the csv file.

// outputs the column name associated with the value found
console.log('#' + key ' = ' + value);
console.log('#' + key + ' = ' + value);
})

@@ -55,2 +59,2 @@ ```

## Licence
(The MIT License) Copyright 2012 HipSnip Limited
(The MIT License) Copyright 2012 HipSnip Limited

@@ -112,3 +112,3 @@ /**

});
it('should emit `data` events with the right data with the specified colmuns',function(done){
it('should emit `data` events with the right data with the specified columns',function(done){
var parser = new Parser({ columns : ['cID','cTitle','cDescription']});

@@ -201,3 +201,3 @@ var length = 0;

// like the delimiter or the endLine character are not considered as specials
// if there are between enclosed chars.
// if there are between enclosed chars.
var parser = new Parser({

@@ -227,3 +227,3 @@ enclosedChar : '"',

// like the delimiter or the endLine character are not considered as specials
// if there are between enclosed chars.
// if there are between enclosed chars.
var parser = new Parser({

@@ -247,5 +247,91 @@ enclosedChar : '"',

});
it('should emit `header` event',function(done){
var parser = new Parser();
var countHeaderEvents = 0;
parser.on('header',function(data){
countHeaderEvents++;
assert.isArray(data);
});
parser.on('end',function(){
assert.equal(countHeaderEvents, 1);
done();
});
parser.parse(csvText);
parser.end();
});
it('should emit `header` event with the right data',function(done){
var parser = new Parser();
var countHeaderEvents = 0;
parser.on('header',function(data){
countHeaderEvents++;
assert.isArray(data);
assert.equal(data[0],'id');
assert.equal(data[1],'title');
assert.equal(data[2],'description');
});
parser.on('end',function(){
assert.equal(countHeaderEvents, 1);
done();
});
parser.parse(csvText);
parser.end();
});
it('should not emit `header` event when there is any specified column',function(done){
var parser = new Parser({ columns : ['cID','cTitle','cDescription']});
var countHeaderEvents = 0;
parser.on('header',function(data){
countHeaderEvents++;
assert.isArray(data);
});
parser.on('end',function(){
assert.equal(countHeaderEvents, 0);
done();
});
parser.parse(csvText);
parser.end();
});
describe('with offset column names',function(done){
var offsetCsvText = 'ElaborateTableTitle\n'
+ '\n'
+ 'id,title,description\n'
+ '1,title1,description1\n'
+ '2,title2,description2\n';
it('should emit `data` events with the right data',function(done){
var parser = new Parser({ columnOffset: 2 });
var length = 0;
parser.on('data',function(data){
assert.isObject(data);
if(length === 0) assert.deepEqual(data, {id : '1', title : 'title1', description : 'description1'});
if(length === 1) assert.deepEqual(data, {id : '2', title : 'title2', description : 'description2'});
length++;
});
parser.on('end',function(){
assert.equal(length,2);
done();
});
parser.parse(offsetCsvText);
parser.end();
});
it('should emit `column` events with the right data',function(done){
var parser = new Parser({ columnOffset: 2 });
var count = 0;
parser.on('column',function(key,value){
var pair = key + '=' + value;
if(count === 0) assert.equal(pair,'id=1');
if(count === 1) assert.equal(pair,'title=title1');
if(count === 2) assert.equal(pair,'description=description1');
if(count === 3) assert.equal(pair,'id=2');
if(count === 4) assert.equal(pair,'title=title2');
if(count === 5) assert.equal(pair,'description=description2');
count++;
});
parser.on('end',function(){
assert.equal(count,6);
done();
});
parser.parse(offsetCsvText);
parser.end();
});
});
});
})

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