Skip to content

Verdict is a lightweight, single file, extensible JavaScript library for evaluating complex conditions against data objects. It uses a syntax similar to MongoDB’s query language, allowing you to perform comparisons, logical operations, and even arithmetic evaluations in a clear, declarative way

License

Notifications You must be signed in to change notification settings

freakynit/Verdict

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Verdict

Verdict is a lightweight, single file, extensible JavaScript library for evaluating complex conditions against data objects. It uses a syntax similar to MongoDB’s query language, allowing you to perform comparisons, logical operations, and even arithmetic evaluations in a clear, declarative way.


Features

  • Field-based Conditions: Access nested properties using dot notation.
  • Comparators: Support for $eq, $ne, $gt, $gte, $lt, $lte, $in, $nin, $contains.
  • Logical Operators: Combine conditions using $and, $or, and $not.
  • Arithmetic Expressions: Evaluate expressions with $add, $subtract, $multiply, $divide, $mod, $exp.
  • Extensible: Easily add custom comparators, logical operators, and arithmetic operators.
  • Robust Error Handling: Clear error messages for invalid conditions or expressions.

Installation

Just copy one file: Verdict.js in your project and start using as show below.


Usage

Basic Example

const Verdict = require('./Verdict');

const executor = new Verdict();

const data = {
   metadata: {
      user_plan: "paid",
      user_age: 25,
      name: "John Doe"
   },
   stats: {
      score: 85
   }
};

const condition = {
   "$and": [
      { "metadata.user_age": { "$gt": 18 } },
      { "stats.score": { "$gte": 80 } }
   ]
};

const result = executor.evaluate(data, condition);
console.log(result); // Output: true

Arithmetic Expression Example

const condition = {
    "stats.score": {
        "$gt": {
            "$add": [50, 30]
        }
    }
};

console.log(executor.evaluate(data, condition)); // Output: true

Extending the Executor

Adding a Custom Comparator

executor.addComparator('$between', (value, range) => {
    if (!Array.isArray(range) || range.length !== 2) {
        throw new Error('$between operator requires an array with exactly two elements.');
    }
    return value >= range[0] && value <= range[1];
});

const betweenCondition = {
    "metadata.user_age": { "$between": [20, 30] }
};

console.log(executor.evaluate(data, betweenCondition)); // Output: true

Adding a Custom Arithmetic Operator

// Add a $square operator that squares a single operand.
executor.addArithmeticOperator('$square', (a) => a * a);

const expr = { "$square": [5] };
console.log(executor.evaluateExpression(expr)); // Output: 25

More Usage Examples

Check out Verdict.test.js


Running Tests

This project uses Jest for testing. To run the tests:

  1. Clone the repository.

  2. Install the dependencies:

    npm install
  3. Run the tests:

    npm test

Contributing

Contributions, feedback, and feature requests are welcome! Feel free to fork the repository, submit pull requests, or open issues to help the project improve.


License

This project is licensed under the MIT License. See the LICENSE file for more details.

About

Verdict is a lightweight, single file, extensible JavaScript library for evaluating complex conditions against data objects. It uses a syntax similar to MongoDB’s query language, allowing you to perform comparisons, logical operations, and even arithmetic evaluations in a clear, declarative way

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published