dynamic-rules
v1.0.1
Published
evaluate javascript code according conditions and formulas
Downloads
1
Readme
Dynamic Rules
the most fun and fast way to eval javascript objects
Usage
- Passing an object and rules, getting the result.
const dynamicRules = require('dynamic-rules');
// This can be any javascript object
const myModel = {
amount: 118,
subtotal: 100,
taxes: 18,
typeDocument: {
name: 'INVOICE',
},
};
// This is the config dynamic-rules needs.
// model represents the parent of the object
const config = {
condition: 'model.typeDocument.name === "INVOICE"',
formula: '(model.subtotal * model.amount) + 10',
};
const result = dynamicRules.execute(myModel, config);
console.log(result);
/*
{
meetCondition: true,
formula: 11790
}
*/
- Merging the result into your object
const dynamicRules = require('dynamic-rules');
// This can be any javascript object
const myModel = {
amount: 118,
subtotal: 100,
taxes: 18,
typeDocument: {
name: 'INVOICE',
},
};
// This is the config dynamic-rules needs.
// model represents the parent of the object
const 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.execute(myModel, config);
console.log(result);
/*
{
model: {
amount: 118,
subtotal: 100,
taxes: 18,
typeDocument: {
name: 'INVOICE',
},
}
condition: true,
formula: 11790
}
*/