SetEqualsExpression.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. if (arguments.length !== 2) throw new Error("two args expected");
  13. base.call(this);
  14. }, klass = SetEqualsExpression,
  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 "$setequals";
  27. };
  28. /**
  29. * Takes 2 arrays. Assigns the second array to the first array.
  30. * @method evaluateInternal
  31. **/
  32. proto.evaluateInternal = function evaluateInternal(vars) {
  33. var array1 = this.operands[0].evaluateInternal(vars),
  34. array2 = this.operands[1].evaluateInternal(vars);
  35. if (array1 instanceof Array) throw new Error(this.getOpName() + ": object 1 must be an array");
  36. if (array2 instanceof Array) throw new Error(this.getOpName() + ": object 2 must be an array");
  37. array1 = array2;
  38. return array1;
  39. };
  40. /** Register Expression */
  41. Expression.registerExpression("$setequals", base.parse(SetEqualsExpression));