Comparing version 1.0.2 to 1.1.0
#!/usr/bin/env node | ||
var fs = require('fs') | ||
var minimist = require('minimist') | ||
var args = minimist(process.argv.slice(2)) | ||
var jsonfilter = require('./') | ||
var firstArg = process.argv[2] | ||
process.stdin.pipe(jsonfilter(firstArg)).on('data', function(o) { | ||
var firstArg = args._[0] | ||
process.stdin.pipe(jsonfilter(firstArg, args)).on('data', function(o) { | ||
process.stdout.write(o) | ||
@@ -8,0 +10,0 @@ }).on('end', function() { |
@@ -7,2 +7,3 @@ ## Collaborators | ||
<tr><th align="left">jlord</th><td><a href="https://github.com/jlord">GitHub/jlord</a></td></tr> | ||
<tr><th align="left">groundwater</th><td><a href="https://github.com/groundwater">GitHub/groundwater</a></td></tr> | ||
</tbody></table> |
38
index.js
var JSONStream = require('JSONStream') | ||
var combiner = require('stream-combiner') | ||
var through = require('through2') | ||
module.exports = function(filter) { | ||
return combiner( | ||
JSONStream.parse(filter), | ||
JSONStream.stringify('', '\n', '') | ||
) | ||
module.exports = function(filter, opts) { | ||
if (!opts) opts = {} | ||
var pipeline = [ | ||
JSONStream.parse(filter) | ||
] | ||
if (opts.match) { | ||
pipeline.push(createFunctionStream(opts.match, function(output, object, next) { | ||
if (!!output) this.push(object) | ||
next() | ||
})) | ||
} | ||
pipeline.push(JSONStream.stringify('', '\n', '')) | ||
return combiner.apply(null, pipeline) | ||
} | ||
function createFunctionStream(func, customTransform) { | ||
var funcStr = 'var that = ' + func + ';\n return that;' | ||
var compiled = new Function(funcStr) | ||
function transform(obj, enc, next) { | ||
var out = compiled.call(obj, obj) | ||
if (customTransform) { | ||
customTransform.call(this, out, obj, next) | ||
} else { | ||
this.push(out) | ||
next() | ||
} | ||
} | ||
return through.obj(transform) | ||
} |
{ | ||
"name": "jsonfilter", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"bin": { | ||
@@ -13,7 +13,10 @@ "jsonfilter": "cli.js" | ||
"JSONStream": "^0.8.4", | ||
"stream-combiner": "^0.2.1" | ||
"stream-combiner": "^0.2.1", | ||
"through2": "^0.6.3" | ||
}, | ||
"devDependencies": {}, | ||
"devDependencies": { | ||
"minimist": "^1.1.0" | ||
}, | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "cat test.json | jsonfilter dog" | ||
}, | ||
@@ -20,0 +23,0 @@ "repository": { |
# jsonfilter | ||
Streaming JSON filtering on the command line. | ||
Streaming JSON filtering on the command line. Supports JSON querying and expression based filtering. | ||
@@ -16,3 +16,3 @@ Works great for JSON datasets that are too big to JSON.parse() or for situations where you want to start reading data immediately. | ||
``` | ||
jsonfilter <filter> | ||
jsonfilter <selector> [--match="filter expression"] | ||
``` | ||
@@ -26,8 +26,15 @@ | ||
`"name"` matches the key `name` in an object and returns the value. | ||
Emit the value of a particular key by naming it, e.g. `"name"` matches the key `name` in an object and returns the value: | ||
``` | ||
$ echo '{"name": "Joe Blogs", "age": 28}' | jsonfilter "name" | ||
"Joe Blogs" | ||
``` | ||
`rows.*` matches any child elements of `rows`, e.g.: | ||
```BASH | ||
$ echo '{ name: "foo", type: "bar"}, { name: "foobar", type: "barfoo"}' | jsonfilter "name" | ||
# "foo" | ||
# "foobar" | ||
$ echo '{"name": "foo", "type": "bar"}{"name": "foobar", "type": "barfoo"}' | jsonfilter "name" | ||
"foo" | ||
"foobar" | ||
``` | ||
@@ -39,4 +46,4 @@ | ||
$ echo '{"rows": [ {"this object": "will be matched"}, {"so will": "this one"} ]}' | jsonfilter "rows.*" | ||
# {"this object": "will be matched"} | ||
# {"so will": "this one"} | ||
{"this object": "will be matched"} | ||
{"so will": "this one"} | ||
``` | ||
@@ -48,3 +55,3 @@ | ||
$ echo '{"rows": [ {"doc": {"this object": "will be matched"}, "foo": "bar"} ]}' | jsonfilter "rows.*.doc" | ||
# {'this object': 'will be matched'} | ||
{'this object': 'will be matched'} | ||
``` | ||
@@ -56,3 +63,19 @@ | ||
$ echo '{"rows": [ {"foo": {"bar": {"baz": {"taco": {"doc": "woo"}}}}} ]}' | jsonfilter "rows..doc" | ||
# "woo" | ||
"woo" | ||
``` | ||
## matching | ||
by default all matched objects are emitted. You can supply a custom JS expression to filter out matching objects with the `--match` option. | ||
```BASH | ||
$ echo '{"name": "foo", "type": "bar"}{"name": "foobar", "type": "barfoo"}' | jsonfilter "name" --match="this === 'foo'" | ||
# foo | ||
$ echo '{"name": "foo", "type": "bar"}{"name": "foobar", "type": "barfoo"}' | jsonfilter --match="this.name === 'foo'" | ||
{"name": "foo", "type": "bar"} | ||
$ echo '{"name": "foo", "type": "bar"}{"name": "foobar", "type": "barfoo"}' | jsonfilter --match="this.name.indexOf('foo') > -1" | ||
{"name": "foo", "type": "bar"} | ||
{"name": "foobar", "type": "barfoo"} | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
4871
7
43
0
77
3
1
+ Addedthrough2@^0.6.3
+ Addedcore-util-is@1.0.3(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedisarray@0.0.1(transitive)
+ Addedreadable-stream@1.0.34(transitive)
+ Addedstring_decoder@0.10.31(transitive)
+ Addedthrough2@0.6.5(transitive)
+ Addedxtend@4.0.2(transitive)