AvgAccumulator.js 832 B

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