I'm working in a very simple rules engine that evaluates a series of conditions consisting in an operation and an expected value. The rules are only valid when all conditions in the array are true.
My code (it currently works, actually) looks similar to the following, I just simplified the function bodies to make the question more concise:
const isA = (p1, p2, p3) => {
return false; // Simplified function body, real thing has some logic
};
const isB = (p1, p2, p3) => {
return false;
};
const getC = (p1, p2, p3) => {
return 0;
};
const getD = (p1, p2, p3) => {
return "string";
};
const rule = {
conditions: [
{ operation: isA, expectedValue: false },
{ operation: isB, expectedValue: false },
{ operation: getC, expectedValue: 0 },
{ operation: getD, expectedValue: "string" }
]
};
function isConditionMet(condition) {
return (
condition.operation(this.p1, this.p2, this.p3) ===
condition.expectedValue
);
}
export const areRuleConditionsMet = (p1, p2, p3) => {
return rule.conditions.every(isConditionMet, { p1, p2, p3 });
}
Basically, p1
, p2
and p3
are used within the operations to generate an actual value to be compared against the expected value to determine if each individual condition is met and if every of them are met, then the exported function would return true
.
What I would like to do is changing my code so that isConditionMet
can become an Arrow Function to follow the structure of the rest of the code and also avoid using this
but my problem is that, obviously, I depend on the thisArg
of Array.prototype.every()
to propagate the arguments of areRuleConditionsMet
to each operation. Any Ideas?