SetEqualsExpression.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. if (arguments.length !== 0) throw new Error("Zero arguments expected. Got " + arguments.length);
  12. this.nargs = 2;
  13. base.call(this);
  14. }, klass = SetEqualsExpression, base = require("./NaryBaseExpressionT")(SetEqualsExpression), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  15. // DEPENDENCIES
  16. var Value = require("../Value"),
  17. Expression = require("./Expression"),
  18. Helpers = require("./Helpers");
  19. // PROTOTYPE MEMBERS
  20. proto.getOpName = function getOpName() {
  21. return "$setEquals";
  22. };
  23. proto.validateArguments = function validateArguments(args) {
  24. if (args.length < 2) throw new Error("Two or more arguments requird. Got " + arguments.length);
  25. };
  26. /**
  27. * Takes arrays. Returns true if the arrays have the same values (after duplicates are removed). Returns false otherwise.
  28. * @method evaluateInternal
  29. **/
  30. proto.evaluateInternal = function evaluateInternal(vars) {
  31. var n = this.operands.length,
  32. lhs = [];
  33. for (var i = 0; i < n; i++){
  34. var nextEntry = this.operands[i].evaluateInternal(vars);
  35. if(!(nextEntry instanceof Array)) throw new Error("All operands of " + this.getOpName() +" must be arrays. One argument is of type: " + typeof nextEntry);
  36. if(i == 0){
  37. lhs = Helpers.arrayToSet(nextEntry);
  38. } else {
  39. var rhs = Helpers.arrayToSet(nextEntry);
  40. if (JSON.stringify(lhs) !== JSON.stringify(rhs)){
  41. return false;
  42. }
  43. }
  44. }
  45. return true;
  46. };
  47. /** Register Expression */
  48. Expression.registerExpression("$setEquals", base.parse);