Git Product home page Git Product logo

Comments (5)

jwadhams avatar jwadhams commented on August 20, 2024

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.

NathanHazout avatar NathanHazout commented on August 20, 2024

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.

jwadhams avatar jwadhams commented on August 20, 2024

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.

jwadhams avatar jwadhams commented on August 20, 2024

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.

NathanHazout avatar NathanHazout commented on August 20, 2024

Thanks, that should be enough

from json-logic-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.