Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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];
Expand All @@ -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) {
Expand Down