SetIntersectionExpression.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. this.fixedArity(2);
  12. if (arguments.length !== 2) throw new Error("two args expected");
  13. base.call(this);
  14. }, klass = SetIntersectionExpression,
  15. base = require("./NaryExpression"),
  16. proto = klass.prototype = Object.create(base.prototype, {
  17. constructor: {
  18. value: klass
  19. }
  20. });
  21. // DEPENDENCIES
  22. var Value = require("../Value"),
  23. Expression = require("./Expression");
  24. // PROTOTYPE MEMBERS
  25. proto.getOpName = function getOpName() {
  26. return "$setintersection";
  27. };
  28. /**
  29. * Takes 2 objects. Returns the intersects of the objects.
  30. * @method evaluateInternal
  31. **/
  32. proto.evaluateInternal = function evaluateInternal(vars) {
  33. var object1 = this.operands[0].evaluateInternal(vars),
  34. object2 = this.operands[1].evaluateInternal(vars);
  35. if (object1 instanceof Array) throw new Error(this.getOpName() + ": object 1 must be an object");
  36. if (object2 instanceof Array) throw new Error(this.getOpName() + ": object 2 must be an object");
  37. var result = object1.filter(function(n) {
  38. return object2.indexOf(n) > -1;
  39. });
  40. };
  41. /** Register Expression */
  42. Expression.registerExpression("$setintersection", SetIntersectionExpression.parse);