UNPKG

@naturalcycles/js-lib

Version:

Standard library for universal (browser + Node.js) javascript

35 lines (34 loc) 774 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AccumulatingAverage = void 0; /** * Container that allows to accumulate average of a set of numbers, * without the need to store all of them in memory. * * @experimental */ class AccumulatingAverage { total = 0; count = 0; /** * Returns the current average. * Returns 0 if no values have been added. */ get average() { if (this.count === 0) return 0; return this.total / this.count; } /** * Adds a new value. */ add(value) { this.total += value; this.count++; } reset() { this.total = 0; this.count = 0; } } exports.AccumulatingAverage = AccumulatingAverage;