-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
116 lines (98 loc) · 2.64 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
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
#!/usr/bin/env node
const _ = require('lodash');
const fs = require('fs');
const yargs = require('yargs');
const ndjson = require('ndjson');
const { highlight } = require('cli-highlight');
const OUTPUT_COUNT = 'count';
const OUTPUT_NDJSON = 'ndjson';
const OUTPUT_JSON = 'json';
const { input, field, value, pattern, colors, limit, output, indent } = yargs
.command('* <input>', 'Search the ndjson', yargs => {
yargs
.positional('input', {
type: 'string',
description: 'The input file path'
})
.option('indent', {
alias: 'i',
type: 'number',
description:
'Spaces of indentation for json output (if none provided no formatting is applied).'
})
.option('field', {
alias: 'f',
type: 'string',
description: 'Field/path to match on (no field will return everything)'
})
.option('pattern', {
alias: 'p',
type: 'string',
description: 'RegEx pattern to match the field on'
})
.option('value', {
alias: 'v',
type: 'string',
description: 'Exact value to match the field on'
})
.option('limit', {
alias: 'l',
type: 'number',
description: 'Maximum amount of items the retrieve'
})
.option('output', {
alias: 'o',
type: 'string',
default: OUTPUT_NDJSON,
choices: [OUTPUT_NDJSON, OUTPUT_JSON, OUTPUT_COUNT],
description: 'Search result output format'
})
.option('colors', {
alias: 'c',
type: 'boolean',
default: false,
description: 'Syntax highlight the json/ndjson output'
});
})
.help().argv;
search();
function search() {
let found = [];
const stream = fs.createReadStream(input).pipe(ndjson.parse());
stream.on('data', item => {
if (!matchField(item, field, value)) {
return;
}
found.push(item);
if (output === OUTPUT_NDJSON) {
printJson(item);
}
if (found.length >= limit) {
stream.on('error', () => {});
stream.end();
}
});
stream.on('end', () => {
if (output === OUTPUT_COUNT) {
console.log(found.length);
}
if (output === OUTPUT_JSON) {
printJson(found);
}
});
}
function printJson(data) {
const jsonString = JSON.stringify(data, null, indent);
if (colors) {
console.log(highlight(jsonString, { language: 'json' }));
return;
}
console.log(jsonString);
}
function matchField(item, field, value) {
const fieldValue = String(_.get(item, field));
if (pattern) {
return new RegExp(pattern).test(fieldValue);
}
return fieldValue === String(value);
}