From 23bd430d0e400d078199720a78d1b00fd7f207fd Mon Sep 17 00:00:00 2001 From: David Worms Date: Sat, 25 Sep 2010 17:41:59 +0200 Subject: [PATCH] Improve readme sample and add sample code --- .gitignore | 1 + readme.md | 31 +++++++++++++++++++++++++------ sample/sample.in | 2 ++ sample/sample.js | 23 +++++++++++++++++++++++ test/fromto.js | 2 +- 5 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 sample/sample.in create mode 100644 sample/sample.js diff --git a/.gitignore b/.gitignore index e32101aa0..1d3506f22 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .* *.tmp lib-cov +sample/sample.out !.gitignore diff --git a/readme.md b/readme.md index 326cff923..476f1e3a0 100644 --- a/readme.md +++ b/readme.md @@ -20,23 +20,42 @@ This project provide CSV parsing and has been tested and used on large source fi Quick exemple ------------- -Using the library is a 3 steps process where all steps are optional: +Using the library is a 4 steps process: 1. Create a source -2. Create a destination -3. Transform the data +2. Create a destination (optional) +3. Transform the data (optional) +4. Listen to events (optional)
-	var csv = require('csv-parser');
+	var csv = require('csv');
 	csv()
-	.fromPath('/tmp/csv.in');
-	.toPath('/tmp/csv.out');
+	.fromPath(__dirname+'/sample.in')
+	.toPath(__dirname+'/sample.out')
 	.transform(function(data){
 		data.unshift(data.pop());
 		return data;
+	})
+	.on('data',function(data,index){
+		console.log('#'+index+' '+JSON.stringify(data));
+	})
+	.on('end',function(count){
+		console.log('Number of lines '+count);
+	})
+	.on('error',function(error){
+		console.log(error.message);
 	});
 
+Installing +---------- + +Manually +Simply copy or link the lib/csv.js file into your $HOME/.node_libraries folder or inside declared path folder. + +NPM +Simply install the project with `npm install node-csv` and you'll be ready to go. + Creating a source ----------------- diff --git a/sample/sample.in b/sample/sample.in new file mode 100644 index 000000000..342ab313a --- /dev/null +++ b/sample/sample.in @@ -0,0 +1,2 @@ +20322051544,1979.0,8.8017226E7,ABC,45,2000-01-01 +28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27 diff --git a/sample/sample.js b/sample/sample.js new file mode 100644 index 000000000..952158eb1 --- /dev/null +++ b/sample/sample.js @@ -0,0 +1,23 @@ + +// CSV sample - Copyright David Worms (MIT Licensed) +// +// node sample/sample.js +// + + var csv = require('csv'); + csv() + .fromPath(__dirname+'/sample.in') + .toPath(__dirname+'/sample.out') + .transform(function(data){ + data.unshift(data.pop()); + return data; + }) + .on('data',function(data,index){ + console.log('#'+index+' '+JSON.stringify(data)); + }) + .on('end',function(count){ + console.log('Number of lines '+count); + }) + .on('error',function(error){ + console.log(error.message); + }); \ No newline at end of file diff --git a/test/fromto.js b/test/fromto.js index ce7643b7f..d5a96694b 100644 --- a/test/fromto.js +++ b/test/fromto.js @@ -34,7 +34,7 @@ module.exports = { assert.strictEqual(2,count); }); }, - 'Test string with destination stream': function(assert){ + 'Test string to stream': function(assert){ csv() .from(fs.readFileSync(__dirname+'/fromto/string_to_stream.in').toString()) .toPath(__dirname+'/fromto/string_to_stream.tmp')