Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Remove trailing line breaks

  • Loading branch information...
commit 9e0965a4b7422c53ed3ab7db7f84d5cb0b892928 1 parent 23bd430
Worms David authored
31 lib/csv.js
View
@@ -52,18 +52,26 @@ module.exports = function(){
// Reading API
CSV.prototype.from = function(data,options){
+ if(options) merge(this.readOptions,options);
var self = this;
process.nextTick(function(){
- parse(data);
+ if(data instanceof Array){
+ if( csv.writeOptions.lineBreaks === null ){
+ csv.writeOptions.lineBreaks = "\r\n";
+ }
+ data.forEach(function(line){
+ state.line = line;
+ flush();
+ })
+ }else{
+ parse(data);
+ }
finish();
});
return this;
}
CSV.prototype.fromStream = function(readStream, options){
- if(!readStream instanceof EventEmitter) throw new Error('Invalid stream');
- if(options){
- merge(this.readOptions,options);
- }
+ if(options) merge(this.readOptions,options);
var self = this;
readStream.on('data', function(data) { parse(data) });
readStream.on('error', function(error) { self.emit('error', error) });
@@ -74,7 +82,7 @@ module.exports = function(){
return this;
}
CSV.prototype.fromPath = function(path, options){
- merge(this.readOptions, options);
+ if(options) merge(this.readOptions,options);
var stream = fs.createReadStream(path, this.readOptions);
stream.setEncoding(this.readOptions.encoding);
return this.fromStream(stream, null);
@@ -83,9 +91,8 @@ module.exports = function(){
// Writting API
CSV.prototype.toStream = function(writeStream, options){
- if(!writeStream instanceof EventEmitter) throw new Error('Invalid stream');
+ if(options) merge(this.writeOptions,options);
var self = this;
- merge(this.writeOptions,options);
switch(this.writeOptions.lineBreaks){
case 'auto':
this.writeOptions.lineBreaks = null;
@@ -112,7 +119,7 @@ module.exports = function(){
return this;
}
CSV.prototype.toPath = function(path, options){
- merge(this.writeOptions, options);
+ if(options) merge(this.writeOptions,options);
var stream = fs.createWriteStream(path, this.writeOptions);
return this.toStream(stream, null);
}
@@ -198,11 +205,10 @@ module.exports = function(){
if(line !== null){
csv.emit('data',line,state.count);
}
- state.count++;
if(line !== null){
if(typeof line === 'object'){
if(line instanceof Array){
- var newLine = '';
+ var newLine = state.count?csv.writeOptions.lineBreaks:'';
line.forEach(function(field,i){
var containsdelimiter = field.indexOf(csv.writeOptions.delimiter||csv.readOptions.delimiter)>=0;
var containsQuote = field.indexOf(csv.writeOptions.quote||csv.readOptions.quote)>=0;
@@ -217,7 +223,7 @@ module.exports = function(){
newLine += (csv.writeOptions.delimiter||csv.readOptions.delimiter)
}
});
- line = newLine+csv.writeOptions.lineBreaks;
+ line = newLine;
}else{
}
@@ -231,6 +237,7 @@ module.exports = function(){
state.bufferPosition += state.buffer.write(line,state.bufferPosition,'utf8');
}
}
+ state.count++;
state.line = [];
state.lastC = '';
}
1  sample/sample.js
View
@@ -5,6 +5,7 @@
//
var csv = require('csv');
+
csv()
.fromPath(__dirname+'/sample.in')
.toPath(__dirname+'/sample.out')
2  test/buffer/same.out
View
@@ -18,4 +18,4 @@
20322051544,8776.0,8.8083993E7,FAE,45,2000-01-01
39828938383,2745.0,8.8002939E7,ABC,45,2000-01-11
20322051544,1979.0,8.8022838E7,FAF,45,2000-01-18
-23889399292,6346.0,8.8029388E7,ABC,45,2000-0
+23889399292,6346.0,8.8029388E7,ABC,45,2000-0
2  test/buffer/smaller.out
View
@@ -25,4 +25,4 @@
13281098009,1979.0,8.8028399E7,ABC,45,2000-01-01
39828938383,2745.0,8.8002939E7,ABC,45,2000-01-11
20322051544,8356.0,8.8092383E7,FAA,45,2000-01-31
-32989328920,5266.0,8.8028000E7,CEA,45,2000-01-01
+32989328920,5266.0,8.8028000E7,CEA,45,2000-01-01
2  test/escape/custom.out
View
@@ -1,2 +1,2 @@
20322051544,"19\"79.0",8.8017226E7,ABC,45,2000-01-01
-28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27
+28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27
2  test/escape/default.out
View
@@ -1,2 +1,2 @@
20322051544,"19""79.0",8.8017226E7,ABC,45,2000-01-01
-28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27
+28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27
27 test/fromto.js
View
@@ -39,7 +39,6 @@ module.exports = {
.from(fs.readFileSync(__dirname+'/fromto/string_to_stream.in').toString())
.toPath(__dirname+'/fromto/string_to_stream.tmp')
.on('data',function(data,index){
- console.log("ON DATA");
assert.ok(index<2);
if(index===0){
assert.strictEqual('20322051544',data[0])
@@ -55,5 +54,31 @@ module.exports = {
);
fs.unlink(__dirname+'/fromto/string_to_stream.tmp');
});
+ },
+ 'Test array to stream': function(assert){
+ // note: destination line breaks is windows styled because we can't guess it
+ var data = [
+ ["20322051544","1979.0","8.8017226E7","ABC","45","2000-01-01"],
+ ["28392898392","1974.0","8.8392926E7","DEF","23","2050-11-27"]
+ ];
+ csv()
+ .from(data)
+ .toPath(__dirname+'/fromto/array_to_stream.tmp')
+ .on('data',function(data,index){
+ assert.ok(index<2);
+ if(index===0){
+ assert.strictEqual('20322051544',data[0])
+ }else if(index===1){
+ assert.strictEqual('28392898392',data[0])
+ }
+ })
+ .on('end',function(count){
+ assert.strictEqual(2,count);
+ assert.equal(
+ fs.readFileSync(__dirname+'/fromto/array_to_stream.out').toString(),
+ fs.readFileSync(__dirname+'/fromto/array_to_stream.tmp').toString()
+ );
+ fs.unlink(__dirname+'/fromto/array_to_stream.tmp');
+ });
}
}
2  test/fromto/array_to_stream.out
View
@@ -0,0 +1,2 @@
+20322051544,1979.0,8.8017226E7,ABC,45,2000-01-01
+28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27
2  test/fromto/sample.out
View
@@ -1,2 +1,2 @@
20322051544,1979.0,8.8017226E7,ABC,45,2000-01-01
-28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27
+28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27
2  test/fromto/string_to_stream.out
View
@@ -1,2 +1,2 @@
20322051544,1979.0,8.8017226E7,ABC,45,2000-01-01
-28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27
+28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27
2  test/lineBreaks/custom.out
View
@@ -1 +1 @@
-20322051544,1979.0,8.8017226E7,ABC,45,2000-01-01::28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27::
+20322051544,1979.0,8.8017226E7,ABC,45,2000-01-01::28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27
2  test/lineBreaks/mac.out
View
@@ -1 +1 @@
-20322051544,1979.0,8.8017226E7,ABC,45,2000-01-01 28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27
+20322051544,1979.0,8.8017226E7,ABC,45,2000-01-01 28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27
2  test/lineBreaks/unicode.out
View
@@ -1 +1 @@
-20322051544,1979.0,8.8017226E7,ABC,45,2000-01-01
28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27
+20322051544,1979.0,8.8017226E7,ABC,45,2000-01-01
28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27
2  test/lineBreaks/unix.out
View
@@ -1,2 +1,2 @@
20322051544,1979.0,8.8017226E7,ABC,45,2000-01-01
-28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27
+28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27
2  test/lineBreaks/windows.out
View
@@ -1,2 +1,2 @@
20322051544,1979.0,8.8017226E7,ABC,45,2000-01-01
-28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27
+28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27
2  test/quotes/delimiter.out
View
@@ -1,3 +1,3 @@
20322051544,",1979.0,8.8017226E7,ABC,45,2000-01-01"
28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27
-"28392898392,1974.0",8.8392926E7,"DEF,23,2050-11-27"
+"28392898392,1974.0",8.8392926E7,"DEF,23,2050-11-27"
2  test/quotes/in_field.out
View
@@ -1,2 +1,2 @@
20322051544,1979.0,"8.801""7226E7",ABC,45,2000-01-01
-28392898392,1974.0,8.8392926E7,DEF,"2""3",2050-11-27
+28392898392,1974.0,8.8392926E7,DEF,"2""3",2050-11-27
2  test/quotes/regular.out
View
@@ -1,2 +1,2 @@
20322051544,1979.0,8.8017226E7,"ABC,45",2000-01-01
-28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27
+28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27
2  test/transform/reorder.out
View
@@ -1,2 +1,2 @@
2000-01-01,20322051544,1979.0,8.8017226E7,ABC,45
-2050-11-27,28392898392,1974.0,8.8392926E7,DEF,23
+2050-11-27,28392898392,1974.0,8.8392926E7,DEF,23
Please sign in to comment.
Something went wrong with that request. Please try again.