diff --git a/logic.js b/logic.js index 6b827df..ddf433d 100644 --- a/logic.js +++ b/logic.js @@ -74,6 +74,9 @@ http://ricostacruz.com/cheatsheets/umdjs.html "log": function(a) { console.log(a); return a; }, + "comment": function(a) { + return a; + }, "in": function(a, b) { if(!b || typeof b.indexOf === "undefined") return false; return (b.indexOf(a) !== -1); @@ -252,7 +255,7 @@ http://ricostacruz.com/cheatsheets/umdjs.html } } if(values.length === i+1) return jsonLogic.apply(values[i], data); - return null; + return undefined; }else if(op === "and") { // Return first falsy, or last for(i=0; i < values.length; i+=1) { current = jsonLogic.apply(values[i], data); @@ -298,6 +301,18 @@ http://ricostacruz.com/cheatsheets/umdjs.html return jsonLogic.apply(scopedLogic, datum); }); + }else if(op === 'mapWithScope'){ + scopedData = jsonLogic.apply(values[0], data); + scopedLogic = values[1]; + + if ( ! Array.isArray(scopedData)) { + return []; + } + + return scopedData.map(function(datum, index){ + return jsonLogic.apply(scopedLogic, Object.assign({}, { 'LOOP_VAR': datum }, { 'ROOT_DATA': data, 'CURRENT_LOOP_INDEX': index })); + }); + }else if(op === 'reduce'){ scopedData = jsonLogic.apply(values[0], data); scopedLogic = values[1]; @@ -318,7 +333,7 @@ http://ricostacruz.com/cheatsheets/umdjs.html ); }else if(op === "all") { - scopedData = jsonLogic.apply(values[0], data); + scopedData = jsonLogic.apply(values[0], data) || []; scopedLogic = values[1]; // All of an empty set is false. Note, some and none have correct fallback after the for loop if( ! scopedData.length) {