UNPKG

@testyard/stats

Version:
133 lines (130 loc) 4.26 kB
/*! * @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'; const toString = (bigint) => { return bigint.toString() + 'n'; }; class BigIntStats { constructor() { this._result = {}; this._numEntries = 0; this._lowestValue = NaN; this._highestValue = NaN; this._sumValue = NaN; this._numAdded = 0; this._isSummarized = false; this.add = (int) => { const bigintAsString = toString(int); if (!(bigintAsString in this._result)) { this._result[bigintAsString] = 0; } this._result[bigintAsString]++; this._numAdded++; }; } get result() { // @ts-expect-error const object = { of: (num) => { this._summarize(); const bigintAsString = toString(num); if (!(bigintAsString in this._result)) { throw new Error(`bigint ${bigintAsString} is not in the results.`); } return this._result[bigintAsString]; } }; const props = { entries: { get: () => { this._summarize(); return { ...this._result }; } }, bigints: { get: () => { this._summarize(); return Object .keys(this._result) .map(value => BigInt(value.slice(0, -1))) .sort((a, b) => { if (a > b) { return 1; } else if (a < b) { return -1; } return 0; }) .map(value => toString(value)); } }, 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 = BigIntStats; //# sourceMappingURL=index.js.map