PushAccumulator.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. "use strict";
  2. /**
  3. * Constructor for PushAccumulator. Pushes items onto an array.
  4. * @class PushAccumulator
  5. * @namespace mungedb-aggregate.pipeline.accumulators
  6. * @module mungedb-aggregate
  7. * @constructor
  8. **/
  9. var PushAccumulator = module.exports = function PushAccumulator(){
  10. this.values = [];
  11. base.call(this);
  12. }, klass = PushAccumulator, Accumulator = require("./Accumulator"), base = Accumulator, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  13. proto.processInternal = function processInternal(input, merging) {
  14. if (!merging) {
  15. if (input !== undefined) {
  16. this.values.push(input);
  17. }
  18. } else {
  19. // If we're merging, we need to take apart the arrays we
  20. // receive and put their elements into the array we are collecting.
  21. // If we didn't, then we'd get an array of arrays, with one array
  22. // from each merge source.
  23. if (!Array.isArray(input)) throw new Error("Assertion failure");
  24. Array.prototype.push.apply(this.values, input);
  25. }
  26. };
  27. proto.getValue = function getValue(toBeMerged) {
  28. return this.values;
  29. };
  30. proto.reset = function reset() {
  31. this.values = [];
  32. };
  33. klass.create = function create() {
  34. return new PushAccumulator();
  35. };
  36. proto.getOpName = function getOpName() {
  37. return "$push";
  38. };