@workday/canvas-kit-react
Version:
The parent module that contains all Workday Canvas Kit React components
31 lines (30 loc) • 1.34 kB
JavaScript
export const permutateProps = (allProps, filter, remainingProps = Object.keys(allProps), values = {}) => {
// When there are no more props to combine with, return result
const prop = remainingProps[0];
if (!prop) {
const props = {};
Object.keys(values).forEach(prop => {
var _a;
props[prop] = (_a = values === null || values === void 0 ? void 0 : values[prop]) === null || _a === void 0 ? void 0 : _a.value;
});
if (filter && !filter(props)) {
return [];
}
const label = Object.keys(values)
.map(prop => { var _a; return (_a = values === null || values === void 0 ? void 0 : values[prop]) === null || _a === void 0 ? void 0 : _a.label; })
.join(' ');
return [
{
label,
props,
},
];
}
const possiblePropValues = allProps[prop];
const permutations = possiblePropValues === null || possiblePropValues === void 0 ? void 0 : possiblePropValues.map((value) => {
const newValues = { ...values }; // Required so we don't overwrite previous references
newValues[prop] = value;
return permutateProps(allProps, filter, remainingProps.slice(1, remainingProps.length), newValues);
});
return permutations.flat();
};