# split-array-stream

> Safely push each item of an array to a stream

Latest version **2.0.0** (published 2018-04-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install split-array-stream
pnpm add split-array-stream
yarn add split-array-stream
bun add split-array-stream
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2018-04-02 |
| First published | 2015-08-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Stephen Sawchuk |
| Maintainers | stephenplusplus |
| Keywords | array, stream, split, push |

## Links

- npm: https://www.npmjs.com/package/split-array-stream
- Repository: https://github.com/stephenplusplus/split-array-stream
- Homepage: https://github.com/stephenplusplus/split-array-stream#readme
- Issues: https://github.com/stephenplusplus/split-array-stream/issues
- npm.io page: https://npm.io/package/split-array-stream

## Dependencies (1)

- [is-stream-ended](https://npm.io/package/is-stream-ended.md) ^0.1.4

## Alternatives

- [byte-size](https://npm.io/package/byte-size.md) — 2.1M weekly downloads
- [speed-limiter](https://npm.io/package/speed-limiter.md) — 16.0K weekly downloads
- [@powersync/node](https://npm.io/package/@powersync/node.md) — 10.9K weekly downloads
- [@ledgerhq/coin-cardano](https://npm.io/package/@ledgerhq/coin-cardano.md) — 1.0K weekly downloads
- [@jayesol/jayeson.lib.streamfinder](https://npm.io/package/@jayesol/jayeson.lib.streamfinder.md) — 1.0K weekly downloads

## Recent versions

- 2.0.0 (latest) — 2018-04-02
- 1.0.3 — 2017-05-09
- 1.0.2 — 2017-05-08
- 1.0.1 — 2017-05-05
- 1.0.0 — 2015-08-01

## README

# split-array-stream
> Safely push each item of an array to a stream.

```sh
$ npm install --save split-array-stream
```
```js
const split = require('split-array-stream');
const through = require('through2');

const array = [
  { id: 1, user: 'Dave' },
  { id: 2, user: 'Stephen' }
];

const stream = through.obj();

stream.on('data', (item) => {
  // { id: 1, user: 'Dave' }
  // ...later...
  // { id: 2, user: 'Stephen' }
});

split(array, stream).then((streamEnded) => {
  if (!streamEnded) {
    stream.push(null);
    stream.end();
  }
}).catch(console.error);
```

Before pushing an item to the stream, `split-array-stream` checks that the stream hasn't been ended. This avoids those "push() after EOF" errors.

### Use case

Say you're getting many items from an upstream API. Multiple requests might be required to page through all of the results. You want to push the results to the stream as they come in, and only get more results if the user hasn't ended the stream.

```js
function getAllUsers() {
  var stream = through.obj();

  var requestOptions = {
    method: 'get',
    url: 'http://api/users',
  };

  request(requestOptions, onResponse);

  function onResponse(err, response) {
    split(response.users, stream).then((streamEnded) => {
      if (streamEnded) {
        return;
      }

      if (response.nextPageToken) {
        requestOptions.pageToken = response.nextPageToken;
        request(requestOptions, onResponse);
        return;
      }

      stream.push(null);
      stream.end();
    });

  });

  return stream;
}

getAllUsers()
  .on('data', function (user) {
    // An item from the `response.users` API response
  })
  .on('end', function () {
    // All users received
  });
```


### split(array, stream, callback)

#### array

- Type: `Array`
- Required

The source array. Each item will be pushed to the provided stream.

#### stream

- Type: `Stream`
- Required

The destination stream to receive the items of the array.

#### callback(streamEnded)

- Type: `Function`
- Required

Callback function executed after all items of the array have been iterated.

##### callback.streamEnded

- Type: `Boolean`

Lets you know if the stream has been ended while items were being pushed.

---
_Source: https://npm.io/package/split-array-stream · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
