Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Recommendation] Support Handlebars-Esque Data Traversal #91

Closed
TotalTechGeek opened this issue Oct 28, 2020 · 5 comments
Closed

[Recommendation] Support Handlebars-Esque Data Traversal #91

TotalTechGeek opened this issue Oct 28, 2020 · 5 comments

Comments

@TotalTechGeek
Copy link

Hello,

I'm creating an "issue" to propose a recommendation & get your feedback. If you approve of the concept, I could probably follow through implementing it & create a PR.

In Handlebars, when you traverse down datasets, you can access data from the previous layer by utilizing ../

{{#each people}}
    {{../prefix}} {{firstname}} 
{{/each}}

JSONLogic currently scopes the data strictly to the object you're performing an operation on, somewhat limiting its functionality (if my understanding is incorrect, I apologize).

Therefore it is quite difficult to do something like map over two arrays & create permutations.

something.map(i=>another.map(j => [i, j]))

Because JSONLogic currently supports traversing objects downwards using dot notation, I propose the ../ operation be considered as well.

I do realize this affects how the iterative operators pass data down.

If you'd like, I could pursue implementing this functionality.

Thanks!

@TotalTechGeek
Copy link
Author

TotalTechGeek commented Apr 28, 2021

I've implemented this functionality in an alternative module, I'm going to close this ticket for now.

An example use case for this style of traversal is here:

{
    "map": [
        {
            "var": "arr"
        },
        {
            "+": [
                {
                    "var": "../../a"
                },
                {
                    "var": ""
                }
            ]
        }
    ]
}
{
    "arr": [
        1,
        2,
        3
    ],
    "a": 2
}

To produce [3,4,5] as output.

@ivan133
Copy link

ivan133 commented Jul 14, 2021

That's great.
I guess you meant https://jessemitchell.me/json-logic-engine/docs/higher
But sadly it means replace json-logic-js which is I'm not ready to do.
Sadly looks like there is no solution using json logic

@TotalTechGeek
Copy link
Author

@ivan133 Heh, sorry about that.

There were a few things I wanted to be able to support (async logic, compilation for native-js performance, data traversal), and it would've required a significant overhaul to support.

The alt module is 90% compatible with json-logic-js according to its test suite. The differences are subtle, like preferring [0] evaluating to true, as it does in native js.

@TotalTechGeek
Copy link
Author

I really like this module (and the json-logic language he designed), and I may try to contribute to it, but I had to make some rather aggressive tweaks to support some of the goals I had in mind.

@ivan133
Copy link

ivan133 commented Jul 15, 2021

Heh, sorry about that.

I really understand it.
I've added it as a reference for people who will find this page with google.

The alt module is 90% compatible with json-logic-js according to its test suite. The differences are subtle, like preferring [0] evaluating to true, as it does in native js.

I've noted your project and will try to switch to it in my spare time.

For now, I've created my own fork of json-logic with custom operation mapWithScope that passes an object with 2 keys: the LOOP_VAR and ROOT_DATA.
And it works.
Ugly but works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants