AvgAccumulator.js 1019 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. "use strict";
  2. var AvgAccumulator = module.exports = (function(){
  3. // Constructor
  4. /**
  5. * A class for constructing accumulators to calculate avg.
  6. *
  7. * @class AvgAccumulator
  8. * @namespace munge.pipeline.accumulators
  9. * @module munge
  10. * @constructor
  11. **/
  12. var klass = module.exports = function AvgAccumulator(){
  13. this.subTotalName = "subTotal";
  14. this.countName = "count";
  15. this.totalIsANumber = true;
  16. base.call(this);
  17. }, SumAccumulator = require("./SumAccumulator"), base = SumAccumulator, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  18. proto.getFactory = function getFactory(){
  19. return klass; // using the ctor rather than a separate .create() method
  20. };
  21. proto.getValue = function getValue(){
  22. if(this.totalIsANumber && this.count > 0)
  23. return this.total/this.count;
  24. else if (this.count === 0)
  25. return 0;
  26. else
  27. throw new Error("$sum resulted in a non-numeric type");
  28. };
  29. proto.getOpName = function getOpName(){
  30. return "$avg";
  31. };
  32. return klass;
  33. })();