Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 36c7fa2
Showing
7 changed files
with
7,000 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
node_modules | ||
*.log | ||
.DS_Store | ||
.nyc_output | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
sudo: required | ||
language: node_js | ||
|
||
node_js: | ||
- '10' | ||
|
||
script: npm run coveralls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# async-iterator-batch | ||
|
||
[![Build status](https://travis-ci.org/achingbrain/async-iterator-batch.svg?branch=master)](https://travis-ci.org/achingbrain/async-iterator-batch?branch=master) [![Coverage Status](https://coveralls.io/repos/github/achingbrain/async-iterator-batch/badge.svg?branch=master)](https://coveralls.io/github/achingbrain/async-iterator-batch?branch=master) [![Dependencies Status](https://david-dm.org/achingbrain/async-iterator-batch/status.svg)](https://david-dm.org/achingbrain/async-iterator-batch) | ||
|
||
> Takes an async iterator that emits variable length arrays and emits them as fixed-size batches | ||
The final batch may be smaller than the max. | ||
|
||
## Install | ||
|
||
```sh | ||
$ npm install --save async-iterator-batch | ||
``` | ||
|
||
## Usage | ||
|
||
```javascript | ||
const batch = require('async-iterator-batch') | ||
const all = require('async-iterator-all') | ||
|
||
async function * iterator (values) { | ||
for (let i = 0; i < values.length; i++) { | ||
yield values[i] | ||
} | ||
} | ||
|
||
const result = await all(batch(iterator([[0, 1, 2], [3], [4]]), 2)) | ||
|
||
console.info(result) // [0, 1], [2, 3], [4] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use strict' | ||
|
||
async function * batch (source, size) { | ||
let things = [] | ||
|
||
for await (const set of source) { | ||
things = things.concat(set) | ||
|
||
while (things.length >= size) { | ||
yield things.slice(0, size) | ||
|
||
things = things.slice(size) | ||
} | ||
} | ||
|
||
while (things.length) { | ||
yield things.slice(0, size) | ||
|
||
things = things.slice(size) | ||
} | ||
} | ||
|
||
module.exports = batch |
Oops, something went wrong.