Skip to content

V2: utils: take()

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

(Since 2.2.0)

take() takes a given number of items from a stream optionally skipping some elements before that. It is returned by require('stream-chain/utils/take') and it is based on Transform.

Factory function: take(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) n is a positive integer indicating how many items to take. Default: 1.
      • (optional) skip is a non-negative integer indicating how many items to skip from the beginning before starting to take. Default: 0.
  • Otherwise it is assumed to be a positive integer, same as n described above.

Static property: take.Constructor

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

Static property: take.Constructor.make

An alias to take. Useful with metaprogramming.

Examples

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

// get 5 items from the top
const pipeline = chain([
  take(5)
  // ... the rest of the pipeline
]);

// skip 10, then get 5 items
const pipeline = chain([
  take({skip: 10, n: 5})
  // ... the rest of the pipeline
]);
Clone this wiki locally