typedash
Version:
modern, type-safe collection of utility functions
1 lines • 1.07 kB
Source Map (JSON)
{"version":3,"file":"chunk-DiKDW-kV.cjs","names":[],"sources":["../src/functions/chunk/chunk.ts"],"sourcesContent":["import type { Maybe } from '../../types';\n\n/**\n * Splits an array into chunks of the given size.\n * @param array The array to split.\n * @param size The maximum size of each chunk.\n * @returns An array of arrays where each sub-array has at most `size` elements.\n * @example\n * ```ts\n * chunk([1, 2, 3, 4, 5], 2) // [[1, 2], [3, 4], [5]]\n * ```\n */\nexport function chunk<T>(array: Maybe<readonly T[]>, size: number): T[][] {\n if (array == null) {\n return [];\n }\n\n const chunks = [];\n for (let index = 0; index < array.length; index += size) {\n chunks.push(array.slice(index, index + size));\n }\n\n return chunks;\n}\n\n/**\n * Alias for {@link chunk}.\n */\nexport const chunkArray = chunk;\n"],"mappings":";;;;;;;;;;;;AAYA,SAAgB,MAAS,OAA4B,MAAqB;AACxE,KAAI,SAAS,KACX,QAAO,EAAE;CAGX,MAAM,SAAS,EAAE;AACjB,MAAK,IAAI,QAAQ,GAAG,QAAQ,MAAM,QAAQ,SAAS,KACjD,QAAO,KAAK,MAAM,MAAM,OAAO,QAAQ,KAAK,CAAC;AAG/C,QAAO;;;;;AAMT,MAAa,aAAa"}