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

Subtract breaks when there is a 0 between two values #128

Open
johnfrancisgit opened this issue Apr 17, 2023 · 3 comments
Open

Subtract breaks when there is a 0 between two values #128

johnfrancisgit opened this issue Apr 17, 2023 · 3 comments

Comments

@johnfrancisgit
Copy link

If you do a subtraction with a 0 in the list of values to subtract, the results is incorrect.
Example: {"-": [10, 0, 100]} should be -90 but it returns 10 for some reason

@jwadhams
Copy link
Owner

It's actually ignoring the third arg. In the docs https://jsonlogic.com/operations.html#arithmetic----- we say because + and * are associative, they take 1-to-n arguments.
- only takes one arg (make negative) and two args (subtract) it does not process args after the second.

@jwadhams
Copy link
Owner

jwadhams commented Apr 17, 2023

Here's the relevant snippet of code, comparing - to +. I don't think it's covered one way or the other by unit tests, arguably we could make it take a list and use a reducer, but I'm not sure its "intuitive"

https://github.com/jwadhams/json-logic-js/blob/master/logic.js#L92-L108

@johnfrancisgit
Copy link
Author

Ah, I see. Thanks for the clarification @jwadhams
I think it would be useful to do subtraction with more than 2 values.
The workaround I suppose is to nest them like {"-": [{"-": [10, 0]}, 100]}

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