{"_id":"split-array-stream","_rev":"293900","name":"split-array-stream","description":"Safely push each item of an array to a stream","dist-tags":{"latest":"2.0.0"},"maintainers":[{"name":"stephenplusplus","email":""}],"time":{"modified":"2021-06-03T18:54:45.000Z","created":"2015-08-01T13:18:58.126Z","2.0.0":"2018-04-02T17:03:19.304Z","1.0.3":"2017-05-09T15:40:54.503Z","1.0.2":"2017-05-08T16:05:00.431Z","1.0.1":"2017-05-05T20:34:52.627Z","1.0.0":"2015-08-01T13:18:58.126Z"},"users":{},"author":{"name":"Stephen Sawchuk","email":"sawchuk@gmail.com"},"repository":{"type":"git","url":"git+https://github.com/stephenplusplus/split-array-stream.git"},"versions":{"2.0.0":{"name":"split-array-stream","version":"2.0.0","description":"Safely push each item of an array to a stream","main":"./build/src/index.js","types":"./build/src/index.d.ts","files":["build/src"],"repository":{"type":"git","url":"git+https://github.com/stephenplusplus/split-array-stream.git"},"scripts":{"test":"mocha build/test","check":"gts check","clean":"gts clean","compile":"tsc -p .","fix":"gts fix","prepare":"npm run compile","pretest":"npm run compile","posttest":"npm run check"},"keywords":["array","stream","split","push"],"author":{"name":"Stephen Sawchuk","email":"sawchuk@gmail.com"},"license":"MIT","devDependencies":{"@types/mocha":"^5.0.0","@types/node":"^9.6.1","@types/through2":"^2.0.33","gts":"^0.5.4","mocha":"^5.0.5","through2":"^2.0.3","typescript":"~2.8.1"},"dependencies":{"is-stream-ended":"^0.1.4"},"gitHead":"6304594c5724cb854237d68b7174b1ef72e32c28","bugs":{"url":"https://github.com/stephenplusplus/split-array-stream/issues"},"homepage":"https://github.com/stephenplusplus/split-array-stream#readme","_id":"split-array-stream@2.0.0","_npmVersion":"5.7.1","_nodeVersion":"9.1.0","_npmUser":{"name":"stephenplusplus","email":"stephenplusplusplus@gmail.com"},"dist":{"shasum":"85a4f8bfe14421d7bca7f33a6d176d0c076a53b1","size":3849,"noattachment":false,"key":"/split-array-stream/-/split-array-stream-2.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/split-array-stream/download/split-array-stream-2.0.0.tgz"},"maintainers":[{"name":"stephenplusplus","email":""}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/split-array-stream_2.0.0_1522688599206_0.4512056500555839"},"_hasShrinkwrap":false,"publish_time":1522688599304,"_cnpm_publish_time":1522688599304},"1.0.3":{"name":"split-array-stream","version":"1.0.3","description":"Safely push each item of an array to a stream","main":"index.js","repository":{"type":"git","url":"git+https://github.com/stephenplusplus/split-array-stream.git"},"scripts":{"test":"mocha"},"keywords":["array","stream","split","push"],"author":{"name":"Stephen Sawchuk","email":"sawchuk@gmail.com"},"license":"MIT","devDependencies":{"mocha":"^3.3.0","through2":"^2.0.0"},"dependencies":{"async":"^2.4.0","is-stream-ended":"^0.1.0"},"gitHead":"9a65446b670b80f160af427dd97ea1f2468106ef","bugs":{"url":"https://github.com/stephenplusplus/split-array-stream/issues"},"homepage":"https://github.com/stephenplusplus/split-array-stream#readme","_id":"split-array-stream@1.0.3","_shasum":"d2b75a8e5e0d824d52fdec8b8225839dc2e35dfa","_from":".","_npmVersion":"4.2.0","_nodeVersion":"7.8.0","_npmUser":{"name":"stephenplusplus","email":"sawchuk@gmail.com"},"dist":{"shasum":"d2b75a8e5e0d824d52fdec8b8225839dc2e35dfa","size":2624,"noattachment":false,"key":"/split-array-stream/-/split-array-stream-1.0.3.tgz","tarball":"http://registry.cnpm.dingdandao.com/split-array-stream/download/split-array-stream-1.0.3.tgz"},"maintainers":[{"name":"stephenplusplus","email":""}],"_npmOperationalInternal":{"host":"packages-12-west.internal.npmjs.com","tmp":"tmp/split-array-stream-1.0.3.tgz_1494344452438_0.25778956385329366"},"directories":{},"publish_time":1494344454503,"_hasShrinkwrap":false,"_cnpm_publish_time":1494344454503},"1.0.2":{"name":"split-array-stream","version":"1.0.2","description":"Safely push each item of an array to a stream","main":"index.js","repository":{"type":"git","url":"git+https://github.com/stephenplusplus/split-array-stream.git"},"scripts":{"test":"mocha"},"keywords":["array","stream","split","push"],"author":{"name":"Stephen Sawchuk","email":"sawchuk@gmail.com"},"license":"MIT","devDependencies":{"mocha":"^3.3.0","through2":"^2.0.0"},"dependencies":{"async":"^2.4.0","end-of-stream":"^1.4.0"},"gitHead":"ab410d2a91571a48ef5beba8475ef290498361d2","bugs":{"url":"https://github.com/stephenplusplus/split-array-stream/issues"},"homepage":"https://github.com/stephenplusplus/split-array-stream#readme","_id":"split-array-stream@1.0.2","_shasum":"f9678169186e0eb16db8164f350742d73305a1e8","_from":".","_npmVersion":"4.2.0","_nodeVersion":"7.8.0","_npmUser":{"name":"stephenplusplus","email":"sawchuk@gmail.com"},"dist":{"shasum":"f9678169186e0eb16db8164f350742d73305a1e8","size":2667,"noattachment":false,"key":"/split-array-stream/-/split-array-stream-1.0.2.tgz","tarball":"http://registry.cnpm.dingdandao.com/split-array-stream/download/split-array-stream-1.0.2.tgz"},"maintainers":[{"name":"stephenplusplus","email":""}],"_npmOperationalInternal":{"host":"packages-12-west.internal.npmjs.com","tmp":"tmp/split-array-stream-1.0.2.tgz_1494259498568_0.09971072524785995"},"directories":{},"publish_time":1494259500431,"_cnpm_publish_time":1494259500431,"_hasShrinkwrap":false},"1.0.1":{"name":"split-array-stream","version":"1.0.1","description":"Safely push each item of an array to a stream","main":"index.js","repository":{"type":"git","url":"git+https://github.com/stephenplusplus/split-array-stream.git"},"scripts":{"test":"mocha"},"keywords":["array","stream","split","push"],"author":{"name":"Stephen Sawchuk","email":"sawchuk@gmail.com"},"license":"MIT","devDependencies":{"mocha":"^3.3.0","through2":"^2.0.0"},"dependencies":{"async":"^2.4.0","end-of-stream":"^1.4.0"},"gitHead":"07d0155042cc73e5f27e1e27c9c859f65a61adcf","bugs":{"url":"https://github.com/stephenplusplus/split-array-stream/issues"},"homepage":"https://github.com/stephenplusplus/split-array-stream#readme","_id":"split-array-stream@1.0.1","_shasum":"b269b0e327ec2757f0820ac48d811971ad9c7263","_from":".","_npmVersion":"4.2.0","_nodeVersion":"7.8.0","_npmUser":{"name":"stephenplusplus","email":"sawchuk@gmail.com"},"dist":{"shasum":"b269b0e327ec2757f0820ac48d811971ad9c7263","size":2638,"noattachment":false,"key":"/split-array-stream/-/split-array-stream-1.0.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/split-array-stream/download/split-array-stream-1.0.1.tgz"},"maintainers":[{"name":"stephenplusplus","email":""}],"_npmOperationalInternal":{"host":"packages-18-east.internal.npmjs.com","tmp":"tmp/split-array-stream-1.0.1.tgz_1494016491366_0.6346166864968836"},"directories":{},"publish_time":1494016492627,"_cnpm_publish_time":1494016492627,"_hasShrinkwrap":false},"1.0.0":{"name":"split-array-stream","version":"1.0.0","description":"Safely push each item of an array to a stream","main":"index.js","repository":{"type":"git","url":"git+https://github.com/stephenplusplus/split-array-stream.git"},"scripts":{"test":"mocha"},"keywords":["array","stream","split","push"],"author":{"name":"Stephen Sawchuk","email":"sawchuk@gmail.com"},"license":"MIT","devDependencies":{"mocha":"^2.2.5","through2":"^2.0.0"},"dependencies":{"async":"^1.4.0","is-stream-ended":"^0.1.0"},"gitHead":"35e934decf8908033046b45bded8c113a48bcb56","bugs":{"url":"https://github.com/stephenplusplus/split-array-stream/issues"},"homepage":"https://github.com/stephenplusplus/split-array-stream#readme","_id":"split-array-stream@1.0.0","_shasum":"d5e4ffacd306161d69ed5252ff56d57e7762eaa2","_from":".","_npmVersion":"2.11.3","_nodeVersion":"0.12.7","_npmUser":{"name":"stephenplusplus","email":"sawchuk@gmail.com"},"dist":{"shasum":"d5e4ffacd306161d69ed5252ff56d57e7762eaa2","size":2649,"noattachment":false,"key":"/split-array-stream/-/split-array-stream-1.0.0.tgz","tarball":"http://registry.cnpm.dingdandao.com/split-array-stream/download/split-array-stream-1.0.0.tgz"},"maintainers":[{"name":"stephenplusplus","email":""}],"directories":{},"publish_time":1438435138126,"_cnpm_publish_time":1438435138126,"_hasShrinkwrap":false}},"readme":"# split-array-stream\n> Safely push each item of an array to a stream.\n\n```sh\n$ npm install --save split-array-stream\n```\n```js\nconst split = require('split-array-stream');\nconst through = require('through2');\n\nconst array = [\n  { id: 1, user: 'Dave' },\n  { id: 2, user: 'Stephen' }\n];\n\nconst stream = through.obj();\n\nstream.on('data', (item) => {\n  // { id: 1, user: 'Dave' }\n  // ...later...\n  // { id: 2, user: 'Stephen' }\n});\n\nsplit(array, stream).then((streamEnded) => {\n  if (!streamEnded) {\n    stream.push(null);\n    stream.end();\n  }\n}).catch(console.error);\n```\n\nBefore pushing an item to the stream, `split-array-stream` checks that the stream hasn't been ended. This avoids those \"push() after EOF\" errors.\n\n### Use case\n\nSay 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.\n\n```js\nfunction getAllUsers() {\n  var stream = through.obj();\n\n  var requestOptions = {\n    method: 'get',\n    url: 'http://api/users',\n  };\n\n  request(requestOptions, onResponse);\n\n  function onResponse(err, response) {\n    split(response.users, stream).then((streamEnded) => {\n      if (streamEnded) {\n        return;\n      }\n\n      if (response.nextPageToken) {\n        requestOptions.pageToken = response.nextPageToken;\n        request(requestOptions, onResponse);\n        return;\n      }\n\n      stream.push(null);\n      stream.end();\n    });\n\n  });\n\n  return stream;\n}\n\ngetAllUsers()\n  .on('data', function (user) {\n    // An item from the `response.users` API response\n  })\n  .on('end', function () {\n    // All users received\n  });\n```\n\n\n### split(array, stream, callback)\n\n#### array\n\n- Type: `Array`\n- Required\n\nThe source array. Each item will be pushed to the provided stream.\n\n#### stream\n\n- Type: `Stream`\n- Required\n\nThe destination stream to receive the items of the array.\n\n#### callback(streamEnded)\n\n- Type: `Function`\n- Required\n\nCallback function executed after all items of the array have been iterated.\n\n##### callback.streamEnded\n\n- Type: `Boolean`\n\nLets you know if the stream has been ended while items were being pushed.\n","_attachments":{},"homepage":"https://github.com/stephenplusplus/split-array-stream#readme","bugs":{"url":"https://github.com/stephenplusplus/split-array-stream/issues"},"license":"MIT"}