| ## Collaborators | ||
| json-merge is only possible due to the excellent work of the following collaborators: | ||
| <table><tbody><tr><th align="left">maxogden</th><td><a href="https://github.com/maxogden">GitHub/maxogden</a></td></tr> | ||
| <tr><th align="left">finnp</th><td><a href="https://github.com/finnp">GitHub/finnp</a></td></tr> | ||
| </tbody></table> |
+29
-6
@@ -12,5 +12,19 @@ #!/usr/bin/env node | ||
| function run() { | ||
| if (!process.argv[2] || !process.argv[3]) | ||
| return console.error('Usage: json-merge <source1> <source2> [<source3>...]') | ||
| var streams = process.argv.splice(2).map(getStream); | ||
| if (!process.argv[2] || !process.argv[3]) { | ||
| var usage = 'Usage: json-merge <source1> [options] <source2> [options] [<source3>...]' | ||
| usage += '\n\nOptions' | ||
| usage += '\n--parse=<s>\t Parse the precedent source with <s>' | ||
| console.error(usage) | ||
| } | ||
| var args = process.argv.splice(2).reduce(function (sources, current, cb) { | ||
| if(current.match(/^--parse=/)) { | ||
| sources.push({uri: sources.pop().uri, parse: current.substring(8)}) | ||
| } else { | ||
| sources.push({uri: current}) | ||
| } | ||
| return sources | ||
| }, []) | ||
| var streams = args.map(getStream); | ||
| var merger = merge(streams) | ||
@@ -20,10 +34,19 @@ merger.pipe(ldj.serialize()).pipe(process.stdout) | ||
| function getStream(uri) { | ||
| function getStream(source) { | ||
| var sourceStream | ||
| var uri = source.uri | ||
| if (uri.match(/^http\:\/\//)) { | ||
| return request(uri).pipe(ldj.parse()) | ||
| sourceStream = request(uri) | ||
| } else if (fs.existsSync(uri)) { | ||
| return fs.createReadStream(uri).pipe(ldj.parse()) | ||
| sourceStream = fs.createReadStream(uri) | ||
| } else { | ||
| return process.stdin.pipe(JSONStream.parse(uri)) | ||
| } | ||
| if(source.parse) { | ||
| return sourceStream.pipe(JSONStream.parse(source.parse)) | ||
| } else { | ||
| return sourceStream.pipe(ldj.parse()) | ||
| } | ||
| } |
+4
-2
| { | ||
| "name": "json-merge", | ||
| "version": "1.1.0", | ||
| "version": "1.2.0", | ||
| "bin": { | ||
@@ -19,3 +19,5 @@ "json-merge": "cli.js" | ||
| "scripts": { | ||
| "test": "cat foobar.json | json-merge \"data.*.a\" \"data.*.b\" \"data.*.c\"" | ||
| "test": "npm run pipe-test && npm run parse-test", | ||
| "pipe-test": "cat foobar.json | json-merge \"data.*.a\" \"data.*.b\" \"data.*.c\"", | ||
| "parse-test": "json-merge foobar.json --parse=\"data.*.a\" foobar.json --parse=\"data.*.b\"" | ||
| }, | ||
@@ -22,0 +24,0 @@ "author": "max ogden", |
+42
-1
@@ -12,3 +12,6 @@ # json-merge | ||
| npm install json-merge -g | ||
| json-merge <source1> <source2> [<source3>...] | ||
| Usage: json-merge <source1> [options] <source2> [options] [<source3>...] | ||
| Options | ||
| --parse=<s> Parse the precedent source with <s> | ||
| ``` | ||
@@ -82,1 +85,39 @@ | ||
| ``` | ||
| ### Parsing different sources | ||
| A given a file `food1.json`: | ||
| ``` | ||
| { | ||
| "salty": | ||
| { | ||
| "tacos": "muybien", | ||
| "pretzel": "jawohl" | ||
| } | ||
| } | ||
| ``` | ||
| and a file `food2.json`: | ||
| ``` | ||
| { | ||
| "sweet": | ||
| { | ||
| "waffle": "delicious", | ||
| "pancake": "yummy" | ||
| } | ||
| } | ||
| ``` | ||
| then running: | ||
| ``` | ||
| json-merge food1.json --parse="salty" food2.json --parse="sweet" | ||
| ``` | ||
| would output: | ||
| ``` | ||
| {"tacos":"muybien","pretzel":"jawohl","waffle":"delicious","pancake":"yummy"} |
4848
48.85%7
16.67%61
45.24%122
50.62%