Skip to content

Commit 9f6f776

Browse files
committed
New methods, esp existing libraries, may take one arg that is an array, that contains JsonLogic rules or 'var's
1 parent 5809e64 commit 9f6f776

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

logic.js

+4
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ jsonLogic.truthy = function(value){
160160
};
161161

162162
jsonLogic.apply = function(logic, data){
163+
//Does this array contain logic? Only one way to find out.
164+
if(Array.isArray(logic)){
165+
return logic.map(function(l){ return jsonLogic.apply(l,data); });
166+
}
163167
//You've recursed to a primitive, stop!
164168
if( ! jsonLogic.is_logic(logic) ){
165169
return logic;

tests/tests.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,27 @@ QUnit.test( "Expanding functionality with add_operator", function( assert) {
112112
jsonLogic.add_operation("fives", fives);
113113
assert.equal( jsonLogic.apply({"fives.add" : 37}), 42 );
114114
assert.equal( jsonLogic.apply({"fives.subtract" : [47] }), 42 );
115+
116+
//Calling a method with multiple var as arguments.
117+
jsonLogic.add_operation("times", function(a,b){ return a*b; });
118+
assert.equal(
119+
jsonLogic.apply(
120+
{"times" : [{"var":"a"}, {"var":"b"}]},
121+
{a:6,b:7}
122+
),
123+
42
124+
);
125+
126+
//Calling a method that takes an array, but the inside of the array has rules, too
127+
jsonLogic.add_operation("array_times", function(a){ return a[0]*a[1]; });
128+
assert.equal(
129+
jsonLogic.apply(
130+
{"array_times" : [[{"var":"a"}, {"var":"b"}]]},
131+
{a:6,b:7}
132+
),
133+
42
134+
);
135+
115136
});
116137

117138
QUnit.test( "Expanding functionality with method", function( assert) {
@@ -143,5 +164,4 @@ QUnit.test( "Expanding functionality with method", function( assert) {
143164
);
144165
assert.equal(a.count, 42); //Happy state change
145166

146-
147167
});

0 commit comments

Comments
 (0)