chunk

import { chunk } from "jsr:@ende/collections";

function

function chunk<T>(iterable: Iterable<T>, size: number): T[][]

Splits the given array into an array of chunks of the given size and returns them.

iterable: Iterable<T>

The iterable to take elements from.

size: number

The size of the chunks. This must be a positive integer.

Returns

An array of chunks of the given size.

Example: Basic usage

import { chunk } from "@ende/collections/chunk";
import { assertEquals } from "@ende/assert";

const words = [
  "lorem",
  "ipsum",
  "dolor",
  "sit",
  "amet",
  "consetetur",
  "sadipscing",
];
const chunks = chunk(words, 3);

assertEquals(
  chunks,
  [
    ["lorem", "ipsum", "dolor"],
    ["sit", "amet", "consetetur"],
    ["sadipscing"],
  ],
);

Source