-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
37 lines (31 loc) · 900 Bytes
/
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
var JSONStream = require('JSONStream')
var combiner = require('stream-combiner')
var through = require('through2')
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)
}