AvgAccumulator.js 1005 B

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