{"_id":"@async-generators/bundle","_rev":"1-0c182efd5f659956785252e33a6f94b0","name":"@async-generators/bundle","description":"convert an iterable sequence into array bundles","dist-tags":{"latest":"0.1.0"},"versions":{"0.1.0":{"name":"@async-generators/bundle","description":"convert an iterable sequence into array bundles","version":"0.1.0","author":{"name":"Meirion Hughes","email":"crakinshot@yahoo.com","url":"https://github.com/MeirionHughes"},"keywords":["async","iterator","generator","esnext","typescript","linq"],"license":"MIT","main":"./dist/commonjs/index.js","typings":"./dist/commonjs/index.d.ts","module":"./dist/es2015/index.js","repository":{"type":"git","url":"git+https://github.com/async-generators/bundle.git"},"bugs":{"url":"https://github.com/async-generators/bundle/issues"},"dependencies":{"@async-generators/iterable":"^0.2.0"},"devDependencies":{"@async-generators/equal":"^0.5.0","@async-generators/map-many":"^0.1.0","@async-generators/to-array":"^0.1.0","@types/chai":"^4.0.4","@types/mocha":"^2.2.43","@types/node":"^8.0.28","chai":"^4.1.2","chai-as-promised":"^7.1.1","codecov":"^2.3.0","mocha":"^3.5.3","nyc":"^11.2.1","ts-node":"^3.3.0","typescript":"^2.5.2"},"scripts":{"test":"nyc node_modules/mocha/bin/mocha --harmony-async-iteration --require source-map-support/register --compilers ts:ts-node/register test/*.ts","build":"npm run build:commonjs && npm run build:es2015","build:commonjs":"tsc --project tsconfig.build.json --rootDir src/ --outDir ./dist/commonjs","build:es2015":"tsc --project tsconfig.build.json --rootDir src/ --outDir ./dist/es2015 --module es2015","cover":"codecov"},"engines":{"node":">=8.5.0"},"nyc":{"exclude":["node_modules/","test/","coverage/"],"extension":[".ts"],"require":["ts-node/register"],"reporter":["text-summary","lcov"],"sourceMap":true,"instrument":true},"gitHead":"db3f3fc44dd7d45088d5889e74895e5f13619f16","homepage":"https://github.com/async-generators/bundle#readme","_id":"@async-generators/bundle@0.1.0","_npmVersion":"5.0.3","_nodeVersion":"8.5.0","_npmUser":{"name":"meirionhughes","email":"crakinshot@yahoo.com"},"dist":{"integrity":"sha512-diDTGUwqffy7Tvrx0uwNcpYWWe7A8mcROozhWbPynihYhlJ8TY1soNRmkj8rpZG+9+DCgUDjPXFoNmjAuYmZdg==","shasum":"beaba224c39842d784721a6a155721ef430ce64d","tarball":"https://registry.npmjs.org/@async-generators/bundle/-/bundle-0.1.0.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIDyo761CV3vJImdftnbDJRIHMGsEo8DT8df9Eylh1DrbAiEA1xSBOU/cSgH8lqQ1mYkQad1f7y0E6/vUf0/ggsaPchw="}]},"maintainers":[{"name":"meirionhughes","email":"crakinshot@yahoo.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/bundle-0.1.0.tgz_1506246454142_0.6040919453371316"}}},"readme":"# bundle\n![logo](https://avatars1.githubusercontent.com/u/31987273?v=4&s=100)\n\nbundle items until a condition is met and yield as an iterable \n\n[![NPM version][npm-image]][npm-url]\n[![Travis Status][travis-image]][travis-url]\n[![Travis Status][codecov-image]][codecov-url]\n\n## Usage\n\n_package requires a system that supports async-iteration, either natively or via down-compiling_\n\n### Install\n```\nnpm install @async-generators/bundle --save\nyarn add @async-generators/bundle\n```\n\nThis package's `main` entry points to a `commonjs` distribution. \n\nAdditionally, the `module` entry points to a `es2015` distribution, which can be used by build systems, such as webpack, to directly use es2015 modules. \n\n## Api\n\n### bundle(source, condition)\n\n<code>bundle()</code> iterates `source` and stores each item in an array while a `condition` is true, after which the bundle array is yielded (as an array). If the `source` completes any remaining non-empty bundled array is yielded. \n\n* when `condition` is a _number_, items are stored until the array contains `condition` number of items. \n* when `condition` is a _function_, `source` items are stored in the current bundle if `condition(item, buffer) === true`. \n* when `condition(item, buffer) === false` the current bundle is yielded (sans `item`) and a new bundle is created containing the latest item. \n\n## Example 1 - fixed size \n\nexample.js\n```js\nconst bundle = require('@async-generators/bundle').default;\n\nasync function main() {\n  let source = async function* () {\n    yield 1;\n    yield 2;\n    yield 3;\n    yield 4;\n    yield 5;\n  }\n  for await (let item of bundle(source(), 2)) {\n    console.log(items);\n  }\n}\n\nmain();\n\n```\n\nExecute with the latest node.js: \n\n```\nnode --harmony-async-iteration example.js\n```\n\n\noutput:\n```\n[ 1, 2 ]\n[ 3, 4 ]\n[ 5 ]\n```\n\n## Example 2 - throttle\n\nexample.js\n```js\nconst bundle = require('@async-generators/bundle').default;\nasync function delay(duration) {\n  return new Promise(r => setTimeout(r, duration));\n}\n\nfunction throttle(period) {\n  let next;\n  return function () {\n    let start = function(){\n      next = false;\n      console.log(\"started timeout\", period);\n      setTimeout(function () { \n        next = true; \n        console.log(\"timed-out\");\n      }, period);\n    }\n\n    if (next === undefined) {\n      start();\n      return true;\n    }\n    if (next) {      \n      start();\n      return false;\n    }\n    if (next) {      \n      next = undefined;\n      return false;\n    }\n    return true;\n  }\n}\n\nasync function main() {\n  let source = async function* () {\n    for (let i = 0; i < 10; i++) {   \n      console.log(\"source yield\", i);   \n      yield i;      \n      await delay(100);     \n    }\n  }\n\n  for await (let items of bundle(source(), throttle(350))) {\n    console.log(\"bundle yield\", items);\n  }\n}\n\nmain();\n```\n\nExecute with the latest node.js: \n\n```\nnode --harmony-async-iteration example.js\n```\n\noutput:\n```\nsource yield 0\nstarted timeout 350\nsource yield 1\nsource yield 2\nsource yield 3\ntimed-out\nsource yield 4\nstarted timeout 350\nbundle yield [ 0, 1, 2, 3 ]\nsource yield 5\nsource yield 6\nsource yield 7\ntimed-out\nsource yield 8\nstarted timeout 350\nbundle yield [ 4, 5, 6, 7 ]\nsource yield 9\nbundle yield [ 8, 9 ]\ntimed-out\n```\n## Typescript\n\nThis library is fully typed and can be imported using: \n\n```ts\nimport bundle from '@async-generators/bundle');\n```\n\nIt is also possible to directly execute your [properly configured](https://stackoverflow.com/a/43694282/1657476) typescript with [ts-node](https://www.npmjs.com/package/ts-node):\n\n```\nts-node --harmony_async_iteration example.ts\n```\n\n[npm-url]: https://npmjs.org/package/@async-generators/bundle\n[npm-image]: https://img.shields.io/npm/v/@async-generators/bundle.svg\n[npm-downloads]: https://img.shields.io/npm/dm/@async-generators/bundle.svg\n[travis-url]: https://travis-ci.org/async-generators/bundle\n[travis-image]: https://img.shields.io/travis/async-generators/bundle/master.svg\n[codecov-url]: https://codecov.io/gh/async-generators/bundle\n[codecov-image]: https://codecov.io/gh/async-generators/bundle/branch/master/graph/badge.svg\n","maintainers":[{"name":"meirionhughes","email":"crakinshot@yahoo.com"}],"time":{"modified":"2022-04-04T16:01:19.786Z","created":"2017-09-24T09:47:34.974Z","0.1.0":"2017-09-24T09:47:34.974Z"},"homepage":"https://github.com/async-generators/bundle#readme","keywords":["async","iterator","generator","esnext","typescript","linq"],"repository":{"type":"git","url":"git+https://github.com/async-generators/bundle.git"},"author":{"name":"Meirion Hughes","email":"crakinshot@yahoo.com","url":"https://github.com/MeirionHughes"},"bugs":{"url":"https://github.com/async-generators/bundle/issues"},"license":"MIT","readmeFilename":"README.md"}