git ssb

3+

cel / ssb-npm-registry



Tree: 4f27567900a7b5b821bccc74e95bfc96c8233159

Files: 4f27567900a7b5b821bccc74e95bfc96c8233159 / node_modules / pull-stream / docs / throughs / take.md

1402 bytesRaw

pull-stream/throughs/take

Example usage

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

API

take has 2 valid signatures:

take(n) => through

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.

take(testFn [, opts]) => through

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.

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

Built with git-ssb-web