Skip to content

Latest commit

 

History

11 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rules Engine Coding Challenge

Your Task

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.

Rule Types

There are three types of rules:

  1. Condition Rule: A simple rule that compares a value from the facts with a specified value using an operator.
  2. All Rule: A rule that returns true only if all of its child rules are true (logical AND).
  3. Any Rule: A rule that returns true if any of its child rules are true (logical OR).

Examples

Simple Condition Rule

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);

Nested Path Access

const rule = {
  type: "condition",
  path: "user.profile.age",
  operator: "gte",
  value: 18,
};

const facts = {
  user: {
    profile: {
      age: 21,
    },
  },
};

// Should return true
execute(rule, facts);

Logical AND (All)

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);

Logical OR (Any)

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);

Available Operators

  • eq: Equal to
  • notEq: Not equal to
  • gt: Greater than
  • gte: Greater than or equal to
  • lt: Less than
  • lte: Less than or equal to
  • in: Value is in an array
  • notIn: Value is not in an array

Good luck!

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages