Skip to content

Commit

Permalink
Better handling of if without a match and else values.
Browse files Browse the repository at this point in the history
  • Loading branch information
jqr committed Oct 2, 2024
1 parent 87bfe70 commit b2935c3
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/json_logic/operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,16 @@ class Operation
end
end,
'if' => ->(v, d) {
v.each_slice(2) do |condition, value|
return condition if value.nil?
return value if condition.truthy?
v.each_slice(2) do |condition_and_value|
# If this slice a single value? It's an else value only.
if condition_and_value.size == 1
return condition_and_value.first
else
condition, value = condition_and_value
return value if condition.truthy?
end
end
nil
},
'==' => ->(v, d) { v[0].to_s == v[1].to_s },
'===' => ->(v, d) { v[0] == v[1] },
Expand Down

0 comments on commit b2935c3

Please sign in to comment.