jwadhams / json-logic-js

Build complex rules, serialize them as JSON, and execute them in JavaScript
MIT License
1.26k stars 140 forks source link

[question] how to chain rules? #43

Closed tuananh closed 5 years ago

tuananh commented 6 years ago

eg. I want to have a set of rules and have data running through all of them.

if i pass data as {a: 1, b: 2, price: 100}, price will be 600.

How can i do this with json-logic?

ivan133 commented 5 years ago

You can implement something like set-temp

JSONLogic.add_operation('set_temp', function(key, value){
  if ( ! this.temp_variables){
    this.temp_variables = {}; 
  }
  this.temp_variables[key] = value;
  console.log('temp value', value);
  return value;
});

JSONLogic.add_operation('get_temp', function(key){
  if ( ! this.temp_variables){
    this.temp_variables = {}; 
  }
  return this.temp_variables[key];
});

And use get_temp on the next step