-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
33 lines (28 loc) · 905 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
var ldj = require('ldjson-stream')
var pumpify = require('pumpify')
var reduce = require('through2-reduce')
module.exports = function(func, memo) {
var stream = createFunctionStream(func, memo)
return pumpify.obj(ldj.parse(), stream)
}
function createFunctionStream(func, memo) {
var compiled;
try {
memo = memo ? JSON.parse(memo) : {}
} catch(err) {
throw err
}
if (typeof func !== 'function') {
var funcStr = func + ';\n return this;'
if (func[0] === '{') funcStr = 'var that = ' + func + ';\n return that;'
compiled = new Function('previous', 'current', funcStr)
} else {
compiled = function() {
return func.apply(this, arguments)
|| this; // in case the function just mutates `this` w/o returning.
};
}
return reduce({objectMode: true}, function(previous, current) {
return compiled.call(previous, previous, current)
}, memo)
}