TreasureTrails/node_modules/remeda/dist/chunk.cjs.map

1 line
11 KiB
Text
Raw Normal View History

2026-03-18 09:02:21 -05:00
{"version":3,"file":"chunk.cjs","names":["purry"],"sources":["../src/chunk.ts"],"sourcesContent":["import type {\n IntRange,\n IsNever,\n IsNumericLiteral,\n LessThan,\n Subtract,\n ValueOf,\n} from \"type-fest\";\nimport type { IntRangeInclusive } from \"./internal/types/IntRangeInclusive\";\nimport type { IterableContainer } from \"./internal/types/IterableContainer\";\nimport type { NTuple } from \"./internal/types/NTuple\";\nimport type { NonEmptyArray } from \"./internal/types/NonEmptyArray\";\nimport type { PartialArray } from \"./internal/types/PartialArray\";\nimport type { TupleParts } from \"./internal/types/TupleParts\";\nimport { purry } from \"./purry\";\n\n// This prevents typescript from failing on complex arrays and large chunks. It\n// allows the typing to remain useful even when very large chunks are needed,\n// without loosing fidelity on smaller ones. It was chosen by trial-and-error,\n// and given some more wiggle room because the complexity of the array also\n// plays a role in when typescript fails to recurse.\n// See the type tests for an example.\ntype MAX_LITERAL_SIZE = 350;\n\ntype Chunk<\n T extends IterableContainer,\n N extends number,\n> = T extends readonly []\n ? []\n : IsNumericLiteral<N> extends true\n ? LessThan<N, 1> extends true\n ? never\n : LessThan<N, MAX_LITERAL_SIZE> extends true\n ? // The spread here is used as a form of \"Simplify\" for arrays; without\n // it our return type isn't useful.\n [...LiteralChunk<T, N>]\n : GenericChunk<T>\n : GenericChunk<T>;\n\ntype LiteralChunk<T extends IterableContainer, N extends number> =\n | ChunkRestElement<\n // Our result will always have the prefix tuple chunked the same way, so\n // we compute it once here and send it to the main logic below\n ChunkFixedTuple<TuplePrefix<T>, N>,\n TupleParts<T>[\"item\"],\n TupleParts<T>[\"suffix\"],\n N\n >\n // If both the prefix and suffix tuples are empty then our input is a simple\n // array of the form `Item[]`. This means it could also be empty, so we\n // need to add the empty output to our return type.\n | ([...TuplePrefix<T>, ...TupleParts<T>[\"suffix\"]] extends readonly []\n ? []\n : never);\n\n/**\n * This type **only** works if the input array `T` is a fixed tuple. For these\n * inputs the chunked output could be computed as literal finite tuples too.\n */\ntype ChunkFixedTuple<\n T,\n N extends number,\n // Important! Result is initialized with an empty array (and not `[[]]`)\n // because the result of `chunk` on an empty array is `[]` and not `[[]]`.\n Result = [],\n> = T extends readonly [infer Head, ...infer Rest]\n ? // We continue consuming the input tuple recursively item by item.\n ChunkFixedTuple<\n Rest,\n N,\n Result extends [\n ...infer Previous extends unknown[][],\n infer Current extends unknown[],\n ]\n ? // We take a look at the last chunk in the result, this is the\n // \"current\" chunk where new items would be added, all chunks before\n // it are already full.\n Current[\"length\"] extends N\n ? // The current chunk is full, create a new chunk and put Head in it.\n [...Previous, Current, [Head]]\n : // The current chunk is not full yet, so we add Head to it.\n [...Previous, [...Current, Head]]\n : // This would only happen on the first iteration, when result is\n // still empty. In this case we create the first chunk and put Head\n // in it.\n [[Head]]\n >\n : // We know T is a finite tuple, so the only case where we would reach this\n // is when T is empty, and in that case our results array contains the whole\n // input chunked by N.\n Result;\n\n/**\n * Here lies the main complexity of building the chunk type. It takes the prefix\n * chunks, the rest param item type, and the suffix (not chunked!) and it\n * creates all possible combinations of adding items to the prefix and suffix\n * for all possibl