@testyard/stats
Version:
Value stats tester.
119 lines (116 loc) • 3.7 kB
JavaScript
/*!
* @testyard/stats v1.4.1
* https://github.com/testyardjs/stats
*
* Copyright (c) 2023 Richard King <richrdkng@gmail.com> (https://richrdkng.com)
* Released under the MIT License
* https://github.com/testyardjs/stats/blob/main/LICENSE
*
* Date: 2023-07-19T13:11:26.750Z
*/
'use strict';
class IntegerStats {
constructor() {
this._result = {};
this._numEntries = 0;
this._lowestValue = NaN;
this._highestValue = NaN;
this._sumValue = NaN;
this._numAdded = 0;
this._isSummarized = false;
this.add = (int) => {
if (!(int in this._result)) {
this._result[int] = 0;
}
this._result[int]++;
this._numAdded++;
};
}
get result() {
// @ts-expect-error
const object = {
of: (num) => {
this._summarize();
if (!(num in this._result)) {
throw new Error(`num ${num} is not in the results.`);
}
return this._result[num];
}
};
const props = {
entries: {
get: () => {
this._summarize();
return { ...this._result };
}
},
integers: {
get: () => {
this._summarize();
return Object
.keys(this._result)
.map(value => parseInt(value, 10))
.sort((a, b) => a - b);
}
},
numEntries: {
get: () => {
this._summarize();
return this._numEntries;
}
},
lowestValue: {
get: () => {
this._summarize();
return this._lowestValue;
}
},
highestValue: {
get: () => {
this._summarize();
return this._highestValue;
}
},
averageValue: {
get: () => {
this._summarize();
return this._sumValue / this._numEntries;
}
}
};
Object.defineProperties(object, props);
return object;
}
_summarize() {
if (!this._isSummarized) {
for (const [key, value] of Object.entries(this._result)) {
this._result[key] = value / this._numAdded;
}
this._numEntries = Object.keys(this._result).length;
for (const value of Object.values(this._result)) {
// eslint-disable-next-line no-self-compare
if (this._lowestValue !== this._lowestValue) {
this._lowestValue = value;
}
if (this._lowestValue > value) {
this._lowestValue = value;
}
// eslint-disable-next-line no-self-compare
if (this._highestValue !== this._highestValue) {
this._highestValue = value;
}
if (this._highestValue < value) {
this._highestValue = value;
}
// eslint-disable-next-line no-self-compare
if (this._sumValue !== this._sumValue) {
this._sumValue = 0;
}
this._sumValue += value;
}
this._isSummarized = true;
}
}
}
module.exports = IntegerStats;
//# sourceMappingURL=index.js.map