json-logic-js

repository·master·Indexed 23 days ago

https://github.com/jwadhams/json-logic-js

A JavaScript implementation of the JsonLogic format (version 2.0.5) used to build complex rules, serialize them as JSON, and execute them in JavaScript. It allows logic rules to be shared between front-end and back-end environments using the jsonLogic.apply(rule, data) method.

Tokens
863
Snippets
6
Records
8
Agent score
31%

What's inside json-logic-js

  1. Handle 'Always' and 'Never' rules

    master

    If the first parameter passed to jsonLogic.apply() is a non-object and a non-associative array (e.g., a boolean), the value is returned immediately without processing data.

    // Always
    jsonLogic.apply(true, data_will_be_ignored);
    // true
    
    // Never
    jsonLogic.apply(false, i_wasnt_even_supposed_to_be_here);
    // false
  2. Download logic.js manually

    master

    If you prefer to manage updates yourself or are using a project that requires a self-contained file, you can download logic.js directly. The library uses a module loader that also makes it compatible with RequireJS projects.

    curl -O https://raw.githubusercontent.com/jwadhams/json-logic-js/master/logic.js
  3. Use data-driven rules with the 'var' operator

    master

    To make rules dynamic, pass a data object as the second argument to jsonLogic.apply(). Use the var operator to retrieve values from the data object.

    Accessing object attributes:

    jsonLogic.apply(
      { "var" : ["a"] }, // Rule
      { a : 1, b : 2 }     // Data
    );
    // 1

    Syntactic sugar for unary operators: You can skip the array wrapper for unary operators like var:

    jsonLogic.apply(
      { "var" : "a" },
      { a : 1, b : 2 }
    );
    // 1

    Accessing array indices: Use var with a numeric index to access elements in an array:

    jsonLogic.apply(
      {"var" : 1 },
      [ "apple", "banana", "carrot" ]
    );
    // "banana"

    Complex mixed rule example:

    var rules = { "and" : [
      {"<" : [ { "var" : "temp" }, 110 ]},
      {"==" : [ { "var" : "pie.filling" }, "apple" ] }
    ] };
    
    var data = { "temp" : 100, "pie" : { "filling" : "apple" } };
    
    jsonLogic.apply(rules, data);
    // true
    jsonLogic.apply(
      { "var" : ["a"] },
      { a : 1, b : 2 }
    );
  4. Execute JsonLogic rules with jsonLogic.apply()

    master

    The primary way to use the library is via jsonLogic.apply(rule, data).

    Rule Format Rules:

    1. The operator is always the key in the rule object.
    2. There is only one key per rule.
    3. Values are typically provided in an array.
    4. Values can be strings, numbers, booleans, arrays (non-associative), or null.

    Simple Rule Example:

    jsonLogic.apply( { "==" : [1, 1] } );
    // true
    jsonLogic.apply( { "==" : [1, 1] } );
  5. Execute JsonLogic rules with apply()

    master

    The primary way to use json-logic-js is by calling jsonLogic.apply(logic, data). This function takes a JSON logic object (the rule) and a data object, then evaluates the rule against that data.

    Rules can be simple objects like `{"==