std/collections/chunk.ts

57 lines
1.3 KiB
TypeScript
Raw Normal View History

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
/**
2022-11-25 11:40:23 +00:00
* Splits the given array into chunks of the given size and returns them.
*
* @typeParam T Type of the elements in the input array.
*
* @param array The array to split into chunks.
* @param size The size of the chunks. This must be a positive integer.
*
* @returns An array of chunks of the given size.
*
* @example Basic usage
* ```ts
* import { chunk } from "@std/collections/chunk";
* import { assertEquals } from "@std/assert";
*
2022-11-25 11:40:23 +00:00
* const words = [
* "lorem",
* "ipsum",
* "dolor",
* "sit",
* "amet",
* "consetetur",
* "sadipscing",
* ];
* const chunks = chunk(words, 3);
*
2022-11-25 11:40:23 +00:00
* assertEquals(
* chunks,
* [
* ["lorem", "ipsum", "dolor"],
* ["sit", "amet", "consetetur"],
* ["sadipscing"],
* ],
* );
* ```
*/
export function chunk<T>(array: readonly T[], size: number): T[][] {
if (size <= 0 || !Number.isInteger(size)) {
throw new RangeError(
2021-09-07 07:40:05 +00:00
`Expected size to be an integer greater than 0 but found ${size}`,
);
}
const result: T[][] = [];
let index = 0;
while (index < array.length) {
result.push(array.slice(index, index + size));
index += size;
}
return result;
}