Skip to content

Commit

Permalink
Add an example which shows how the write function can be used to dupl…
Browse files Browse the repository at this point in the history
…icate headers.
  • Loading branch information
jonseymour committed Apr 9, 2011
1 parent 52c1c99 commit 8b8e07b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions samples/duplicate-headers.in
@@ -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
40 changes: 40 additions & 0 deletions samples/duplicate-headers.js
@@ -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.