Created
May 2, 2023 22:27
-
-
Save jckw/6d35eed7226d4cd08b08ad322410b637 to your computer and use it in GitHub Desktop.
Named Cartesian arrays for use with jest.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cartesian from "fast-cartesian" | |
type ArrayToUnion<T extends readonly unknown[]> = T[number] | |
type ObjectValuesToUnion<T extends Record<string, readonly unknown[]>> = { | |
[K in keyof T]: ArrayToUnion<T[K]> | |
} | |
const namedCartesian = <T extends Record<string, readonly unknown[]>>( | |
fields: T, | |
) => | |
cartesian(Object.values(fields)).map( | |
(values) => | |
Object.fromEntries( | |
Object.entries(fields).map(([k], i) => [k, values[i]]), | |
) as ObjectValuesToUnion<T>, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment