import { IsNumericLiteral, LessThan, IfNever, ValueOf, Subtract, IntRange } from 'type-fest'; import { I as IntRangeInclusive } from './IntRangeInclusive-Cn-qsrAN.cjs'; import { I as IterableContainer } from './IterableContainer-CtfinwiH.cjs'; import { N as NonEmptyArray } from './NonEmptyArray-C9Od1wmF.cjs'; import { N as NTuple } from './NTuple-BgsZT9dJ.cjs'; import { T as TupleParts } from './TupleParts-CP0H7BrE.cjs'; type MAX_LITERAL_SIZE = 350; type Chunk = T extends readonly [] ? [] : IsNumericLiteral extends true ? LessThan extends true ? never : LessThan extends true ? [ ...LiteralChunk ] : GenericChunk : GenericChunk; type LiteralChunk = TupleParts> = ChunkInfinite, Parts["item"], Parts["suffix"], N> | ([...Parts["prefix"], ...Parts["suffix"]]["length"] extends 0 ? [] : never); /** * This type **only** works if the input array `T` is a finite tuple ( * `[number, "abc", true, { a: "hello" }]` etc..., and it doesn't contain a rest * parameter). For these inputs the chunked output could be computed as literal * finite tuples too. */ type ChunkFinite = T extends readonly [infer Head, ...infer Rest] ? ChunkFinite>, infer Current extends Array ] ? Current["length"] extends N ? [ ...Previous, Current, [Head] ] : [ ...Previous, [...Current, Head] ] : [ [Head] ]> : Result; /** * Here lies the main complexity of building the chunk type. It takes the prefix * chunks, the rest param item type, and the suffix (not chunked!) and it * creates all possible combinations of adding items to the prefix and suffix * for all possible scenarios for how many items the rest param "represents". */ type ChunkInfinite, N extends number> = IfNever>, infer LastPrefixChunk extends Array ] ? ValueOf<{ [Padding in IntRangeInclusive<0, Subtract>]: [ ...PrefixFullChunks, ...ChunkFinite<[ ...LastPrefixChunk, ...NTuple, ...Suffix ], N> ]; }> | [ ...PrefixFullChunks, [ ...LastPrefixChunk, ...NTuple> ], ...Array>, ...SuffixChunk ] : [ ...Array>, ...SuffixChunk ]>; /** * This type assumes it takes a finite tuple that represents the suffix of our * input array. It builds all possible combinations of adding items to the * **head** of the suffix in order to pad the suffix until the last chunk is * full. */ type SuffixChunk, Item, N extends number> = T extends readonly [] ? [ ValueOf<{ [K in IntRangeInclusive<1, N>]: NTuple; }> ] : ValueOf<{ [Padding in IntRange<0, N>]: ChunkFinite<[ ...NTuple, ...T ], N>; }>; /** * This is the legacy type used when we don't know what N is. We can only adjust * our output based on if we know for sure that the array is empty or not. */ type GenericChunk = T extends readonly [...Array, unknown] | readonly [unknown, ...Array] ? NonEmptyArray> : Array>; /** * Split an array into groups the length of `size`. If `array` can't be split evenly, the final chunk will be the remaining elements. * * @param array - The array. * @param size - The length of the chunk. * @signature * R.chunk(array, size) * @example * R.chunk(['a', 'b', 'c', 'd'], 2) // => [['a', 'b'], ['c', 'd']] * R.chunk(['a', 'b', 'c', 'd'], 3) // => [['a', 'b', 'c'], ['d']] * @dataFirst * @category Array */ declare function chunk(array: T, size: N): Chunk; /** * Split an array into groups the length of `size`. If `array` can't be split evenly, the final chunk will be the remaining elements. * * @param size - The length of the chunk. * @signature * R.chunk(size)(array) * @example * R.chunk(2)(['a', 'b', 'c', 'd']) // => [['a', 'b'], ['c', 'd']] * R.chunk(3)(['a', 'b', 'c', 'd']) // => [['a', 'b', 'c'], ['d']] * @dataLast * @category Array */ declare function chunk(size: N): (array: T) => Chunk; export { chunk };