Skip to content

Commit d3ced29

Browse files
authored
Merge pull request #6 from persona-id/jkim/sync-changes
Jkim/sync changes
2 parents 89e2c6c + 8c5f649 commit d3ced29

File tree

3 files changed

+14
-144
lines changed

3 files changed

+14
-144
lines changed

lib/json_logic/operation.rb

Lines changed: 13 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Operation
3333
v[0][v[1]..limit]
3434
end,
3535
'none' => -> (v,d) do
36+
3637
v[0].each do |val|
3738
this_val_satisfies_condition = interpolated_block(v[1], val)
3839
if this_val_satisfies_condition
@@ -66,10 +67,9 @@ class Operation
6667
return value if condition.truthy?
6768
end
6869
},
69-
'=' => ->(v, d) { v[0].to_s.downcase == v[1].to_s.downcase },
70-
'==' => ->(v, d) { v[0].to_s.downcase == v[1].to_s.downcase },
70+
'==' => ->(v, d) { v[0].to_s == v[1].to_s },
7171
'===' => ->(v, d) { v[0] == v[1] },
72-
'!=' => ->(v, d) { v[0].to_s.downcase != v[1].to_s.downcase },
72+
'!=' => ->(v, d) { v[0].to_s != v[1].to_s },
7373
'!==' => ->(v, d) { v[0] != v[1] },
7474
'!' => ->(v, d) { v[0].falsy? },
7575
'!!' => ->(v, d) { v[0].truthy? },
@@ -79,23 +79,20 @@ class Operation
7979
result.nil? ? v.last : result
8080
},
8181
'?:' => ->(v, d) { LAMBDAS['if'].call(v, d) },
82-
'>' => ->(v, d) { format_values(v).each_cons(2).all? { |i, j| i > j } },
83-
'>=' => ->(v, d) { format_values(v).each_cons(2).all? { |i, j| i >= j } },
84-
'<' => ->(v, d) { format_values(v).each_cons(2).all? { |i, j| i < j } },
85-
'<=' => ->(v, d) { format_values(v).each_cons(2).all? { |i, j| i <= j } },
86-
'max' => ->(v, d) { format_values(v).max },
87-
'min' => ->(v, d) { format_values(v).min },
82+
'>' => ->(v, d) { v.map(&:to_f).each_cons(2).all? { |i, j| i > j } },
83+
'>=' => ->(v, d) { v.map(&:to_f).each_cons(2).all? { |i, j| i >= j } },
84+
'<' => ->(v, d) { v.map(&:to_f).each_cons(2).all? { |i, j| i < j } },
85+
'<=' => ->(v, d) { v.map(&:to_f).each_cons(2).all? { |i, j| i <= j } },
86+
'max' => ->(v, d) { v.map(&:to_f).max },
87+
'min' => ->(v, d) { v.map(&:to_f).min },
8888
'+' => ->(v, d) { v.map(&:to_f).reduce(:+) },
8989
'-' => ->(v, d) { v.map!(&:to_f); v.size == 1 ? -v.first : v.reduce(:-) },
9090
'*' => ->(v, d) { v.map(&:to_f).reduce(:*) },
9191
'/' => ->(v, d) { v.map(&:to_f).reduce(:/) },
9292
'%' => ->(v, d) { v.map(&:to_i).reduce(:%) },
9393
'^' => ->(v, d) { v.map(&:to_f).reduce(:**) },
9494
'merge' => ->(v, d) { v.flatten },
95-
'in' => ->(v, d) {
96-
v1_arg = interpolated_block(v[1], d)
97-
(v1_arg.is_a?(Array) ? v1_arg.flatten : v1_arg).include? v[0]
98-
},
95+
'in' => ->(v, d) { interpolated_block(v[1], d).include? v[0] },
9996
'cat' => ->(v, d) { v.map(&:to_s).join },
10097
'log' => ->(v, d) { puts v }
10198
}
@@ -108,6 +105,7 @@ def self.interpolated_block(block, data)
108105
def self.perform(operator, values, data)
109106
# If iterable, we can only pre-fill the first element, the second one must be evaluated per element.
110107
# If not, we can prefill all.
108+
111109
if is_iterable?(operator)
112110
interpolated = [JSONLogic.apply(values[0], data), *values[1..-1]]
113111
else
@@ -116,21 +114,8 @@ def self.perform(operator, values, data)
116114

117115
interpolated.flatten!(1) if interpolated.size == 1 # [['A']] => ['A']
118116

119-
nil_vars = values.filter.with_index { |v, i| !JSONLogic.uses_data(v).empty? && interpolated[i].nil? }
120-
first_var_is_nil = !JSONLogic.uses_data(values.first).empty? && interpolated.first.nil?
121-
122-
if !is_standard?(operator)
123-
send(operator, interpolated, data)
124-
elsif is_nilable?(operator) && !nil_vars.empty?
125-
# short circuit and return nil
126-
nil
127-
elsif is_iterable?(operator) && first_var_is_nil
128-
# if an array variable resolves to nil, default to []
129-
interpolated[0] = []
130-
LAMBDAS[operator.to_s].call(interpolated, data)
131-
else
132-
LAMBDAS[operator.to_s].call(interpolated, data)
133-
end
117+
return LAMBDAS[operator.to_s].call(interpolated, data) if is_standard?(operator)
118+
send(operator, interpolated, data)
134119
end
135120

136121
def self.is_standard?(operator)
@@ -143,42 +128,10 @@ def self.is_iterable?(operator)
143128
['filter', 'some', 'all', 'none', 'in', 'map', 'reduce'].include?(operator.to_s)
144129
end
145130

146-
def self.is_nilable?(operator)
147-
[
148-
'substr',
149-
'>',
150-
'>=',
151-
'<',
152-
'<=',
153-
'max',
154-
'min',
155-
'+',
156-
'-',
157-
'*',
158-
'/',
159-
'%',
160-
'^'
161-
].include?(operator.to_s)
162-
end
163-
164131
def self.add_operation(operator, function)
165132
self.class.send(:define_method, operator) do |v, d|
166133
function.call(v, d)
167134
end
168135
end
169-
170-
def self.format_values(values)
171-
values = Array(values).flatten
172-
# If at least 1 value is numeric, assume all values can be treated as such
173-
# Sometimes numbers are represented as strings, so they need to be converted
174-
if values.any? { |v| v.is_a?(Numeric) }
175-
values.map(&:to_f)
176-
elsif values.all? { |v| v.is_a?(String) }
177-
values.map(&:downcase)
178-
else
179-
# Convert everything to strings. This handles date comparison
180-
values.map(&:to_s)
181-
end
182-
end
183136
end
184137
end

lib/json_logic/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module JSONLogic
2-
VERSION = '0.4.7.persona'
2+
VERSION = '0.4.5.persona'
33
end

test/json_logic_test.rb

Lines changed: 0 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -154,87 +154,4 @@ def test_uses_data_missing
154154
assert_equal ["x"], JSONLogic.apply({"missing": [vars]}, provided_data_missing_x)
155155
end
156156

157-
def test_nil_var_substr
158-
logic = {"substr" => [{"var" => "test"}, 1]}
159-
assert_nil(JSONLogic.apply(logic, {}))
160-
end
161-
162-
def test_nil_var_equals
163-
logic = {"=" => [ {"var" => "test"}, 10 ]}
164-
assert_equal(false, JSONLogic.apply(logic, {}))
165-
166-
logic = {"==" => [ 10, {"var" => "test"} ]}
167-
assert_equal(false, JSONLogic.apply(logic, {}))
168-
169-
logic = {"===" => [ {"var" => "test"}, 10 ]}
170-
assert_equal(false, JSONLogic.apply(logic, {}))
171-
172-
logic = {"!=" => [ 10, {"var" => "test"} ]}
173-
assert_equal(true, JSONLogic.apply(logic, {}))
174-
175-
logic = {"!==" => [ {"var" => "test"}, 10 ]}
176-
assert_equal(true, JSONLogic.apply(logic, {}))
177-
end
178-
179-
def test_nil_var_gt_lt
180-
logic = {">" => [ {"var" => "test"}, 10 ]}
181-
assert_nil(JSONLogic.apply(logic, {}))
182-
183-
logic = {">=" => [ 10, {"var" => "test"} ]}
184-
assert_nil(JSONLogic.apply(logic, {}))
185-
186-
logic = {"<" => [ {"var" => "test"}, 10 ]}
187-
assert_nil(JSONLogic.apply(logic, {}))
188-
189-
logic = {"<=" => [ 10, {"var" => "test"} ]}
190-
assert_nil(JSONLogic.apply(logic, {}))
191-
end
192-
193-
def test_nil_var_max_min
194-
logic = {"max" => [ {"var" => "test"}, 10, 20 ]}
195-
assert_nil(JSONLogic.apply(logic, {}))
196-
197-
logic = {"min" => [ 10, {"var" => "test"}, 20 ]}
198-
assert_nil(JSONLogic.apply(logic, {}))
199-
end
200-
201-
def test_nil_var_math_operators
202-
logic = {"+" => [ {"var" => "test"}, 10 ]}
203-
assert_nil(JSONLogic.apply(logic, {}))
204-
205-
logic = {"-" => [ 10, {"var" => "test"} ]}
206-
assert_nil(JSONLogic.apply(logic, {}))
207-
208-
logic = {"*" => [ {"var" => "test"}, 10 ]}
209-
assert_nil(JSONLogic.apply(logic, {}))
210-
211-
logic = {"/" => [ 10, {"var" => "test"} ]}
212-
assert_nil(JSONLogic.apply(logic, {}))
213-
214-
logic = {"%" => [ {"var" => "test"}, 10 ]}
215-
assert_nil(JSONLogic.apply(logic, {}))
216-
217-
logic = {"^" => [ 10, {"var" => "test"} ]}
218-
assert_nil(JSONLogic.apply(logic, {}))
219-
end
220-
221-
def test_values_dates
222-
logic = {">" => [ Time.at(1), Time.at(0) ]}
223-
assert_equal(true, JSONLogic.apply(logic, {}))
224-
225-
logic = {">=" => [ Time.at(0), Time.at(1) ]}
226-
assert_equal(false, JSONLogic.apply(logic, {}))
227-
228-
logic = {"<" => [ Time.at(1), Time.at(0) ]}
229-
assert_equal(false, JSONLogic.apply(logic, {}))
230-
231-
logic = {"<=" => [ Time.at(0), Time.at(1) ]}
232-
assert_equal(true, JSONLogic.apply(logic, {}))
233-
end
234-
235-
def test_format_values
236-
assert_equal [1.0, 2.0], JSONLogic::Operation.format_values(["1", 2])
237-
assert_equal ["test", "hello"], JSONLogic::Operation.format_values(["TEST", "hEllO"])
238-
assert_equal ["1969-12-31 16:00:00 -0800", "1969-12-31 16:00:01 -0800"], JSONLogic::Operation.format_values([Time.at(0), Time.at(1)])
239-
end
240157
end

0 commit comments

Comments
 (0)