Skip to content

Commit

Permalink
handle more edge cases for "in" operation
Browse files Browse the repository at this point in the history
  • Loading branch information
liuming committed Aug 9, 2017
1 parent 613d43e commit d6b4425
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/json_logic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -397,13 +397,19 @@ defmodule JsonLogic do
end

@doc false
def operation_in([substring, string], data) when not is_list(substring) and not is_list(string) do
String.contains?(JsonLogic.apply(string, data), JsonLogic.apply(substring, data))
def operation_in([member, list], data) when is_list(list) do
members = list |> Enum.map(fn(m) -> JsonLogic.apply(m, data) end)
Enum.member?(members, JsonLogic.apply(member, data))
end

@doc false
def operation_in([member, list], data) when is_list(list) do
Enum.member?(JsonLogic.apply(list, data), JsonLogic.apply(member, data))
def operation_in([substring, string], data) when is_binary(string) do
String.contains?(string, JsonLogic.apply(substring, data))
end

@doc false
def operation_in([find, from], data) do
operation_in([JsonLogic.apply(find, data), JsonLogic.apply(from, data)], data)
end

@doc false
Expand Down
4 changes: 4 additions & 0 deletions test/json_logic_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,9 @@ defmodule JsonLogicTest do
test "returns true from vars" do
assert JsonLogic.apply(%{"in" => [%{"var" => "find"}, %{"var" => "from"}]}, %{"find" => "sub", "from" => "substring"}) == true
end

test "returns true from var list" do
assert JsonLogic.apply(%{"in" => [%{"var" => "find"}, %{"var" => "from"}]}, %{"find" => "sub", "from" => ["sub", "string"]}) == true
end
end
end

0 comments on commit d6b4425

Please sign in to comment.