json-logic
json-logic copied to clipboard
Feature request: make "in" operator supporting subsets
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?
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
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.