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

max/min not working with array #25

Open
sauravkkr opened this issue Nov 25, 2020 · 1 comment
Open

max/min not working with array #25

sauravkkr opened this issue Nov 25, 2020 · 1 comment

Comments

@sauravkkr
Copy link

Rule: {"max":[{"var" : "integers"}]}
Data: {"integers":[1,2,3,4,5]}
Always return null.

@jwadhams
Copy link
Owner

jwadhams commented Dec 1, 2020

I don't think the documentation is clear enough.

As implemented, max returns the largest argument (and assumes all arguments are numeric or coercable to a numeric) but no implementation really knows what to do with an argument that is an array, and the shared unit tests don't cover it.

In PHP:

JWadhams\JsonLogic::apply(['max'=>[1,2,3]]); 
// returns 3
JWadhams\JsonLogic::apply(['max'=>[[1,2,3]]]);
// returns [1,2,3]

In JavaScript:

jsonLogic.apply({"max":[1,2,3]})
// returns 3
jsonLogic.apply({"max":[[1,2,3]]})
// returns NaN

You could pretty easily write a version in your language of choice that expects a single array argument. Here's a JavaScript approach:

jsonLogic.add_operation('array_max', function (a) {
    return Math.max.apply(this, a);
});
jsonLogic.apply({"array_max":[[1,2,3]]})
// returns 3
jsonLogic.apply(
    {"array_max":[{"var":"integers"}]},
    {"integers":[1,2,3,4,5]}
);
// returns 5

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