Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Apr 26, 2019
0 parents commit 36c7fa2
Show file tree
Hide file tree
Showing 7 changed files with 7,000 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
node_modules
*.log
.DS_Store
.nyc_output
coverage
7 changes: 7 additions & 0 deletions .travis.yml
@@ -0,0 +1,7 @@
sudo: required
language: node_js

node_js:
- '10'

script: npm run coveralls
30 changes: 30 additions & 0 deletions README.md
@@ -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]
```
23 changes: 23 additions & 0 deletions index.js
@@ -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

0 comments on commit 36c7fa2

Please sign in to comment.