Implement execute() in src/rules.ts to satisfy all the test cases in src/rules.test.ts.
The function should take a rule object and a facts object, and return a boolean indicating whether the facts satisfy the rule.
There are three types of rules:
- Condition Rule: A simple rule that compares a value from the facts with a specified value using an operator.
- All Rule: A rule that returns true only if all of its child rules are true (logical AND).
- Any Rule: A rule that returns true if any of its child rules are true (logical OR).
const rule = {
type: "condition",
path: "age",
operator: "gte", // greater than or equal
value: 18,
};
const facts = {
age: 21,
};
// Should return true because 21 >= 18
execute(rule, facts);const rule = {
type: "condition",
path: "user.profile.age",
operator: "gte",
value: 18,
};
const facts = {
user: {
profile: {
age: 21,
},
},
};
// Should return true
execute(rule, facts);const rule = {
type: "all",
all: [
{ type: "condition", path: "age", operator: "gte", value: 18 },
{ type: "condition", path: "hasConsent", operator: "eq", value: true },
],
};
const facts = {
age: 21,
hasConsent: true,
};
// Should return true because both conditions are true
execute(rule, facts);const rule = {
type: "any",
any: [
{ type: "condition", path: "isMember", operator: "eq", value: true },
{ type: "condition", path: "isAdmin", operator: "eq", value: true },
],
};
const facts = {
isMember: false,
isAdmin: true,
};
// Should return true because at least one condition is true
execute(rule, facts);eq: Equal tonotEq: Not equal togt: Greater thangte: Greater than or equal tolt: Less thanlte: Less than or equal toin: Value is in an arraynotIn: Value is not in an array
Good luck!