diff --git a/README.md b/README.md index 1cfdbac..a690bee 100644 --- a/README.md +++ b/README.md @@ -14,9 +14,200 @@ def deps do end ``` +## Examples + ```elixir -iex> JsonLogic.apply(%{"log" => "value"}) -"value" +JsonLogic.resolve(nil) +#=> nil + +JsonLogic.resolve(%{}) +#=> %{} + +JsonLogic.resolve(%{"var" => "key"}, %{"key" => "value"}) +#=> "value" + +JsonLogic.resolve(%{"var" => "nested.key"}, %{"nested" => %{"key" => "value"}}) +#=> "value" + +JsonLogic.resolve(%{"var" => ["none", "default"]}, %{"key" => "value"}) +#=> "default" + +JsonLogic.resolve(%{"var" => 0}, ~w{a b}) +#=> "a" + +JsonLogic.resolve(%{"==" => [1, 1]}) +#=> true + +JsonLogic.resolve(%{"==" => [0, 1]}) +#=> false + +JsonLogic.resolve(%{"!=" => [1, 1]}) +#=> false + +JsonLogic.resolve(%{"!=" => [0, 1]}) +#=> true + +JsonLogic.resolve(%{"===" => [1, 1]}) +#=> true + +JsonLogic.resolve(%{"===" => [1, 1.0]}) +#=> false + +JsonLogic.resolve(%{"===" => [1, %{"var" => "key"}]}, %{"key" => 1}) +#=> true + +JsonLogic.resolve(%{"!==" => [1, 1.0]}) +#=> true + +JsonLogic.resolve(%{"!==" => [1, 1]}) +#=> false + +JsonLogic.resolve(%{"!" => true}) +#=> false + +JsonLogic.resolve(%{"!" => false}) +#=> true + +JsonLogic.resolve(%{"if" => [true, "yes", "no" ]}) +#=> "yes" + +JsonLogic.resolve(%{"if" => [false, "yes", "no" ]}) +#=> "no" + +JsonLogic.resolve(%{"if" => [false, "unexpected", false, "unexpected", "default" ]}) +#=> "default" + +JsonLogic.resolve(%{"or" => [false, nil, "truthy"]}) +#=> "truthy" + +JsonLogic.resolve(%{"or" => ["first", "truthy"]}) +#=> "first" + +JsonLogic.resolve(%{"and" => [false, "falsy"]}) +#=> false + +JsonLogic.resolve(%{"and" => [true, 1, "truthy"]}) +#=> "truthy" + +JsonLogic.resolve(%{"max" => [1,2,3]}) +#=> 3 + +JsonLogic.resolve(%{"min" => [1,2,3]}) +#=> 1 + +JsonLogic.resolve(%{"<" => [0, 1]}) +#=> true + +JsonLogic.resolve(%{"<" => [1, 0]}) +#=> false + +JsonLogic.resolve(%{"<" => [0, 1, 2]}) +#=> true + +JsonLogic.resolve(%{"<" => [0, 2, 1]}) +#=> false + +JsonLogic.resolve(%{">" => [1, 0]}) +#=> true + +JsonLogic.resolve(%{">" => [0, 1]}) +#=> false + +JsonLogic.resolve(%{">" => [2, 1, 0]}) +#=> true + +JsonLogic.resolve(%{">" => [2, 0, 1]}) +#=> false + +JsonLogic.resolve(%{"<=" => [1, 1]}) +#=> true + +JsonLogic.resolve(%{"<=" => [1, 0]}) +#=> false + +JsonLogic.resolve(%{"<=" => [1, 1, 2]}) +#=> true + +JsonLogic.resolve(%{"<=" => [1, 0, 2]}) +#=> false + +JsonLogic.resolve(%{">=" => [1, 1]}) +#=> true + +JsonLogic.resolve(%{">=" => [0, 1]}) +#=> false + +JsonLogic.resolve(%{">=" => [1, 1, 0]}) +#=> true + +JsonLogic.resolve(%{">=" => [0, 1, 2]}) +#=> false + +JsonLogic.resolve(%{"+" => [1,2,3]}) +#=> 6 + +JsonLogic.resolve(%{"+" => [2]}) +#=> 2 + +JsonLogic.resolve(%{"-" => [7,4]}) +#=> 3 + +JsonLogic.resolve(%{"-" => [2]}) +#=> -2 + +JsonLogic.resolve(%{"*" => [2,3,4]}) +#=> 24 + +JsonLogic.resolve(%{"/" => [5,2]}) +#=> 2.5 + +JsonLogic.resolve(%{"%" => [7, 3]}) +#=> 1 + +JsonLogic.resolve(%{"map" => [[1,2,3,4,5], %{"*" => [%{"var" => ""}, 2]}]}) +#=> [2,4,6,8,10] + +JsonLogic.resolve(%{"filter" => [[1,2,3,4,5], %{">" => [%{"var" => ""}, 2]}]}) +#=> [3,4,5] + +JsonLogic.resolve(%{"reduce" => [[1,2,3,4,5], %{"+" => [%{"var" => "current"}, %{"var" => "accumulator"}]}, 0]}) +#=> 15 + +JsonLogic.resolve(%{"all" => [[1,2,3], %{">" => [%{"var" => ""}, 0]}]}) +#=> true + +JsonLogic.resolve(%{"all" => [[-1,2,3], %{">" => [%{"var" => ""}, 0]}]}) +#=> false + +JsonLogic.resolve(%{"none" => [[1,2,3], %{"<" => [%{"var" => ""}, 0 ]}]}) +#=> true + +JsonLogic.resolve(%{"none" => [[-1,2,3], %{"<" => [%{"var" => ""}, 0 ]}]}) +#=> false + +JsonLogic.resolve(%{"some" => [[-1,2,3], %{"<" => [%{"var" => ""}, 0 ]}]}) +#=> true + +JsonLogic.resolve(%{"some" => [[1,2,3], %{"<" => [%{"var" => ""}, 0 ]}]}) +#=> false + +JsonLogic.resolve(%{"in" => ["sub", "substring"]}) +#=> true + +JsonLogic.resolve(%{"in" => ["na", "substring"]}) +#=> false + +JsonLogic.resolve(%{"in" => ["a", ["a", "b", "c"]]}) +#=> true + +JsonLogic.resolve(%{"in" => ["z", ["a", "b", "c"]]}) +#=> false + +JsonLogic.resolve(%{"cat" => ["a", "b", "c"]}) +#=> "abc" + +JsonLogic.resolve(%{"log" => "string"}) +#=> "string" ``` Detailed documentation can be found at [https://hexdocs.pm/json_logic](https://hexdocs.pm/json_logic). diff --git a/mix.exs b/mix.exs index aa1ae5e..45aee32 100644 --- a/mix.exs +++ b/mix.exs @@ -1,7 +1,7 @@ defmodule JsonLogic.Mixfile do use Mix.Project - @version "0.4.0" + @version "1.0.0" def project do [ diff --git a/test/json_logic_test.exs b/test/json_logic_test.exs index 190ed53..c2da063 100644 --- a/test/json_logic_test.exs +++ b/test/json_logic_test.exs @@ -841,6 +841,16 @@ defmodule JsonLogicTest do end describe ">" do + test "comparison with variables" do + logic = %{">" => [%{"var" => "quantity"}, 25]} + data = %{"quantity" => 1} + assert JsonLogic.resolve(logic, data) == false + + logic = %{">" => [%{"var" => "quantity"}, 25]} + data = %{"abc" => 1} + assert JsonLogic.resolve(logic, data) == false + end + test "integer, float, and decimal comparisons" do ones = [Decimal.new("1.0"), "1.0", "1", 1.0, 1] twos = [Decimal.new("2.0"), "2.0", "2", 2.0, 2]