Skip to content

Commit

Permalink
emit csv when the first line lacks some of the fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximillian Murphy committed Mar 10, 2018
1 parent 27aa52c commit 376f296
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
35 changes: 32 additions & 3 deletions bin/csv.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env node

var fs = require('fs');

// Render lines of JSON as CSV.

var by = process.argv[2];
Expand All @@ -11,6 +13,21 @@ function csvRecord(record){
return json.substr(1,json.length-2);
}

function getHeaders(stream){
return new Promise(function(yay, nay){
var headers = {};
parseStream(stream)
.on('jline', function(record){
Object.keys(record).forEach(function(key){
headers[key] = null;
})
})
.on('end', function(){
yay(Object.keys(headers))
})
});
}

function streamToCsv(stream){
var headers;
return parseStream(stream)
Expand All @@ -33,8 +50,20 @@ if(require.main === module) {
}
process.stdout.on('error',process.exit);
console.log.apply(null,['#'].concat(process.argv));
streamToCsv(process.stdin)
.on('csvHeader', console.log)
.on('csv' , console.log);
var infile = process.argv[2];
if (infile === undefined) {
streamToCsv(process.stdin)
.on('csvHeader', console.log)
.on('csv' , console.log);
} else {
getHeaders(fs.createReadStream(infile))
.then(function(headers){
console.log(csvRecord(headers));
parseStream(fs.createReadStream(infile))
.on('jline', function(record){
console.log(csvRecord(headers.map(function(col){return (record[col] === undefined)?null:record[col];})));
});
});
}
}

4 changes: 3 additions & 1 deletion bin/csv.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ csv.md

Convert JSON lines to CSV.

This uses the keys of the first line as the column headings.
When reading from stdin, this uses the keys of the first line as the column headings.

When the input file is given as an argument it is scanned twice, once to collect all the keys and a second time to emit the data.

## Command line:

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jline",
"version": "0.0.45",
"version": "0.0.46",
"description": "Sort, grep and join files where every line is JSON.",
"main": "index.js",
"bin": {
Expand Down

0 comments on commit 376f296

Please sign in to comment.