SetEqualsExpression.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. "use strict";
  2. /**
  3. * A $setequals pipeline expression.
  4. * @see evaluateInternal
  5. * @class SetEqualsExpression
  6. * @namespace mungedb-aggregate.pipeline.expressions
  7. * @module mungedb-aggregate
  8. * @constructor
  9. **/
  10. var SetEqualsExpression = module.exports = function SetEqualsExpression() {
  11. this.nargs = 2;
  12. base.call(this);
  13. }, klass = SetEqualsExpression,
  14. base = require("./NaryExpression"),
  15. proto = klass.prototype = Object.create(base.prototype, {
  16. constructor: {
  17. value: klass
  18. }
  19. });
  20. // DEPENDENCIES
  21. var Value = require("../Value"),
  22. Expression = require("./Expression");
  23. // PROTOTYPE MEMBERS
  24. proto.getOpName = function getOpName() {
  25. return "$setequals";
  26. };
  27. /**
  28. * Takes 2 arrays. Assigns the second array to the first array.
  29. * @method evaluateInternal
  30. **/
  31. proto.evaluateInternal = function evaluateInternal(vars) {
  32. var array1 = this.operands[0].evaluateInternal(vars),
  33. array2 = this.operands[1].evaluateInternal(vars);
  34. if (array1 instanceof Array) throw new Error(this.getOpName() + ": object 1 must be an array");
  35. if (array2 instanceof Array) throw new Error(this.getOpName() + ": object 2 must be an array");
  36. array1 = array2;
  37. return array1;
  38. };
  39. /** Register Expression */
  40. Expression.registerExpression("$setequals", base.parse(SetEqualsExpression));