Skip to content

if statements return all arguments on no matching conditions #41

Description

@jqr

When an if statement doesn't match on any of it's conditions, it returns all arguments instead of nil as expected:

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

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions