-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
49 lines (41 loc) · 1 KB
/
index.js
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
#!/usr/bin/env node
'use strict';
let stdin = process.openStdin();
let arg = process.argv[2];
let data = '';
stdin.on('data', chunk => {
data += chunk;
});
stdin.on('end', () => {
let lines = convertToJSON(data) || data.split('\n').filter(line => line).map(line => {
return convertToJSON(line) || line.split(' ').filter(word => word).map(word => {
return convertToJSON(word) || word;
});
});
//console.log('data' + process.argv[2]);
if(arg) {
let result = eval('lines' + (/^[\.\[].*/.test(arg.substr(0, 1)) ? '' : '.') + arg);
if(result) {
if(Array === result.constructor) {
result.filter(item => item).forEach(item => {
console.log(item);
});
} else if(typeof result === 'object') {
console.log(JSON.stringify(result, null, 4));
} else {
console.log(result);
}
}
} else {
console.log(JSON.stringify(lines, null, 4));
}
});
function convertToJSON(string) {
let result;
try {
result = JSON.parse(string);
} catch(error) {
result = false;
}
return result;
}