forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_extract.js
More file actions
54 lines (48 loc) · 1.3 KB
/
json_extract.js
File metadata and controls
54 lines (48 loc) · 1.3 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
#!/usr/bin/env node
var json = '';
if (require.main === module) {
process.stdin.setEncoding('utf8');
process.stdin.on('readable', () => {
const chunk = process.stdin.read();
if (chunk !== null) {
json += chunk;
}
});
process.stdin.on('end', () => {
var obj = JSON.parse(json);
var argv = process.argv.slice(2);
extractPaths(obj, argv).forEach(function(line) {
console.log(line);
})
});
}
function extractPaths(obj, paths) {
var lines = [];
paths.forEach(function(exp) {
var objs = obj instanceof Array ? [].concat(obj) : [obj];
exp.split('.').forEach(function(name) {
for(var i = 0; i < objs.length; i++) {
var o = objs[i];
if (o instanceof Array) {
// Expand and do over
objs = objs.slice(0, i).concat(o).concat(objs.slice(i+1, objs.length));
i--;
} else {
name.split("=").forEach(function(name, index) {
if (index == 0) {
objs[i] = o = o[name];
} else if (name.charAt(0) == '^') {
if (o.indexOf(name.substr(1)) !== 0) {
objs.splice(i, 1);
i--;
}
}
});
}
}
});
lines.push(objs.join("|"));
});
return lines;
}
exports.extractPaths = extractPaths;