SetIntersectionExpression.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. "use strict";
  2. /**
  3. * A $setintersection pipeline expression.
  4. * @see evaluateInternal
  5. * @class SetIntersectionExpression
  6. * @namespace mungedb-aggregate.pipeline.expressions
  7. * @module mungedb-aggregate
  8. * @constructor
  9. **/
  10. var SetIntersectionExpression = module.exports = function SetIntersectionExpression() {
  11. base.call(this);
  12. }, klass = SetIntersectionExpression, base = require("./NaryBaseExpressionT")(SetIntersectionExpression), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  13. // DEPENDENCIES
  14. var Value = require("../Value"),
  15. Expression = require("./Expression");
  16. // PROTOTYPE MEMBERS
  17. proto.getOpName = function getOpName() {
  18. return "$setIntersection";
  19. };
  20. /**
  21. * Takes 2 objects. Returns the intersects of the objects.
  22. * @method evaluateInternal
  23. **/
  24. proto.evaluateInternal = function evaluateInternal(vars) {
  25. var object1 = this.operands[0].evaluateInternal(vars),
  26. object2 = this.operands[1].evaluateInternal(vars);
  27. if (object1 instanceof Array) throw new Error(this.getOpName() + ": object 1 must be an object");
  28. if (object2 instanceof Array) throw new Error(this.getOpName() + ": object 2 must be an object");
  29. var result = object1.filter(function(n) {
  30. return object2.indexOf(n) > -1;
  31. });
  32. };
  33. /** Register Expression */
  34. Expression.registerExpression("$setIntersection", base.parse);