Skip to content

V2: utils: takeWhile()

Eugene Lazutkin edited this page Aug 3, 2022 · 1 revision

(Since 2.2.0)

takeWhile() takes items as long as a function returns a truthy value, then all items are ignored. It is returned by require('stream-chain/utils/takeWhile') and it is based on Transform.

Factory function: takeWhile(options)

It takes options and returns a Transform stream.

options can be one of these:

  • an object detailed in the Node's documentation.
    • readableObjectMode and writableObjectMode are always set to `true.
    • The following custom options are recognized:
      • (optional) condition is a function or an asynchronous function, which takes an item and returns a boolean. Default: () => true.
  • Otherwise it is assumed to be a function, same as condition described above.

Static property: takeWhile.Constructor

A constructor of the underlying stream class produced by takeWhile(). Useful to inspect objects with instanceof.

Static property: takeWhile.Constructor.make

An alias to takeWhile. Useful with metaprogramming.

Examples

const {chain} = require('stream-chain');
const takeWhile = require('stream-chain/utils/takeWhile');

// get numbers until we encounter 3
const pipeline = chain([
  takeWhile(item => item !== 3)
  // ... the rest of the pipeline
]);
// input:  1, 2, 3, 4, 5
// output: 1, 2
Clone this wiki locally