You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
978 B

'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)
})
}
}
}