FirstAccumulator.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. "use strict";
  2. /**
  3. * Constructor for FirstAccumulator, wraps Accumulator and adds flag to track whether we have started or not
  4. * @class FirstAccumulator
  5. * @namespace mungedb-aggregate.pipeline.accumulators
  6. * @module mungedb-aggregate
  7. * @constructor
  8. **/
  9. var FirstAccumulator = module.exports = function FirstAccumulator(){
  10. if (arguments.length !== 0) throw new Error("zero args expected");
  11. this.reset();
  12. base.call(this);
  13. }, klass = FirstAccumulator, base = require("./Accumulator"), proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  14. proto.processInternal = function processInternal(input, merging) {
  15. // only remember the first value seen
  16. if (!this._haveFirst) {
  17. this._haveFirst = true;
  18. this._first = input;
  19. }
  20. };
  21. proto.getValue = function getValue(toBeMerged) {
  22. return this._first;
  23. };
  24. proto.reset = function reset() {
  25. this._haveFirst = false;
  26. this._first = undefined;
  27. };
  28. klass.create = function create() {
  29. return new FirstAccumulator();
  30. };
  31. proto.getOpName = function getOpName() {
  32. return "$first";
  33. };