Skip to content

Instantly share code, notes, and snippets.

@jckw
Created May 2, 2023 22:27
Show Gist options
  • Save jckw/6d35eed7226d4cd08b08ad322410b637 to your computer and use it in GitHub Desktop.
Save jckw/6d35eed7226d4cd08b08ad322410b637 to your computer and use it in GitHub Desktop.
Named Cartesian arrays for use with jest.
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