Skip to content

Commit

Permalink
Add ndjson-cat.
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Nov 11, 2016
1 parent 084829d commit a980333
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ npm install ndjson-cli

## Command Line Reference

* [ndjson-cat](#ndjson_cat) - concatenate objects to form a stream
* [ndjson-filter](#ndjson_filter) - filter objects
* [ndjson-map](#ndjson_map) - transform objects
* [ndjson-reduce](#ndjson_reduce) - reduce a stream of objects to a single value
Expand All @@ -16,6 +17,14 @@ npm install ndjson-cli
* [ndjson-sort](#ndjson_sort) - sort a stream of objects
* [ndjson-top](#ndjson_top) - select the top objects from a stream

<a name="ndjson_cat" href="#ndjson_cat">#</a> <b>ndjson-cat</b> [<i>files…</i>] [<>](https://github.com/mbostock/ndjson-cli/blob/master/ndjson-cat "Source")

Sequentially concatenates one or more input *files* containing JSON into a single newline-delimited JSON on stdout. If *files* is not specified, it defaults to “-”, indicating stdin. This command is especially useful for converting pretty-printed JSON (that contains newlines) into newline-delimited JSON. For example, to print the binaries exported by this repository’s package.json:

```
ndjson-cat package.json | ndjson-split 'Object.keys(d.bin)'
```

<a name="ndjson_filter" href="#ndjson_filter">#</a> <b>ndjson-filter</b> [<i>expression</i>] [<>](https://github.com/mbostock/ndjson-cli/blob/master/ndjson-filter "Source")

Filters the newline-delimited JSON stream on stdin according to the specified *expression*: if the *expression* evaluates truthily for the given JSON object *d* at the given zero-based index *i* in the stream, the resulting JSON object is output to stdout; otherwise, it is ignored. If *expression* is not specified, it defaults to `true`. This program is much like [*array*.filter](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter).
Expand Down
31 changes: 31 additions & 0 deletions ndjson-cat
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env node

var fs = require("fs"),
output = require("./output");

if (process.argv.length < 3) {
process.argv[2] = "-";
}

var i = 1, n = process.argv.length, task = Promise.resolve();
while (++i < n) task = task.then(cat(process.argv[i]));
task.catch(function(error) { console.error(error); });

function cat(input) {
return new Promise(function(resolve, reject) {
var data = [], stream = input === "-" ? process.stdin : fs.createReadStream(input);
stream
.on("data", function(d) { data.push(d); })
.on("error", reject)
.on("end", function() {
try {
var d = JSON.parse(data);
} catch (e) {
console.error((input === "-" ? "stdin" : input) + ": SyntaxError: " + e.message);
process.exit(1);
};
output(d);
resolve();
});
});
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"url": "http://github.com/mbostock/ndjson-cli.git"
},
"bin": {
"ndjson-cat": "ndjson-cat",
"ndjson-filter": "ndjson-filter",
"ndjson-join": "ndjson-join",
"ndjson-map": "ndjson-map",
Expand Down

0 comments on commit a980333

Please sign in to comment.