Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Counter<T>

A Counter is a Map subclass for counting objects. Elements are stored as keys and their counts are stored as values. This is similar to Python's collections.Counter.

const counter = new Counter(['a', 'a', 'b']);
counter.get('a') //=> 2
counter.update(['a'])
counter.get('a') //=> 3

Type parameters

  • T

    Object to be counted. Used as key in the Map.

Hierarchy

  • Map<T, number>
    • Counter

Index

Constructors

constructor

  • new Counter<T>(iterableOrMap?: Iterable<T> | Map<T, number>): Counter<T>
  • Type parameters

    • T

    Parameters

    • Optional iterableOrMap: Iterable<T> | Map<T, number>

    Returns Counter<T>

Properties

Readonly [Symbol.toStringTag]

[Symbol.toStringTag]: string

Readonly size

size: number

Static Readonly [Symbol.species]

[Symbol.species]: MapConstructor

Methods

[Symbol.iterator]

  • [Symbol.iterator](): IterableIterator<[T, number]>
  • Returns an iterable of entries in the map.

    Returns IterableIterator<[T, number]>

clear

  • clear(): void
  • Returns void

delete

  • delete(key: T): boolean
  • Parameters

    • key: T

    Returns boolean

entries

  • entries(): IterableIterator<[T, number]>
  • Returns an iterable of key, value pairs for every entry in the map.

    Returns IterableIterator<[T, number]>

forEach

  • forEach(callbackfn: (value: number, key: T, map: Map<T, number>) => void, thisArg?: any): void
  • Parameters

    • callbackfn: (value: number, key: T, map: Map<T, number>) => void
        • (value: number, key: T, map: Map<T, number>): void
        • Parameters

          • value: number
          • key: T
          • map: Map<T, number>

          Returns void

    • Optional thisArg: any

    Returns void

get

  • get(key: T): number

has

  • has(key: T): boolean
  • Parameters

    • key: T

    Returns boolean

keys

  • keys(): IterableIterator<T>
  • Returns an iterable of keys in the map

    Returns IterableIterator<T>

set

  • set(key: T, value: number): Counter<T>
  • Parameters

    • key: T
    • value: number

    Returns Counter<T>

sorted

  • sorted(): [T, number][]

sum

  • sum(): number

update

  • update(iterableOrMap: Iterable<T> | Map<T, number>): Counter<T>

values

  • values(): IterableIterator<number>
  • Returns an iterable of values in the map

    Returns IterableIterator<number>

Generated using TypeDoc