Permalink
Browse files

Add an example which shows how the write function can be used to dupl…

…icate headers.
  • Loading branch information...
1 parent 52c1c99 commit 8b8e07b58dadedbdac46ff193180319a8fb1e5ff @jonseymour jonseymour committed Apr 9, 2011
Showing with 43 additions and 0 deletions.
  1. +3 −0 samples/duplicate-headers.in
  2. +40 −0 samples/duplicate-headers.js
@@ -0,0 +1,3 @@
+ts,year,ms,chars,age,date
+20322051544,1979.0,8.8017226E7,ABC,45,2000-01-01
+28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27
@@ -0,0 +1,40 @@
+// CSV sample - Copyright jon seymour jon.seymour@gmail.com
+// node samples/duplicate-headers.js < samples/duplicate-headers.in
+
+//
+// This sample shows how the write function can be used
+// to write a new record into the output stream.
+//
+// In this rather contrived example, the first line read
+// is interpreted as a header and is inserted before
+// every other data line in the file.
+//
+
+ var
+ csv = require('csv'),
+ header;
+
+ process.stdin.resume();
+ csv()
+ .fromStream(process.stdin)
+ .toStream(process.stdout)
+ .transform(function(data){
+ if (header) {
+ this.write(header);
+ } else {
+ header=data;
+ return null;
+ }
+ return data;
+ })
+ .on('error',function(error){
+ console.log(error.message);
+ });
+
+//
+// expected output
+//
+//ts,year,ms,chars,age,date
+//20322051544,1979.0,8.8017226E7,ABC,45,2000-01-01
+//ts,year,ms,chars,age,date
+//28392898392,1974.0,8.8392926E7,DEF,23,2050-11-27

0 comments on commit 8b8e07b

Please sign in to comment.