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

if statements return first condition on nil value #42

Open
jqr opened this issue Oct 2, 2024 · 0 comments
Open

if statements return first condition on nil value #42

jqr opened this issue Oct 2, 2024 · 0 comments

Comments

@jqr
Copy link

jqr commented Oct 2, 2024

When an if statement encounters a value that is nil, all condition evaluation stops and the condition is returned.

JSONLogic.apply({ "if" => [true, nil] }, {})            # => true   (expected nil)
JSONLogic.apply({ "if" => [false, nil] }, {})           # => false  (expected nil)
JSONLogic.apply({ "if" => [false, nil, true, 1] }, {})  # => false  (expected 1)

Background

if generally has the form [condition1, value1, condition2, value2, ...] and the first condition to be truthy returns the associated value, and if no conditions match null is returned.

{ "if": [false, 1] }            // => null
{ "if": [false, 1, true, 2] }   // => 2
{ "if": [false, 1, false, 2] }  // => null

There is a special case with an odd number of arguments [condition1, value1, condition2, value2, value_else] where the value_else is returned if none of the previous conditions match.

{ "if": [false, 1, 2] }            // => 2
{ "if": [false, 1, false, 2, 3] }  // => 3

jqr added a commit to connectedbits/json-logic-ruby that referenced this issue Oct 2, 2024
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

1 participant