@loadsmart/miranda-wc
Version:
Miranda Web Components component library
16 lines (15 loc) • 815 B
TypeScript
type ElementType<A> = A extends ReadonlyArray<infer T> ? T : never;
type ElementsOfAll<Inputs, R extends ReadonlyArray<unknown> = []> = Inputs extends readonly [infer F, ...infer M] ? ElementsOfAll<M, [...R, ElementType<F>]> : R;
type CartesianProduct<Inputs> = ElementsOfAll<Inputs>[];
/**
* Returns the cartesian product of one or more iterables.
*
* Based on https://stackoverflow.com/a/72059390
*
* @example
* Consider the standard playing card ranks and its suits:
* cartesianProduct([A, K, Q, J, 10, 9, 8, 7, 6, 5, 4, 3, 2], [♠, ♥, ♦, ♣]);
* The product of these iterables would be [[A, ♠], [A, ♥]...] and so on until we have all 52 cards.
*/
export declare const cartesianProduct: <Sets extends ReadonlyArray<ReadonlyArray<unknown>>>(...sets: Sets) => CartesianProduct<Sets>;
export {};