|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +var |
| 4 | + argv=require("yargs") |
| 5 | + .version() |
| 6 | + .usage("Usage:\n $0 [input filename] [output filename]\n $0 < [input] > [output] -t") |
| 7 | + .example("$0 old.json v20.json", "converts JSON-stat file old.json into JSON-stat 2.0 (v20.json).") |
| 8 | + .example("$0 < old.json > v20.json -t", "converts JSON-stat stream old.json into a JSON-stat 2.0 stream (v20.json).") |
| 9 | + .alias("f", "filter") |
| 10 | + .describe("f", "Filter string (for example: area=DE,year=2014)") |
| 11 | + .boolean("t") |
| 12 | + .alias("t", "stream") |
| 13 | + .describe("t", "Enable the stream interface") |
| 14 | + .help("h") |
| 15 | + .alias("h", "help") |
| 16 | + .argv |
| 17 | + , |
| 18 | + inout=require("./inout"), |
| 19 | + |
| 20 | + stringify=function(json){ |
| 21 | + var tree=json.__tree__; |
| 22 | + |
| 23 | + //if v<2.0 convert to 2.0 |
| 24 | + if(!tree.hasOwnProperty("id")){ |
| 25 | + tree.version="2.0"; |
| 26 | + if(!tree.hasOwnProperty("class")){ |
| 27 | + tree.class="dataset"; |
| 28 | + } |
| 29 | + |
| 30 | + tree.id=tree.dimension.id; |
| 31 | + tree.size=tree.dimension.size; |
| 32 | + delete tree.dimension.id; |
| 33 | + delete tree.dimension.size; |
| 34 | + |
| 35 | + if(tree.dimension.hasOwnProperty("role")){ |
| 36 | + tree.role=tree.dimension.role; |
| 37 | + delete tree.dimension.role; |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + delete tree.role.classification; |
| 42 | + ["geo", "time", "metric"].forEach(function(e){ |
| 43 | + if(tree.role[e]===null){ |
| 44 | + delete tree.role[e]; |
| 45 | + } |
| 46 | + }); |
| 47 | + |
| 48 | + return JSON.stringify(tree); |
| 49 | + } |
| 50 | + |
| 51 | + callback=function(contents){ |
| 52 | + var |
| 53 | + arrfil, |
| 54 | + subset, |
| 55 | + filter=argv.filter |
| 56 | + ; |
| 57 | + |
| 58 | + //filter is not required: if not specified (no -f or -f without string) input=output |
| 59 | + if(!filter || filter===true){ |
| 60 | + //Depending on version (v<2.0) |
| 61 | + return contents.id ? contents : stringify( inout.dataset(contents) ); |
| 62 | + } |
| 63 | + |
| 64 | + if(filter.indexOf("=")===-1){ |
| 65 | + console.error("Error: The filter has not been specified correctly (missing =)."); |
| 66 | + process.exit(1); |
| 67 | + } |
| 68 | + |
| 69 | + arrfil=filter.split(",").map(function(e){ |
| 70 | + return e.split("="); |
| 71 | + }); |
| 72 | + |
| 73 | + subset=inout.dataset(contents).Slice(arrfil); |
| 74 | + if(subset===null){ |
| 75 | + console.error("Error: The input does not containt valid JSON-stat or the filters are not valid."); |
| 76 | + process.exit(1); |
| 77 | + }else{ |
| 78 | + return stringify(subset); |
| 79 | + } |
| 80 | + } |
| 81 | +; |
| 82 | + |
| 83 | +inout.main(argv, callback, true); |
0 commit comments