Skip to content

Commit

Permalink
Adds examples to readme (#13)
Browse files Browse the repository at this point in the history
Adds examples to readme and a test case for comparisons with variables.
  • Loading branch information
warmwaffles authored Feb 18, 2023
2 parents bc7a28a + 10151a4 commit 2c09745
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 3 deletions.
195 changes: 193 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule JsonLogic.Mixfile do
use Mix.Project

@version "0.4.0"
@version "1.0.0"

def project do
[
Expand Down
10 changes: 10 additions & 0 deletions test/json_logic_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 2c09745

Please sign in to comment.