Git Product home page Git Product logo

Comments (3)

jwadhams avatar jwadhams commented on August 20, 2024

I think you just have a JavaScript problem, at least trying to break this down in a Node REPL.

Just the match part of the rule:

jsonLogic.apply(
   {"method":[{"var":"a"},"match",["(.)(.)(.)?(.)"]]},
   {"a": "abc"}
);

Returns:

[ 'abc', 'a', 'b', undefined, 'c', index: 0, input: 'abc' ]

So the slice returns [ undefined ]. So you need a solution where undefined is not in an array (because == and === don't work on arrays unless they're literally references to the same memory location), and where it's probably not strictly compared to null. (I assume you're using null in the rule because it's valid JSON while undefined isn't.)

This rule returns true:

jsonLogic.apply(
    {"==":[
        null,
        {"method":[
            {"method":[
                {"method":[
                    {"var":"a"},
                    "match",
                    ["(.)(.)(.)?(.)"]
                ]},
                "slice",
                [3,4]
            ]},
            "shift"
        ]}
    ]},
    {"a": "abc"}
);

But that's kind of janky, it's like "abc".match().slice().shift() when what you really want is "abc".match()[3]

I don't think JavaScript has a cleaner method to get data by index, and JsonLogic doesn't have one either, as you note, except on data, but you could add one:

jsonLogic.add_operation("index", function(a, b){
    return a[b];
});

jsonLogic.apply(
    {"==":[
        null,
        {"index":[
            {"method":[
                {"var":"a"},
                "match",
                ["(.)(.)(.)?(.)"]
            ]},
            3
        ]}
    ]},
    {"a": "abc"}
);

You'll need to think through how your index function works, that one throws an exception if a is null, which is to say if the match fails. But my next attempt returned something falsy if a is null or undefined, made the rule as a whole return true if the match fails.

from json-logic-js.

jwadhams avatar jwadhams commented on August 20, 2024

Really, what I'd want to do with that regex in a real language is assign the result to a temp variable and then run a few tests on it.

This adds two methods that let your rule set temp data in a namespace that can't overwrite your other data or otherwise escape.

JsonLogic isn't really set up to run rules sequentially, so we store the value in the first clause of theand rule then retrieve it in the second (and potentially 3rd...Nth).

jsonLogic.add_operation("set_temp", function(key, value){
    if( ! this.temp_variables){ this.temp_variables = {}; }
    this.temp_variables[key] = value;
    return value;
});

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

jsonLogic.add_operation("index", function(a, b){
    if(typeof a === 'undefined' || a === null){
        return undefined;
    }
    return a[b];
});

var rule = {"and":[
    {'!=':[
        null,
        {"set_temp":[
            "match_result",
            {"method":[
                {"var":"a"},
                "match",
                ["(.)(.)(.)?(.)"]
            ]}
        ]}
    ]},
    {"==":[
        null,
        {"index":[
            {"get_temp":"match_result"},
            3
        ]}
    ]}
]};

jsonLogic.apply(rule, {"a": "ab"}); //false, match fails
jsonLogic.apply(rule, {"a": "abc"}); //true, match succeeds, optional capture missing
jsonLogic.apply(rule, {"a": "abcd"}); //false, match succeeds, optional capture present

from json-logic-js.

deinspanjer avatar deinspanjer commented on August 20, 2024

Wow! Thank you very much for the follow-up, I had not thought about setting and getting variables within the logic tree like that.

I did add an operation for getting an array element by index and a custom match function which made things a lot easier than all the splice shift madness. :)

I'll definitely keep this trick handy too though.

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.