json-logic-rb 0.1.0.beta1 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +7 -1
- data/lib/json_logic/semantics.rb +18 -7
- data/lib/json_logic/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5707bf9b405fa29dd6538a189fb8651219da38a6adae5c20b83a9105e76f4ca8
|
|
4
|
+
data.tar.gz: 59e6be4c62e2b4bc48bca88d1c24010c896a919d4f0ad3cd06ef326b3df7b65d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 7b9b11b25711edc2956999404da117022453074ce2a23420a3a5b885859c993ec84cb553e8f55a556feacf01deff8ed90e76b154b1d5fd4122f4675c42e92b6f
|
|
7
|
+
data.tar.gz: b1bb6ed52e18c158b72e04fc20e42883dd3e426b937cf4be37d24cfdf7785b629f315ace39dd612dd42a0d5079c06a299463549917a4f56740991012f886f899
|
data/README.md
CHANGED
|
@@ -43,7 +43,7 @@ gem install json-logic-rb
|
|
|
43
43
|
## Quick start
|
|
44
44
|
|
|
45
45
|
```ruby
|
|
46
|
-
require
|
|
46
|
+
require 'json_logic'
|
|
47
47
|
|
|
48
48
|
rule = { "+" => [1, 2, 3] }
|
|
49
49
|
|
|
@@ -313,3 +313,9 @@ Expected output
|
|
|
313
313
|
- Rules are **data**, not code; no Ruby eval.
|
|
314
314
|
- When evaluating untrusted rules, consider adding a timeout and error handling at the call site.
|
|
315
315
|
- Custom operations should be **pure** (no IO, no network, no shell).
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
## Authors
|
|
319
|
+
|
|
320
|
+
- [Valeriya Petrova](https://github.com/piatrova-valeriya1999)
|
|
321
|
+
- [Tavrel Kate](https://github.com/tavrelkate)
|
data/lib/json_logic/semantics.rb
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
module JsonLogic
|
|
4
4
|
module Semantics
|
|
5
|
+
NUMERIC_RE = /\A[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?\z/.freeze
|
|
6
|
+
|
|
5
7
|
module_function
|
|
6
8
|
|
|
7
9
|
def truthy?(v)
|
|
@@ -18,8 +20,16 @@ module JsonLogic
|
|
|
18
20
|
def falsy?(v) = !truthy?(v)
|
|
19
21
|
|
|
20
22
|
def to_number(v)
|
|
21
|
-
|
|
22
|
-
v
|
|
23
|
+
case v
|
|
24
|
+
when Integer then v
|
|
25
|
+
when Numeric then v.to_f
|
|
26
|
+
when String
|
|
27
|
+
s = v.strip
|
|
28
|
+
return nil unless NUMERIC_RE.match?(s)
|
|
29
|
+
s =~ /[.eE]/ ? s.to_f : s.to_i
|
|
30
|
+
else
|
|
31
|
+
nil
|
|
32
|
+
end
|
|
23
33
|
end
|
|
24
34
|
|
|
25
35
|
def strict_equal(a,b)
|
|
@@ -30,12 +40,13 @@ module JsonLogic
|
|
|
30
40
|
end
|
|
31
41
|
end
|
|
32
42
|
|
|
33
|
-
def loose_equal(a,b)
|
|
34
|
-
if (a.is_a?(Numeric)||a.is_a?(String)) && (b.is_a?(Numeric)||b.is_a?(String))
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
43
|
+
def loose_equal(a, b)
|
|
44
|
+
if (a.is_a?(Numeric) || a.is_a?(String)) && (b.is_a?(Numeric) || b.is_a?(String))
|
|
45
|
+
na = to_number(a)
|
|
46
|
+
nb = to_number(b)
|
|
47
|
+
return na == nb unless na.nil? || nb.nil?
|
|
38
48
|
end
|
|
49
|
+
a == b
|
|
39
50
|
end
|
|
40
51
|
end
|
|
41
52
|
end
|
data/lib/json_logic/version.rb
CHANGED