AddToSetAccumulator.js 1.5 KB

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