UNPKG

remeda

Version:

A utility library for JavaScript and Typescript.

70 lines (67 loc) 2.8 kB
import { C as CoercedArray } from './CoercedArray-DRz3tqda.cjs'; import { I as IterableContainer } from './IterableContainer-CtfinwiH.cjs'; import { P as PartialArray } from './PartialArray-DqgYiDUP.cjs'; import { R as RemedaTypeError } from './RemedaTypeError-BIoNlKC-.cjs'; import { T as TupleParts } from './TupleParts-Ci1vY__a.cjs'; /** * The union of all possible ways to write a tuple as [...left, ...right]. */ type TupleSplits<T extends IterableContainer> = T extends unknown ? // The complete set of all splits is the union of splitting each part of SplitPrefix<T> | SplitOptional<T> | SplitRest<T> | SplitSuffix<T> : never; type SplitPrefix<T extends IterableContainer> = FixedTupleSplits<TupleParts<T>["required"]> extends infer Req ? Req extends { left: infer Left; right: infer Right extends Array<unknown>; } ? { left: Left; right: [ ...Right, ...PartialArray<TupleParts<T>["optional"]>, ...CoercedArray<TupleParts<T>["item"]>, ...TupleParts<T>["suffix"] ]; } : RemedaTypeError<"SplitPrefix", "Unexpected result shape from FixedTupleSplits", { type: never; metadata: [Req, T]; }> : never; type SplitOptional<T extends IterableContainer> = FixedTupleSplits<TupleParts<T>["optional"]> extends infer Optional ? Optional extends { left: infer Left extends Array<unknown>; right: infer Right extends Array<unknown>; } ? { left: [...TupleParts<T>["required"], ...PartialArray<Left>]; right: [ ...PartialArray<Right>, ...CoercedArray<TupleParts<T>["item"]>, ...TupleParts<T>["suffix"] ]; } : RemedaTypeError<"SplitOptional", "Unexpected result shape from FixedTupleSplits", { type: never; metadata: [Optional, T]; }> : never; type SplitRest<T extends IterableContainer> = { left: [ ...TupleParts<T>["required"], ...PartialArray<TupleParts<T>["optional"]>, ...CoercedArray<TupleParts<T>["item"]> ]; right: [...CoercedArray<TupleParts<T>["item"]>, ...TupleParts<T>["suffix"]]; }; type SplitSuffix<T extends IterableContainer> = FixedTupleSplits<TupleParts<T>["suffix"]> extends infer Suffix ? Suffix extends { left: infer Left extends Array<unknown>; right: infer Right; } ? { left: [ ...TupleParts<T>["required"], ...PartialArray<TupleParts<T>["optional"]>, ...CoercedArray<TupleParts<T>["item"]>, ...Left ]; right: Right; } : RemedaTypeError<"SplitSuffix", "Unexpected result shape from FixedTupleSplits", { type: never; metadata: [Suffix, T]; }> : never; type FixedTupleSplits<L, R extends Array<unknown> = []> = { left: L; right: R; } | (L extends readonly [...infer Head, infer Tail] ? FixedTupleSplits<Head, [Tail, ...R]> : never); export type { TupleSplits as T };