counter.ts 431 B

1234567891011121314151617181920
  1. export class Counter {
  2. private readonly count = new Map<string, number>();
  3. add(key: string, amount: number): void {
  4. this.count.set(key, this.get(key) + amount);
  5. }
  6. get(key: string): number {
  7. return this.count.get(key) ?? 0;
  8. }
  9. update(other: Counter): void {
  10. for (const [key, amount] of other.entries())
  11. this.add(key, amount);
  12. }
  13. entries(): IterableIterator<[string, number]> {
  14. return this.count.entries();
  15. }
  16. }