AddToSetAccumulator.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. var AddToSetAccumulator = module.exports = (function(){
  2. // DEPENDENCIES
  3. var Value = require("../Value");
  4. require("es6-shim");
  5. // CONSTRUCTOR
  6. /** Create an expression that finds the sum of n operands. **/
  7. var klass = module.exports = function AddToSetAccumulator(/* pCtx */){
  8. if(arguments.length !== 0) throw new Error("zero args expected");
  9. this.set = new Map();
  10. //this.itr = undefined; /* Shoudln't need an iterator for the set */
  11. //this.pCtx = undefined; /* Not using the context object currently as it is related to sharding */
  12. base.call(this);
  13. }, Accumulator = require("./Accumulator"), base = Accumulator, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  14. // PROTOTYPE MEMBERS
  15. proto.getOpName = function getOpName(){
  16. return "$addToSet";
  17. };
  18. proto.getFactory = function getFactory(){
  19. return klass; // using the ctor rather than a separate .create() method
  20. };
  21. proto.evaluate = function evaluate(doc) {
  22. if(arguments.length !== 1) throw new Error("One and only one arg expected");
  23. var rhs = this.operands[0].evaluate(doc);
  24. if ('undefined' != typeof rhs) {
  25. Value.verifyArray(rhs);
  26. for(var i=0; i<rhs.length; i++) {
  27. this.set.set(rhs[i], rhs[i]); //Sorry about variable names here... just following the rules!
  28. }
  29. }
  30. return undefined;
  31. };
  32. proto.getValue = function getValue() {
  33. return this.set.values();
  34. };
  35. return klass;
  36. })();