UNPKG

@bitblit/ratchet-common

Version:

Common tools for general use

49 lines 1.33 kB
export class Histogram { entries = []; update(val, addValue = 1) { if (val) { const entry = this.entries.find((e) => e.item === val); if (entry) { entry.count += addValue; } else { this.entries.push({ item: val, count: addValue, }); } } } sort() { this.entries.sort((a, b) => { let rval = b.count - a.count; if (rval === 0) { rval = String(b.item).localeCompare(String(a.item)); } return rval; }); } sortKeys() { this.entries.sort((a, b) => String(b.item).localeCompare(String(a.item))); } reverse() { this.entries.reverse(); } getEntries() { return this.entries; } getTotalCount() { let rval = 0; this.entries.forEach((h) => (rval += h.count)); return rval; } countForValue(val) { const entry = this.entries.find((test) => test.item === val); return entry ? entry.count : 0; } percentForValue(val) { const total = this.getTotalCount(); return total === 0 ? 0 : this.countForValue(val) / total; } } //# sourceMappingURL=histogram.js.map