PushAccumulator.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. if (arguments.length !== 0) throw new Error("zero args expected");
  11. this.values = [];
  12. base.call(this);
  13. }, klass = PushAccumulator, Accumulator = require("./Accumulator"), base = Accumulator, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  14. proto.processInternal = function processInternal(input, merging) {
  15. if (!merging) {
  16. if (input !== undefined) {
  17. this.values.push(input);
  18. }
  19. } else {
  20. // If we're merging, we need to take apart the arrays we
  21. // receive and put their elements into the array we are collecting.
  22. // If we didn't, then we'd get an array of arrays, with one array
  23. // from each merge source.
  24. if (!Array.isArray(input)) throw new Error("Assertion failure");
  25. Array.prototype.push.apply(this.values, input);
  26. }
  27. };
  28. proto.getValue = function getValue(toBeMerged) {
  29. return this.values;
  30. };
  31. proto.reset = function reset() {
  32. this.values = [];
  33. };
  34. klass.create = function create() {
  35. return new PushAccumulator();
  36. };
  37. proto.getOpName = function getOpName() {
  38. return "$push";
  39. };