diff --git a/packages/reacord/helpers/generate-prop-combinations.ts b/packages/reacord/helpers/generate-prop-combinations.ts new file mode 100644 index 0000000..7ec5a97 --- /dev/null +++ b/packages/reacord/helpers/generate-prop-combinations.ts @@ -0,0 +1,21 @@ +export function generatePropCombinations

(values: { + [K in keyof P]: ReadonlyArray +}) { + return generatePropCombinationsRecursive(values) as P[] +} + +function generatePropCombinationsRecursive( + value: Record, +): Array> { + const [key] = Object.keys(value) + if (!key) return [{}] + + const { [key]: values = [], ...otherValues } = value + const result: Array> = [] + for (const value of values) { + for (const otherValue of generatePropCombinationsRecursive(otherValues)) { + result.push({ [key]: value, ...otherValue }) + } + } + return result +}