Skip to main content
Module

x/fonction/mod.ts>chunk

A modern practical functional library
Latest
variable chunk
import { chunk } from "https://deno.land/x/fonction@v2.1.0-beta.4/mod.ts";

Return an array of elements split into groups the length of size.

Examples

Example 1

// Basic
chunk(1, ['a', 'b', 'c', 'd']) // [['a'], ['b'], ['c'], ['d']]
chunk(3, ['a', 'b', 'c', 'd']) // [['a', 'b', 'c'], ['d']]
chunk(5, ['a', 'b', 'c', 'd']) // [['a', 'b', 'c', 'd']]

Example 2

// Illegal size
chunk(0, ['a', 'b', 'c']) // ['a', 'b', 'c']
chunk(-3, ['a', 'b', 'c']) // ['a', 'b', 'c']
chunk(5, []) // []

type

<T extends number, U extends readonly unknown[]>(size: T, array: U) => T extends 0 ? U : `${T}` extends `-${number}` ? U : U extends readonly [] ? U : U extends readonly (infer R)[] ? R[][] : never