Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an example which shows how the write function can be used to dupl…
…icate headers.
- Loading branch information
1 parent
52c1c99
commit 8b8e07b
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |