Comments (5)
Can you give me some more context? I've got an app that uses the heck out of JsonLogic for taxes, which I'd assume is similar. In that case the rule (stored in the database, per white-label customer) is something like:
var tax = {
"name":"Florida State Sales Tax",
"value_rule":{"*":[
0.06,
{"var":"goods"}
]}
};
var purchase = {"goods":123.45}
var total = purchase.goods + jsonLogic.apply(tax.value_rule, purchase);
(That's oversimplifying, there's usually an array of taxes and fees, and there are a bunch of subtotals and applicability that also have to be considered, but this is the gist of it.)
One of the little things I 😍 about this process is that if you need to support a fixed fee, making value_rule
a number just works, JsonLogic just passes through primitives that aren't rules.
from json-logic-js.
Thanks @jwadhams
An example of such a computation would be if the discount depends on various factors such as age of the customer, country, gender, etc (this is completely bogus by the way, the real thing is more complicated).
So maybe if age is between 18 to 30 then you get a 10% discount, 30 to 50 you get 20%.
Plus a 10% if country is USA, etc.
Of course all of it stored in database just like you, white label.
from json-logic-js.
Assuming you can define and document one big data object for them, like transaction
below, your customer could provide a rule like discounts
-- in this case the result of that rule is a percentage, which your calling code could use like the console.log commands to show pretty subtotals. (that also puts you in a position to error out if your customer's rule tries to give a 110% discount by mistake)
var jsonLogic = require('json-logic-js');
var transaction = {
total : 100,
customer : {
age : 46,
country : "US"
}
};
var discounts = {"+":[
{"if":[
{"<=":[ 18, {"var":"customer.age"}, 30 ]},
0.10,
{"<=":[ 30, {"var":"customer.age"}, 50 ]},
0.20,
0
]},
{"if":[
{"==":[ {"var":"customer.country"}, "US"]},
0.10,
0
]}
]};
var discount_pct = jsonLogic.apply(discounts, transaction);
console.log("Subtotal: $", transaction.total);
var discount_amount = transaction.total*discount_pct;
console.log("Discounts: ", discount_pct, "% = $", discount_amount);
console.log("Total: $", transaction.total - discount_amount);
from json-logic-js.
BTW, if you want to support both fixed discounts ($5 off purchases of $100 or more for example) and percentages, I think you'd have to make your customer include the "multiply percentages by total" in the rule themselves (that's how I actually use this).
from json-logic-js.
Thanks, that should be enough
from json-logic-js.
Related Issues (20)
- Access to index in "reduce" operation
- Is it possible to return "Undefined"? HOT 1
- [Question] Using var operator at nested level of array operators HOT 2
- Addition function adding unwanted precision HOT 4
- Feature Request: ability to add custom operators which are not automatically traversed for evaluation HOT 9
- Would you be interested in a code merge? (json-logic-engine)
- How to know if logic is valid? HOT 1
- Referencing data values within a map HOT 1
- Handle undefined HOT 4
- Support friendly error messages? HOT 2
- Reference a key with a dot in it HOT 1
- Subtract breaks when there is a 0 between two values HOT 3
- Example on how to persist in a relational database HOT 1
- Global variable in the iteration HOT 2
- In-place data update
- [IDEA] Callable data
- Is There a way to split String by space or Comma HOT 2
- Can't seem to manipulate data inside an 'in' statement
- "IN" Operator Fails When Accessed Array via "VAR" Operator HOT 4
- Reduce initial value doesn't get evaluated HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json-logic-js.