Skip to content

Commit

Permalink
fix: fix in operation with non array
Browse files Browse the repository at this point in the history
  • Loading branch information
GonzaRodriguez committed Jun 29, 2022
1 parent 1fe6722 commit 71b0e96
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/json_logic/operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ class Operation
'%' => ->(v, d) { v.map(&:to_i).reduce(:%) },
'^' => ->(v, d) { v.map(&:to_f).reduce(:**) },
'merge' => ->(v, d) { v.flatten },
'in' => ->(v, d) { interpolated_block(v[1], d).include? v[0] },
'in' => ->(v, d) do
result = interpolated_block(v[1], d)&.include? v[0]
result.nil? ? false : result
end,
'cat' => ->(v, d) { v.map(&:to_s).join },
'log' => ->(v, d) { puts v }
}
Expand Down
7 changes: 7 additions & 0 deletions test/json_logic_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,11 @@ def test_uses_data_missing
assert_equal ["y"], JSONLogic.apply({"missing": [vars]}, provided_data_missing_y)
assert_equal ["x"], JSONLogic.apply({"missing": [vars]}, provided_data_missing_x)
end

def test_in_with_non_array
logic = { "in" => ["searchable_elem", { "var" => "non_array" }] }

refute JSONLogic.apply(logic, { "non_array" => nil })
refute JSONLogic.apply(logic, nil)
end
end

0 comments on commit 71b0e96

Please sign in to comment.