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

Feature request: make "in" operator supporting subsets #15

Open
sergatgh opened this issue Feb 21, 2019 · 2 comments
Open

Feature request: make "in" operator supporting subsets #15

sergatgh opened this issue Feb 21, 2019 · 2 comments

Comments

@sergatgh
Copy link

sergatgh commented Feb 21, 2019

Hello @jwadhams ,

I've got some difficulties trying to implement subset expression.

Currently it's possible to check whether a string is a part of another string

Example from site: {"in":["Spring", "Springfield"]}

I'd like to see a logic that will also work for expression {"in":[ [1, 2], [0, 1, 2, 3]]} - in this case I would expect to have "true" value in the result.

If that looks confusing, then it might be a good idea to have a new operator for this?

May I also ask you to help me to compose subset logic with existing operators?
Currently I'm using a logic like this:

{ "and": [ "in":[ 1, [0, 1, 2, 3]], "in":[ 2, [0, 1, 2, 3]] ] }

Is there a better way of writing this expression?

@jwadhams
Copy link
Owner

Could you rewrite it to use all?

{"all":[
    [1,2],
    {"in":[{"var":""}, [0,1,2,3]]}
]}

Otherwise you could add an operation:

jsonLogic.add_operation('containsAll', function(needles, haystack){
    return needles
        .map((needle) => haystack.indexOf(needle) !== -1) 
        .reduce((includes, carry) => {return carry && includes;}, true);
})

jsonLogic.apply({"containsAll":[[1,2],[0,1,2,3]]}) // true

@sergatgh
Copy link
Author

Hello @jwadhams
Thanks for your reply.

Yes I tried that way using "all", but in my case collection [0,1,2,3] was supposed to be given from state object like this:

{"all":[
    [1,2],
    {"in":[{"var":""}, {"var":"collection"}]}
]}
// (note: the example doesn't work, because context object changes in operations "some", "all" and "none")

Thanks for the provided example, that's what our team is going to use.

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