pull-stream
Version:
minimal pull stream
56 lines (43 loc) • 1.4 kB
Markdown
var pull = require('pull-stream')
// var take = require('pull-stream/throughs/take') // if you just need take
pull(
pull.values(['a', 'b', 'c', 'd', 'e']),
pull.take(3),
pull.collect((err, data) => {
console.log(data)
// => ['a', 'b', 'c']
})
)
```
take has 2 valid signatures:
Where `n` is a positive integer.
`take` pulls n values from the source and then closes the stream.
This is really useful for limiting how much you pull.
If `testFn` is a function, read data from the source stream and forward it downstream until `testFn(data)` returns false, then close the stream.
`opts` is an optional Object of form `{ last: Boolean }`, where `opts.last` determines whether the last value tested (before closing the stream) is included or excluded (default). e.g.
```js
pull(
pull.values([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
pull.take(n => n < 4.6), { last: true }), // include the last value tested (5)
pull.collect(function (err, results) {
console.log(results)
// => [1, 2, 3, 4, 5]
})
})
```
```js
pull(
pull.values([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
pull.take(n => n < 4.6), { last: false }), // exclude the last value tested (5)
pull.collect(function (err, results) {
console.log(results)
// => [1, 2, 3, 4]
})
})
```
```js