Skip to content

Commit 63dec02

Browse files
committed
Add line-by-line parsing
1 parent ae4cfe9 commit 63dec02

File tree

6 files changed

+736
-153
lines changed

6 files changed

+736
-153
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
bin

.eslintrc

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"extends": [
3+
"eslint:recommended",
4+
"prettier"
5+
],
6+
"plugins": [
7+
"prettier"
8+
],
9+
"parserOptions": {
10+
"ecmaVersion": 2016,
11+
"sourceType": "module"
12+
},
13+
"env": {
14+
"es6": true,
15+
"node": true,
16+
"mocha": true
17+
},
18+
"rules": {
19+
"prettier/prettier": ["error", { "singleQuote": true, "printWidth": 100 }],
20+
"arrow-body-style": ["error", "as-needed"],
21+
"arrow-parens": ["error", "as-needed"],
22+
"arrow-spacing": ["error"],
23+
"brace-style": ["error", "1tbs"],
24+
"curly": ["error", "all"],
25+
"dot-notation": ["error", { "allowPattern": "^[a-z]+(_[a-z]+)+$" }],
26+
"eol-last": ["error", "always"],
27+
"eqeqeq": ["error", "smart"],
28+
"generator-star-spacing": ["error"],
29+
"handle-callback-err": [0],
30+
"indent": ["error", 2],
31+
"lines-around-directive": ["error", { "before": "never", "after": "always" }],
32+
"max-len": [1, { "code": 100, "ignoreUrls": true }],
33+
"new-cap": [0],
34+
"newline-after-var": ["error", "always"],
35+
"newline-before-return": ["error"],
36+
"no-console": [1],
37+
"no-else-return": ["error"],
38+
"no-extra-bind": ["error"],
39+
"no-extra-label": ["error"],
40+
"no-floating-decimal": ["error"],
41+
"no-implicit-coercion": ["error", { "allow": ["!!"] }],
42+
"no-lonely-if": ["error"],
43+
"no-undef-init": ["error"],
44+
"no-unneeded-ternary": ["error"],
45+
"no-useless-computed-key": ["error"],
46+
"no-useless-rename": ["error"],
47+
"no-useless-return": ["error"],
48+
"no-var": ["error"],
49+
"nonblock-statement-body-position": ["error", "below"],
50+
"object-curly-spacing": ["error", "always"],
51+
"object-property-newline": ["error"],
52+
"object-shorthand": ["error"],
53+
"prefer-arrow-callback": ["error"],
54+
"prefer-const": ["error"],
55+
"prefer-spread": ["error"],
56+
"prefer-template": ["error"],
57+
"quotes": ["error", "backtick", { "avoidEscape": true }],
58+
"require-jsdoc": [0],
59+
"rest-spread-spacing": ["error"],
60+
"spaced-comment": ["error", "always"],
61+
"template-curly-spacing": ["error"],
62+
"valid-jsdoc": [0],
63+
"wrap-iife": ["error", "inside"],
64+
"wrap-regex": ["error"],
65+
"yield-star-spacing": ["error"],
66+
"yoda": ["error", "never"]
67+
}
68+
}

bin/index.js

Lines changed: 89 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ var _mapStream = require('map-stream');
2121

2222
var _mapStream2 = _interopRequireDefault(_mapStream);
2323

24+
var _split = require('split');
25+
26+
var _split2 = _interopRequireDefault(_split);
27+
2428
var _concatStream = require('concat-stream');
2529

2630
var _concatStream2 = _interopRequireDefault(_concatStream);
@@ -33,110 +37,104 @@ var _path = require('path');
3337

3438
var _path2 = _interopRequireDefault(_path);
3539

36-
function _interopRequireDefault(obj) {
37-
return obj && obj.__esModule ? obj : { default: obj };
38-
}
39-
40-
var argv = _yargs2.default
41-
.usage(
42-
'Pipe $0 onto a JSON source from the commandline to parse the output:\n cat data.json | $0 [options] query'
43-
)
44-
.options({
45-
p: {
46-
alias: 'path',
47-
describe: 'Use JSON Path notation (https://github.com/dchester/jsonpath)',
48-
type: 'boolean'
49-
},
50-
k: {
51-
alias: 'keys',
52-
describe: 'Print object keys only',
53-
type: 'boolean'
54-
},
55-
f: {
56-
alias: 'file',
57-
describe: 'Read input from file',
58-
requiresArg: true,
59-
type: 'string'
60-
},
61-
i: {
62-
alias: 'indent',
63-
describe: 'Number of spaces for indentation (ignored by --human)',
64-
requiresArg: true,
65-
default: 2,
66-
type: 'number',
67-
coerce: function coerce(x) {
68-
return !isNaN(parseFloat(x)) && isFinite(x) ? +x : x;
69-
}
70-
},
71-
h: {
72-
alias: 'human',
73-
describe: 'Print human-readable (non-JSON) format',
74-
type: 'boolean'
75-
},
76-
b: {
77-
alias: 'break',
78-
describe: 'Set break length of object/array for human format',
79-
requiresArg: true,
80-
type: 'number'
81-
},
82-
c: {
83-
alias: 'no-color',
84-
describe: 'Disable color for human format',
85-
type: 'boolean'
86-
},
87-
d: {
88-
alias: 'depth',
89-
describe: 'Depth for human format',
90-
requiresArg: true,
91-
type: 'number'
92-
}
93-
})
94-
.help()
95-
.epilogue(
96-
'Queries use the Lodash get method by default.\nFor more information, see https://github.com/therealklanni/jp'
97-
).argv;
98-
99-
var format = argv.human
100-
? function(x) {
101-
return x;
40+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
41+
42+
var argv = _yargs2.default.usage('Pipe $0 onto a JSON source from the commandline to parse the output:\n cat data.json | $0 [options] query').options({
43+
p: {
44+
alias: 'path',
45+
describe: 'Use JSON Path notation (https://github.com/dchester/jsonpath)',
46+
type: 'boolean'
47+
},
48+
k: {
49+
alias: 'keys',
50+
describe: 'Print object keys only',
51+
type: 'boolean'
52+
},
53+
f: {
54+
alias: 'file',
55+
describe: 'Read input from file',
56+
requiresArg: true,
57+
type: 'string'
58+
},
59+
i: {
60+
alias: 'indent',
61+
describe: 'Number of spaces for indentation (ignored by --human)',
62+
requiresArg: true,
63+
default: 2,
64+
type: 'number',
65+
coerce: function coerce(x) {
66+
return !isNaN(parseFloat(x)) && isFinite(x) ? Number(x) : x;
10267
}
103-
: _lodash2.default.partialRight(JSON.stringify, null, argv.indent || 2);
68+
},
69+
h: {
70+
alias: 'human',
71+
describe: 'Print human-readable (non-JSON) format',
72+
type: 'boolean'
73+
},
74+
b: {
75+
alias: 'break',
76+
describe: 'Set break length of object/array for human format',
77+
requiresArg: true,
78+
type: 'number'
79+
},
80+
c: {
81+
alias: 'no-color',
82+
describe: 'Disable color for human format',
83+
type: 'boolean'
84+
},
85+
d: {
86+
alias: 'depth',
87+
describe: 'Depth for human format',
88+
requiresArg: true,
89+
type: 'number'
90+
},
91+
L: {
92+
alias: 'line-by-line',
93+
describe: 'Parse each line as a separate input',
94+
type: 'boolean'
95+
}
96+
}).help().epilogue('Queries use the Lodash get method by default.\nFor more information, see https://github.com/therealklanni/jp').argv;
97+
98+
var format = argv.human ? function (x) {
99+
return x;
100+
} : _lodash2.default.partialRight(JSON.stringify, null, argv.indent || 2);
101+
102+
var logOpts = {
103+
colors: !argv.noColor,
104+
breakLength: argv.break || null,
105+
depth: argv.depth >= 0 ? argv.depth : null
106+
};
104107

105-
var log = argv.human
106-
? _lodash2.default.partialRight(console.dir.bind(console), {
107-
colors: !argv.noColor,
108-
breakLength: argv.break || null,
109-
depth: argv.depth >= 0 ? argv.depth : null
110-
})
111-
: console.log.bind(console);
108+
var log = argv.human ? _lodash2.default.partialRight(console.dir.bind(console), logOpts) : console.log.bind(console);
112109

113110
var print = _lodash2.default.flow(format, log);
114111

115112
var query = argv._[0];
116113

114+
var parseBuf = function parseBuf(buf) {
115+
var obj = JSON.parse(buf.toString());
116+
var output = void 0;
117+
118+
if (argv.path) {
119+
output = query ? _jsonpath2.default.query(obj, query) : obj;
120+
} else {
121+
output = query ? _lodash2.default.get(obj, query) : obj;
122+
}
123+
124+
print(argv.keys ? Object.keys(output) : output);
125+
};
126+
117127
var parse = function parse(stream) {
118-
stream.pipe((0, _utf8Stream2.default)()).pipe(
119-
(0, _concatStream2.default)(function(buf) {
120-
var obj = JSON.parse(buf.toString());
121-
var output = void 0;
122-
123-
if (argv.path) {
124-
output = query ? _jsonpath2.default.query(obj, query) : obj;
125-
} else {
126-
output = query ? _lodash2.default.get(obj, query) : obj;
127-
}
128-
129-
print(argv.keys ? Object.keys(output) : output);
130-
})
131-
);
128+
return stream.pipe((0, _utf8Stream2.default)())
129+
// Use utf8 effectively as a noop
130+
.pipe(argv.L ? (0, _split2.default)() : (0, _utf8Stream2.default)()).pipe(argv.L ? (0, _mapStream2.default)(parseBuf) : (0, _concatStream2.default)(parseBuf));
132131
};
133132

134133
if (!process.stdin.isTTY) {
135134
parse(process.stdin);
136135
} else if (argv.file) {
137-
parse(
138-
_fs2.default.createReadStream(_path2.default.resolve(argv.file), 'utf8')
139-
);
136+
parse(_fs2.default.createReadStream(_path2.default.resolve(argv.file), 'utf8'));
140137
} else {
141138
_yargs2.default.showHelp();
142139
}
140+

0 commit comments

Comments
 (0)