mirror of
https://github.com/Instadapp/Swap-Aggregator-Subgraph.git
synced 2024-07-29 21:57:12 +00:00
42 lines
978 B
JavaScript
42 lines
978 B
JavaScript
'use strict'
|
|
|
|
//read a number of items and then stop.
|
|
module.exports = function take (test, opts) {
|
|
opts = opts || {}
|
|
var last = opts.last || false // whether the first item for which !test(item) should still pass
|
|
var ended = false
|
|
if('number' === typeof test) {
|
|
last = true
|
|
var n = test; test = function () {
|
|
return --n
|
|
}
|
|
}
|
|
|
|
return function (read) {
|
|
|
|
function terminate (cb) {
|
|
read(true, function (err) {
|
|
last = false; cb(err || true)
|
|
})
|
|
}
|
|
|
|
return function (end, cb) {
|
|
if(ended && !end) last ? terminate(cb) : cb(ended)
|
|
else if(ended = end) read(ended, cb)
|
|
else
|
|
read(null, function (end, data) {
|
|
if(ended = ended || end) {
|
|
//last ? terminate(cb) :
|
|
cb(ended)
|
|
}
|
|
else if(!test(data)) {
|
|
ended = true
|
|
last ? cb(null, data) : terminate(cb)
|
|
}
|
|
else
|
|
cb(null, data)
|
|
})
|
|
}
|
|
}
|
|
}
|