-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjsonstat2jsonstat
More file actions
executable file
·157 lines (133 loc) · 3.54 KB
/
jsonstat2jsonstat
File metadata and controls
executable file
·157 lines (133 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env node
const
argv=require("yargs")
.version()
.usage("Usage:\n $0 [input filename] [output filename]\n $0 < [input] > [output] -t")
.example("$0 old.json v20.json", "converts JSON-stat file old.json into JSON-stat 2.0 (v20.json).")
.example("$0 < old.json > v20.json -t", "converts JSON-stat stream old.json into a JSON-stat 2.0 stream (v20.json).")
.alias("f", "filter")
.describe("f", "Filter string (for example: area=DE,year=2014)")
.boolean("m")
.alias("m", "modify")
.describe("m", "Modify value and status type")
.boolean("o")
.alias("o", "ovalue")
.describe("o", "Type of value: object instead of array")
.boolean("s")
.alias("s", "ostatus")
.describe("s", "Type of status: object instead of array")
.boolean("t")
.alias("t", "stream")
.describe("t", "Enable the stream interface")
.help("h")
.alias("h", "help")
.argv
,
inout=require("./inout"),
arr2obj=function(o,p){ //o object p property
let ret={};
if(Object.prototype.toString.call(o[p]) === '[object Array]'){
o[p].forEach(function(e,i){
if(e!==null){
ret[String(i)]=e;
}
});
return ret;
}
return o[p];
},
obj2arr=function(o,p){ //o object p property
let
v,
ret=[],
i=o.size.reduce(function(a, b){ return a * b;});
;
if(typeof o[p] === 'object' && Object.prototype.toString.call(o[p]) !== '[object Array]'){
for(;i--;){
v=o[p][String(i)];
ret[i]=typeof v!=="undefined" ? v : null;
}
return ret;
}
return o[p];
},
modify=function(tree, attr){
const
arg=(attr==="value") ? argv.ovalue : argv.ostatus,
typ=Object.prototype.toString.call(tree[attr])
;
if(
//value/status is array but object was requested
(typ==="[object Array]" && arg)
||
//value/status is object but array was requested
(typ!=="[object Array]" && !arg)
){
const newcont=arg ? arr2obj(tree, attr) : obj2arr(tree, attr);
delete tree[attr];
tree[attr]=newcont;
}
},
stringify=function(json){
const tree=json.__tree__;
//if v<2.0 convert to 2.0
if(!tree.hasOwnProperty("id")){
tree.version="2.0";
if(!tree.hasOwnProperty("class")){
tree.class="dataset";
}
tree.id=tree.dimension.id;
tree.size=tree.dimension.size;
delete tree.dimension.id;
delete tree.dimension.size;
if(tree.dimension.hasOwnProperty("role")){
tree.role=tree.dimension.role;
delete tree.dimension.role;
}
}
if(tree.hasOwnProperty("status") && tree.status===null){
delete tree.status;
}
if(tree.hasOwnProperty("role")){
delete tree.role.classification;
["geo", "time", "metric"].forEach(function(e){
if(tree.role[e]===null){
delete tree.role[e];
}
});
}
if(argv.modify){
modify(tree, "value");
if(tree.hasOwnProperty("status")){
modify(tree, "status");
}
}
return JSON.stringify(tree);
},
callback=function(contents){
const filter=argv.filter;
let
arrfil,
subset
;
//filter is not required: if not specified (no -f or -f without string) don't slice
if(!filter || filter===true){
return stringify( inout.dataset(contents) );
}
if(filter.indexOf("=")===-1){
console.error("Error: The filter has not been specified correctly (missing =).");
process.exit(1);
}
arrfil=filter.split(",").map(function(e){
return e.split("=");
});
subset=inout.dataset(contents).Slice(arrfil);
if(subset===null){
console.error("Error: The input does not containt valid JSON-stat or the filters are not valid.");
process.exit(1);
}else{
return stringify(subset);
}
}
;
inout.main(argv, callback, true);