From a9803336000da28f2440110bb895c5e1409fa00f Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Fri, 11 Nov 2016 14:23:25 -0800 Subject: [PATCH] Add ndjson-cat. --- README.md | 9 +++++++++ ndjson-cat | 31 +++++++++++++++++++++++++++++++ package.json | 1 + 3 files changed, 41 insertions(+) create mode 100755 ndjson-cat diff --git a/README.md b/README.md index cee2a66..a6bd704 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 +# ndjson-cat [files…] [<>](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)' +``` + # ndjson-filter [expression] [<>](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). diff --git a/ndjson-cat b/ndjson-cat new file mode 100755 index 0000000..7e88555 --- /dev/null +++ b/ndjson-cat @@ -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(); + }); + }); +} diff --git a/package.json b/package.json index 5b7f315..0569d66 100644 --- a/package.json +++ b/package.json @@ -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",