Class Batch<T>

Source
Expand description

Represents a batch of items.

for of

import { Batch } from '../src/index.js';

console.log(Array.from(Batch.from([1, 2, 3, 4, 5], 3)));

for await of

import { Batch } from '../src/index.js';

async function* getSource() {
for (let i = 0; i < 5; i++) {
await new Promise((resolve) => setTimeout(resolve));
yield i;
}
}

console.log(await Array.fromAsync(Batch.fromAsync(getSource(), 3)));

Predicate:

import { Batch } from '../src/index.js';

console.log(Array.from(Batch.from([1, 2, 3, 4, 5], 2, (v) => v % 2 === 0)));

Chaining:

import { Batch } from '../src/index.js';

Batch.from([1, 2, 3, 4, 5], 3).forEach((batch) => {
console.log(batch);
});

Constructors§

Source§

new Batch<T>(items: readonly T[], index: undefined | number): Batch<T>

Properties§

Source§

public readonly index: undefined | number

Source§

public readonly items: readonly T[]

Accessors§

Source§

get length(): number

Methods§

Source§

from<T>(
    iterable: Iterable<T>,
    size: number,
    predicate?: (value: T, index: number) => boolean,
): Generator<Batch<T>, undefined>

Source§

fromAsync<T>(
    iterable: Iterable<T, any, any> | AsyncIterable<T, any, any>,
    size: number,
    predicate?: (value: T, index: number) => boolean,
): AsyncGenerator<Batch<T>, undefined>