|
|
@@ -21,7 +21,8 @@ var SetIntersectionExpression = module.exports = function SetIntersectionExpress
|
|
|
|
|
|
// DEPENDENCIES
|
|
|
var Value = require("../Value"),
|
|
|
- Expression = require("./Expression");
|
|
|
+ Expression = require("./Expression"),
|
|
|
+ Helpers = require("./Helpers");
|
|
|
|
|
|
// PROTOTYPE MEMBERS
|
|
|
proto.getOpName = function getOpName() {
|
|
|
@@ -29,18 +30,47 @@ proto.getOpName = function getOpName() {
|
|
|
};
|
|
|
|
|
|
/**
|
|
|
- * Takes 2 objects. Returns the intersects of the objects.
|
|
|
+ * Takes any number of arrays. Returns the intersection of the arrays.
|
|
|
* @method evaluateInternal
|
|
|
**/
|
|
|
proto.evaluateInternal = function evaluateInternal(vars) {
|
|
|
- var object1 = this.operands[0].evaluateInternal(vars),
|
|
|
- object2 = this.operands[1].evaluateInternal(vars);
|
|
|
- if (object1 instanceof Array || typeof object1 != object) throw new Error(this.getOpName() + ": object 1 must be an object. Got a(n) " + typeof object1);
|
|
|
- if (object2 instanceof Array || typeof object2 != object) throw new Error(this.getOpName() + ": object 2 must be an object. Got a(n) " + typeof object2);
|
|
|
+ var n = this.operands.length,
|
|
|
+ currentIntersection = {};
|
|
|
|
|
|
- var result = object1.filter(function(n) {
|
|
|
- return object2.indexOf(n) > -1;
|
|
|
- });
|
|
|
+ for (var i = 0; i < n; i++){
|
|
|
+
|
|
|
+ var nextEntry = this.operands[i].evaluateInternal(vars);
|
|
|
+ if (nextEntry == null || nextEntry == undefined){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (! (nextEntry instanceof Array )) throw new Error("Uassert 17047: All operands of " + this.getOpName() + "must be arrays. One argument is of type: " + typeof array1);
|
|
|
+
|
|
|
+ if (i === 0){
|
|
|
+ currentIntersection = Helpers.arrayToSet(nextEntry);
|
|
|
+ } else {
|
|
|
+
|
|
|
+ var nextSet = Helpers.arrayToSet(nextEntry);
|
|
|
+ if (Object.keys(currentIntersection).length > Object.keys(nextSet).length){
|
|
|
+ var temp = currentIntersection;
|
|
|
+ currentIntersection = nextSet;
|
|
|
+ nextSet = temp;
|
|
|
+ }
|
|
|
+
|
|
|
+ Object.keys(currentIntersection).forEach(function (key){
|
|
|
+ if (Object.keys(nextSet).indexOf(key) < 0){
|
|
|
+ delete currentIntersection[key]
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ if(currentIntersection === {}){
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var result = Helpers.setToArray(currentIntersection);
|
|
|
+
|
|
|
+ return Value.consume(result);
|
|
|
};
|
|
|
|
|
|
/** Register Expression */
|