So I'm trying to calculate the cartesian product of object's properties
const style = {
fontFamily: ['helvetica', 'comic sans'],
fontWeight: [300, 700],
}
const expected = [
{ fontFamily: 'helvetica', fontWeight: 300 },
{ fontFamily: 'helvetica', fontWeight: 700 },
{ fontFamily: 'comic sans', fontWeight: 300 },
{ fontFamily: 'comic sans', fontWeight: 700 },
]
I have an algorithm that does that all nice and stuff. Then I realized I have to actually compute the cartesian property of TWO (or more) objects' properties (with the same keys in each object), returning an array of objects
const style = {
foo: {
fontFamily: ['helvetica', 'comic sans'],
fontWeight: [300, 700],
}
bar: {
fontFamily: ['gotham', 'proxima nova'],
fontWeight: [300, 700],
}
}
const expected = [
{ foo: { fontFamily: 'helvetica', fontWeight: 300 },
bar: { fontFamily: 'gotham', fontWeight: 300 },
},
{ foo: { fontFamily: 'helvetica', fontWeight: 300 },
bar: { fontFamily: 'gotham', fontWeight: 700 },
},
{ foo: { fontFamily: 'helvetica', fontWeight: 700 },
bar: { fontFamily: 'gotham', fontWeight: 300 },
},
{ foo: { fontFamily: 'helvetica', fontWeight: 700 },
bar: { fontFamily: 'gotham', fontWeight: 700 },
},
… … …
]
The product solver works fine; I was just trying to figure out how to make it work for deeply nested properties; easiest thing I could think of was to transform the input into a flattened object as seen in the test cases, then solve it like before, then unflatten it back into the expected structure.
With that said, do my functions look ok? They're the cleanest way I could thing of implementing it…