Dynamic Rules
the most fun and fast way to eval javascript objects
Usage
- Passing an object and rules, getting the result.
const dynamicRules = ; // This can be any javascript objectconst myModel = amount: 118 subtotal: 100 taxes: 18 typeDocument: name: 'INVOICE' ; // This is the config dynamic-rules needs.// model represents the parent of the objectconst config = condition: 'model.typeDocument.name === "INVOICE"' formula: '(model.subtotal * model.amount) + 10'; const result = dynamicRules; console; /* { meetCondition: true, formula: 11790 }*/
- Merging the result into your object
const dynamicRules = ; // This can be any javascript objectconst myModel = amount: 118 subtotal: 100 taxes: 18 typeDocument: name: 'INVOICE' ; // This is the config dynamic-rules needs.// model represents the parent of the objectconst config = merge: true // make a copy of your model and return the result on it. condition: 'model.typeDocument.name === "INVOICE"' formula: '(model.subtotal * model.amount) + 10'; const result = dynamicRules; console; /* { model: { amount: 118, subtotal: 100, taxes: 18, typeDocument: { name: 'INVOICE', }, } condition: true, formula: 11790 }*/