Skip to content

Commit 1524c29

Browse files
committed
Missing works with Merge and If, but it has to figure out whether it's given one array, or many scalars.
1 parent 6bf5f19 commit 1524c29

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

logic.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,17 @@ jsonLogic.apply = function(logic, data){
132132
given one parameter, evaluate and return it.
133133
given 0 parameters, return NULL
134134
*/
135-
while(values.length >= 2){
136-
var conditional = jsonLogic.apply(values.shift(), data),
137-
consequent = values.shift();
135+
var v = values.slice(0); //Don't shift values off the original rule, destroy a copy
136+
while(v.length >= 2){
137+
var conditional = jsonLogic.apply(v.shift(), data),
138+
consequent = v.shift();
138139

139140
if( jsonLogic.truthy(conditional) ){
140141
return jsonLogic.apply(consequent, data);
141142
}
142143
}
143144

144-
if(values.length === 1) return jsonLogic.apply(values[0], data);
145+
if(v.length === 1) return jsonLogic.apply(v[0], data);
145146
return null;
146147
}
147148

@@ -161,6 +162,14 @@ jsonLogic.apply = function(logic, data){
161162
return data;
162163

163164
}else if(op === "missing"){
165+
/*
166+
Missing can receive many keys as many arguments, like {"missing:[1,2]}
167+
Missing can also receive *one* argument that is an array of keys,
168+
which typically happens if it's actually acting on the output of another command
169+
(like IF or MERGE)
170+
*/
171+
if( Array.isArray(values[0]) ){ values = values[0]; }
172+
164173
var missing = [];
165174
values.map(function(data_key){
166175
if(jsonLogic.apply({'var':data_key}, data) === null){

tests/tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ QUnit.test( "Shared JsonLogic.com tests ", function( assert ){
4646
assert.deepEqual(
4747
jsonLogic.apply(rule, data),
4848
expected,
49-
"jsonLogic("+ JSON.stringify(rule) +","+ JSON.stringify( data ) +") = " + expected
49+
"jsonLogic("+ JSON.stringify(rule) +","+ JSON.stringify( data ) +") = " + JSON.stringify(expected)
5050
);
5151
}
5252

tests/tests.json

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,35 @@
8787

8888
"Merge arrays",
8989
[{"merge":[]}, null, []],
90-
[{"merge":[["a"]]}, null, ["a"]],
91-
[{"merge":[["a"], ["b"]]}, null, ["a","b"]],
92-
[{"merge":[["a"], ["b"], ["c"]]}, null, ["a","b","c"]],
93-
[{"merge":[["a", "b"], ["c"]]}, null, ["a","b","c"]],
94-
[{"merge":[["a"], ["b", "c"]]}, null, ["a","b","c"]],
90+
[{"merge":[[1]]}, null, [1]],
91+
[{"merge":[[1],[]]}, null, [1]],
92+
[{"merge":[[1], [2]]}, null, [1,2]],
93+
[{"merge":[[1], [2], [3]]}, null, [1,2,3]],
94+
[{"merge":[[1, 2], [3]]}, null, [1,2,3]],
95+
[{"merge":[[1], [2, 3]]}, null, [1,2,3]],
96+
"Given non-array arguments, merge converts them to arrays",
97+
[{"merge":1}, null, [1]],
98+
[{"merge":[1,2]}, null, [1,2]],
99+
[{"merge":[1,[2]]}, null, [1,2]],
100+
101+
"Missing and Merge, and If are friends. VIN is always required, APR is only required if financing is true.",
102+
[
103+
{"missing":{"merge":[
104+
"vin",
105+
{"if": [{"var":"financing"}, ["apr"], [] ]}
106+
]} },
107+
{"financing":true},
108+
["vin","apr"]
109+
],
110+
111+
[
112+
{"missing":{"merge":[
113+
"vin",
114+
{"if": [{"var":"financing"}, ["apr"], [] ]}
115+
]} },
116+
{"financing":false},
117+
["vin"]
118+
],
95119

96120
"END"
97121
]

0 commit comments

Comments
 (0)