UNPKG

1.4 kBMarkdownView Raw
1# pull-stream/throughs/take
2
3## Example usage
4
5```js
6var pull = require('pull-stream')
7// var take = require('pull-stream/throughs/take') // if you just need take
8
9pull(
10 pull.values(['a', 'b', 'c', 'd', 'e']),
11 pull.take(3),
12 pull.collect((err, data) => {
13 console.log(data)
14 // => ['a', 'b', 'c']
15 })
16)
17```
18
19## API
20
21take has 2 valid signatures:
22
23### `take(n) => through`
24
25Where `n` is a positive integer.
26`take` pulls n values from the source and then closes the stream.
27This is really useful for limiting how much you pull.
28
29### `take(testFn [, opts]) => through`
30
31If `testFn` is a function, read data from the source stream and forward it downstream until `testFn(data)` returns false, then close the stream.
32
33`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.
34
35```js
36pull(
37 pull.values([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
38 pull.take(n => n < 4.6), { last: true }), // include the last value tested (5)
39 pull.collect(function (err, results) {
40 console.log(results)
41 // => [1, 2, 3, 4, 5]
42 })
43})
44```
45
46```js
47pull(
48 pull.values([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
49 pull.take(n => n < 4.6), { last: false }), // exclude the last value tested (5)
50 pull.collect(function (err, results) {
51 console.log(results)
52 // => [1, 2, 3, 4]
53 })
54})
55```