Skip to content

Commit

Permalink
fix bug on array operations
Browse files Browse the repository at this point in the history
  • Loading branch information
liuming committed Oct 4, 2017
1 parent 65c4ad9 commit a8c170d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
13 changes: 7 additions & 6 deletions lib/json_logic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,11 @@ defmodule JsonLogic do

@doc false
def operation_var("", data) do
Enum.at(data, 0)
data
end

@doc false
# TODO: may need refactoring
def operation_var(path, data) when is_binary(path) do
[variable_name | names] = path |> String.split(".")

Expand Down Expand Up @@ -437,13 +438,13 @@ defmodule JsonLogic do
@doc false
def operation_map([list, map_action], data) do
JsonLogic.apply(list, data)
|> Enum.map(fn(item) -> JsonLogic.apply(map_action, [item]) end)
|> Enum.map(fn(item) -> JsonLogic.apply(map_action, item) end)
end

@doc false
def operation_filter([list, filter_action], data) do
JsonLogic.apply(list, data)
|> Enum.filter(fn(item) -> JsonLogic.apply(filter_action, [item]) end)
|> Enum.filter(fn(item) -> JsonLogic.apply(filter_action, item) end)
end

@doc false
Expand All @@ -457,19 +458,19 @@ defmodule JsonLogic do
@doc false
def operation_all([list, test], data) do
JsonLogic.apply(list, data)
|> Enum.all?(fn(item) -> JsonLogic.apply(test, [item]) end)
|> Enum.all?(fn(item) -> JsonLogic.apply(test, item) end)
end

@doc false
def operation_none([list, test], data) do
JsonLogic.apply(list, data)
|> Enum.all?(fn(item) -> Kernel.if(JsonLogic.apply(test, [item]), do: false, else: true) end)
|> Enum.all?(fn(item) -> Kernel.if(JsonLogic.apply(test, item), do: false, else: true) end)
end

@doc false
def operation_some([list, test], data) do
JsonLogic.apply(list, data)
|> Enum.any?(fn(item) -> JsonLogic.apply(test, [item]) end)
|> Enum.any?(fn(item) -> JsonLogic.apply(test, item) end)
end

@doc false
Expand Down
11 changes: 8 additions & 3 deletions test/json_logic_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,24 @@ defmodule JsonLogicTest do
end

describe "map" do
test "returns mapped result" do
test "returns mapped integers" do
assert JsonLogic.apply(%{"map" => [ %{"var" => "integers"}, %{"*" => [%{"var" => ""}, 2]} ]}, %{"integers" => [1,2,3,4,5]}) == [2,4,6,8,10]
end
end

describe "filter" do
test "returns filtered result" do
test "returns filtered integers" do
assert JsonLogic.apply(%{"filter" => [ %{"var" => "integers"}, %{">" => [%{"var" => ""}, 2]} ]}, %{"integers" => [1,2,3,4,5]}) == [3,4,5]
end

test "returns filtered objects" do
data = %{"objects" => [%{"uid" => "A"}, %{"uid" => "B"}]}
assert JsonLogic.apply(%{"filter" => [ %{"var" => "objects"}, %{"==" => [%{"var" => "uid"}, "A"]} ]}, data) == [%{"uid" => "A"}]
end
end

describe "reduce" do
test "returns reduced result" do
test "returns reduced integers" do
assert JsonLogic.apply(%{"reduce" => [ %{"var" => "integers"}, %{"+" => [%{"var" => "current"}, %{"var" => "accumulator"}]} ]}, %{"integers" => [1,2,3]}) == 6
end
end
Expand Down

0 comments on commit a8c170d

Please sign in to comment.