AddToSetAccumulator.js 1.4 KB

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