in client/src/components/special/cellprofiler/model/modules/parse-module-configuration.js [213:289]
function generateCondition (parts) {
if (typeof parts === 'object' && parts.open === '(') {
return generateCondition(parts.parts || []);
}
if (typeof parts !== 'object' || !Array.isArray(parts) || !parts.length) {
return () => true;
}
const criterias = [];
const logicalOperators = [];
let idx = 0;
while (idx < parts.length) {
const operand = parts[idx];
if (typeof operand === 'string') {
// someProperty == someValue
const operator = parts[idx + 1];
const value = parseValue(parts[idx + 2]);
if (!/(===|!==|==|!=|=|<=|>|<)/i.test(operator) || value === undefined) {
console.log('ERROR', operator, value, {operand, operator, value: parts[idx + 2]}, idx, parts);
break;
}
criterias.push((cpModule) => {
const parameterValue = cpModule.getParameterValue(operand);
switch (operator) {
case '===':
case '==':
return parameterValue === value;
case '!==':
case '!=':
return parameterValue !== value;
case '>':
return parameterValue > value;
case '<':
return parameterValue < value;
case '>=':
return parameterValue > value;
case '<=':
return parameterValue < value;
default:
return true;
}
});
idx += 3;
} else if (typeof operand === 'object' && operand.open === '(') {
criterias.push(generateCondition(operand));
idx += 1;
}
if (idx < parts.length) {
const logical = parts[idx];
if (!/(and|or)/i.test(logical)) {
break;
}
logicalOperators.push(logical);
}
idx += 1;
}
if (criterias.length - 1 === logicalOperators.length) {
return (cpModule) => {
let result = criterias[0](cpModule);
for (let l = 0; l < logicalOperators.length; l += 1) {
const next = criterias[l + 1];
const logical = logicalOperators[l];
switch (logical.toLowerCase().trim()) {
case 'and':
result = result && next(cpModule);
break;
case 'or':
result = result || next(cpModule);
break;
default:
break;
}
}
return result;
};
}
return () => true;
}