UNPKG

iter-tools-es

Version:
40 lines (33 loc) 794 B
const { asyncIterableCurry } = require('../../internal/async-iterable.js'); const { __asyncSpliterate } = require('../$spliterate/async-spliterate.js'); async function* asyncBatchSpliterator(split, { size }, source) { let i = 0; for await (const value of source) { if (i === size) { yield split; i = 0; } yield value; i++; } } function __asyncBatch(source, size) { return __asyncSpliterate(source, asyncBatchSpliterator, { size }); } exports.__asyncBatch = __asyncBatch; const asyncBatch = /*#__PURE__*/asyncIterableCurry(__asyncBatch, { validateArgs(args) { if (typeof args[1] !== 'number' || args[1] < 1) { throw new TypeError('batch size should be a number greater than zero'); } } }); exports.asyncBatch = asyncBatch;