UNPKG

foxts

Version:

Opinionated collection of common TypeScript utils by @SukkaW

1 lines 877 B
var t,r;t=Symbol.toStringTag,r=Symbol.iterator;class e{map=new Map;constructor(t){if(Array.isArray(t))for(const[r,e]of t)this.incr(r,e);else if(void 0!==t)for(const r in t)Object.prototype.hasOwnProperty.call(t,r)&&this.incr(r,t[r])}forEach(t,r){this.map.forEach(t,r)}[t]="Counter";get size(){return this.map.size}clear(){this.map.clear()}delete(t){return this.map.delete(t)}has(t){return this.map.has(t)}get(t){const r=this.map.get(t);return void 0===r?0:r}incr(t,r=1){if(r<0)throw TypeError(`Counter#incr only accepts positive values: ${r}`);return this.map.set(t,this.get(t)+r),this}decr(t,r=1){if(r<0)throw TypeError(`Counter#decr only accepts positive values: ${r}`);const e=this.get(t);return e<=r?this.map.delete(t):this.map.set(t,e-r),this}entries(){return this.map.entries()}[r](){return this.map[Symbol.iterator]()}keys(){return this.map.keys()}}export{e as Counter};