0.1.0 • Published 2 years ago

batch-of v0.1.0

Weekly downloads
-
License
-
Repository
github
Last release
2 years ago

batch-of

Yet another function to batch an iterable into smaller iterables.

Differences:

  • Works on all iterables - array, set, generators, async generators
  • Assigns size to the output iterable (for use in other consumers like progress-of)
  • Small, no dependencies

Install

npm install batch-of

Usage

import batchOf from 'batch-of'

const array = [1,2,3]
function* sync() { yield* array }
async function* async() { yield* sync() }

for(const batch of batchOf(array)) {}
for(const batch of batchOf(sync())) {}
for await(const batch of batchOf(async())) {}

API

const output = batchOf(input, opts)
  • input Input iterable
  • opts.size [number=10] Batch size
  • opts.inputSize [number=input.size|length] Specify input iterable's size manually
  • output.size [number=input.size÷opts.size]

Async Warning

This essentially converts an async iterator to a sync iterator.

Be sure to use the await keyword to prevent an infinite loop!

    async function* async() { yield* [1,2,3] }
for await (const batch of batchOf(async()))
    ^^^^^ // must
0.1.0

2 years ago