Commit 523af095 authored by MARCO Jonathan's avatar MARCO Jonathan
Browse files

️ Create a function to share the code for condition translation

parent 7a3ff89e
......@@ -76,6 +76,30 @@ const insertTranslation = (
return i;
};
const conditionsCheckOneByOne = (
conditions: FieldCondition[],
i: number,
parentArray: ConditionTranslated[],
) => {
if (conditions[i].type === 'item' && conditions[i].value === '(') {
i = subConditionInsert(conditions, i, parentArray);
} else if (conditionTest(conditions, i)) {
i = insertTranslation(conditions, i, parentArray);
} else if (conditionWithSubConditionAfter(conditions, i)) {
const subConditionArray: ConditionTranslated[] = [];
const subConditionIFinal = subConditionInsert(conditions, i + 2, subConditionArray);
parentArray.push({
fieldA: {id: <string>conditions[i].id, value: <string>conditions[i].value},
operator: <'&&' | '||'>conditions[i + 1].value,
fieldB: {id: <string>subConditionArray[0]?.child?.id, value: 'child'},
});
parentArray.push(subConditionArray[0]);
i = subConditionIFinal;
}
return i;
};
/**
* Translates the sub condition and returns this with the index.
*
......@@ -96,21 +120,7 @@ function translateSubCondition(
break;
}
if (conditions[i].type === 'item' && conditions[i].value === '(') {
i = subConditionInsert(conditions, i, subConditions);
} else if (conditionTest(conditions, i)) {
i = insertTranslation(conditions, i, subConditions);
} else if (conditionWithSubConditionAfter(conditions, i)) {
const subConditionArray: ConditionTranslated[] = [];
const subConditionIFinal = subConditionInsert(conditions, i + 2, subConditionArray);
subConditions.push({
fieldA: {id: <string>conditions[i].id, value: <string>conditions[i].value},
operator: <'&&' | '||'>conditions[i + 1].value,
fieldB: {id: <string>subConditionArray[0]?.child?.id, value: 'child'},
});
subConditions.push(subConditionArray[0]);
i = subConditionIFinal;
}
i = conditionsCheckOneByOne(conditions, i, subConditions);
}
return {subConditions, i: finalIndex};
......@@ -191,21 +201,7 @@ class ConditionService {
});
} else {
for (let i = 0; i < conditions.length; i++) {
if (conditions[i].type === 'item' && conditions[i].value === '(') {
i = subConditionInsert(conditions, i, translation);
} else if (conditionTest(conditions, i)) {
i = insertTranslation(conditions, i, translation);
} else if (conditionWithSubConditionAfter(conditions, i)) {
const subConditionArray: ConditionTranslated[] = [];
const subConditionIFinal = subConditionInsert(conditions, i + 2, subConditionArray);
translation.push({
fieldA: {id: <string>conditions[i].id, value: <string>conditions[i].value},
operator: <'&&' | '||'>conditions[i + 1].value,
fieldB: {id: <string>subConditionArray[0]?.child?.id, value: 'child'},
});
translation.push(subConditionArray[0]);
i = subConditionIFinal;
}
i = conditionsCheckOneByOne(conditions, i, translation,);
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment