File size: 1,337 Bytes
3206347 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import get from 'lodash.get';
const variableRegExp = /({{step\.[\da-zA-Z-]+(?:\.[^.}{]+)+}})/g;
export default function computeParameters(parameters, executionSteps) {
const entries = Object.entries(parameters);
return entries.reduce((result, [key, value]) => {
if (typeof value === 'string') {
const parts = value.split(variableRegExp);
const computedValue = parts
.map((part) => {
const isVariable = part.match(variableRegExp);
if (isVariable) {
const stepIdAndKeyPath = part.replace(/{{step.|}}/g, '');
const [stepId, ...keyPaths] = stepIdAndKeyPath.split('.');
const keyPath = keyPaths.join('.');
const executionStep = executionSteps.find((executionStep) => {
return executionStep.stepId === stepId;
});
const data = executionStep?.dataOut;
const dataValue = get(data, keyPath);
return dataValue;
}
return part;
})
.join('');
return {
...result,
[key]: computedValue,
};
}
if (Array.isArray(value)) {
return {
...result,
[key]: value.map((item) => computeParameters(item, executionSteps)),
};
}
return {
...result,
[key]: value,
};
}, {});
}
|