Skip to content

Commit 043dd54

Browse files
committed
Instead of copying and shifting values, move back to a non-destructive FOR loop.
1 parent 1524c29 commit 043dd54

File tree

2 files changed

+16
-29
lines changed

2 files changed

+16
-29
lines changed

logic.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -125,24 +125,18 @@ jsonLogic.apply = function(logic, data){
125125
if( 0 ){ 1 }else if( 2 ){ 3 }else if( 4 ){ 5 }else{ 6 };
126126
127127
The implementation is:
128-
given two+ parameters,
129-
shift off the first two values.
128+
For pairs of values (0,1 then 2,3 then 4,5 etc)
130129
If the first evaluates truthy, evaluate and return the second
131-
If the first evaluates falsy, start again with the remaining parameters.
132-
given one parameter, evaluate and return it.
133-
given 0 parameters, return NULL
130+
If the first evaluates falsy, jump to the next pair (e.g, 0,1 to 2,3)
131+
given one parameter, evaluate and return it. (it's an Else and all the If/ElseIf were false)
132+
given 0 parameters, return NULL (not great practice, but there was no Else)
134133
*/
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();
139-
140-
if( jsonLogic.truthy(conditional) ){
141-
return jsonLogic.apply(consequent, data);
134+
for(var i = 0 ; i < values.length - 1 ; i += 2){
135+
if( jsonLogic.truthy( jsonLogic.apply(values[i], data) ) ){
136+
return jsonLogic.apply(values[i+1], data);
142137
}
143138
}
144-
145-
if(v.length === 1) return jsonLogic.apply(v[0], data);
139+
if(values.length === i+1) return jsonLogic.apply(values[i], data);
146140
return null;
147141
}
148142

tests/tests.json

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
[{"if":[true, "apple", true, "banana", true, "carrot", "date"]}, null, "apple"],
6464

6565
"Missing",
66-
6766
[{"missing":[]}, null, []],
6867
[{"missing":["a"]}, null, ["a"]],
6968
[{"missing":"a"}, null, ["a"]],
@@ -81,7 +80,7 @@
8180
[{"missing":["a.b"]}, {"a":{"b":"apple brownie"}}, []],
8281
[{"missing":["a.b", "a.c"]}, {"a":{"b":"apple brownie"}}, ["a.c"]],
8382

84-
"Missing and If are friends",
83+
"Missing and If are friends, because empty arrays are falsey in JsonLogic",
8584
[{"if":[ {"missing":"a"}, "missed it", "found it" ]}, {"a":"apple"}, "found it"],
8685
[{"if":[ {"missing":"a"}, "missed it", "found it" ]}, {"b":"banana"}, "missed it"],
8786

@@ -98,23 +97,17 @@
9897
[{"merge":[1,2]}, null, [1,2]],
9998
[{"merge":[1,[2]]}, null, [1,2]],
10099

101-
"Missing and Merge, and If are friends. VIN is always required, APR is only required if financing is true.",
100+
"Missing, Merge, and If are friends. VIN is always required, APR is only required if financing is true.",
102101
[
103-
{"missing":{"merge":[
104-
"vin",
105-
{"if": [{"var":"financing"}, ["apr"], [] ]}
106-
]} },
107-
{"financing":true},
108-
["vin","apr"]
102+
{"missing":{"merge":[ "vin", {"if": [{"var":"financing"}, ["apr"], [] ]} ]} },
103+
{"financing":true},
104+
["vin","apr"]
109105
],
110106

111107
[
112-
{"missing":{"merge":[
113-
"vin",
114-
{"if": [{"var":"financing"}, ["apr"], [] ]}
115-
]} },
116-
{"financing":false},
117-
["vin"]
108+
{"missing":{"merge":[ "vin", {"if": [{"var":"financing"}, ["apr"], [] ]} ]} },
109+
{"financing":false},
110+
["vin"]
118111
],
119112

120113
"END"

0 commit comments

Comments
 (0)