Skip to content

Instantly share code, notes, and snippets.

@jahredhope
Last active October 2, 2022 09:34
Show Gist options
  • Save jahredhope/0d338f561bf57688447686d593d2b1aa to your computer and use it in GitHub Desktop.
Save jahredhope/0d338f561bf57688447686d593d2b1aa to your computer and use it in GitHub Desktop.
Create the caresian product of TypeScript arrays
function product<K extends string, T>(arrays: Record<K, T[]>): Record<K, T>[] {
return Object.entries(arrays).reduce(
// @ts-expect-error Object.entries is resolving K to string
(acc, [field, arr]: [K, T[]]): Record<K, T>[] =>
arr.flatMap((value) =>
acc.map((existing) => ({ ...existing, [field]: value })),
),
[{}] as Record<K, T>[],
) as Record<K, T>[];
}
cartesianProduct({ a: [1, 2], b: [1, 2] }); // [{ a: 1, b: 1 }, { a: 2, b: 1 }, { a: 1, b: 2 }, { a: 2, b: 2 }]
describe('cartesian-product', () => {
test('small', () => {
const res = product({ a: [1, 2], b: [1, 2] });
console.log(res);
expect(res).toEqual([
{ a: 1, b: 1 },
{ a: 2, b: 1 },
{ a: 1, b: 2 },
{ a: 2, b: 2 },
]);
});
test('full', () => {
const res = product({ a: [1, 2], b: [3, 4], c: [5, 6] });
expect(res).toEqual([
{ a: 1, b: 3, c: 5 },
{ a: 2, b: 3, c: 5 },
{ a: 1, b: 4, c: 5 },
{ a: 2, b: 4, c: 5 },
{ a: 1, b: 3, c: 6 },
{ a: 2, b: 3, c: 6 },
{ a: 1, b: 4, c: 6 },
{ a: 2, b: 4, c: 6 },
]);
});
test('length', () => {
expect(
product({ a: [1, 2, 3], b: [3, 4, 5], c: [5, 6, 7, 8, 9] }),
).toHaveLength(45);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment