Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Collection<K, V>

Collections serve as data holders throughout the library. They are a combination of JavaScript Maps and Arrays with the ability to hold large amount of data.

template

V

Type parameters

  • K

  • V

Hierarchy

Index

Constructors

constructor

  • new Collection(entries?: Iterable<keyof [K, V]> | null, limit?: undefined | number): Collection

Properties

Readonly [Symbol.toStringTag]

[Symbol.toStringTag]: string

Readonly limit

limit: number | null | undefined

The maximum number of items allowed in this Collection

Readonly size

size: number

Static Map

Map: MapConstructor

Accessors

first

  • get first(): V | undefined

firstKey

  • get firstKey(): K | undefined

last

  • get last(): V | undefined

lastKey

  • get lastKey(): K | undefined

toArray

  • get toArray(): V[]

toArrayEntries

  • get toArrayEntries(): [K, V][]

toArrayKeys

  • get toArrayKeys(): K[]

Methods

[Symbol.iterator]

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

    Returns IterableIterator<[K, V]>

clear

  • clear(): void
  • Returns void

delete

  • delete(key: K): boolean
  • Parameters

    • key: K

    Returns boolean

entries

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

    Returns IterableIterator<[K, V]>

every

  • every(cb: (value: V, key: K, collection: this) => boolean): boolean

filter

  • filter<T>(cb: (value: V, key?: K, collection?: this) => boolean): Collection<K, T>
  • Creates a new Collection with all elements that pass the test implemented by the provided callback. Identical to {@link Array.prototype.filter}

    Type parameters

    • T: V

    Parameters

    • cb: (value: V, key?: K, collection?: this) => boolean

      Callback function. Return true to keep the element, false otherwise

        • (value: V, key?: K, collection?: this): boolean
        • Parameters

          • value: V
          • Optional key: K
          • Optional collection: this

          Returns boolean

    Returns Collection<K, T>

find

  • find(cb: (value: V, key: K, collection: this) => boolean): V | undefined

forEach

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

    • callbackfn: (value: V, key: K, map: Map<K, V>) => void
        • (value: V, key: K, map: Map<K, V>): void
        • Parameters

          • value: V
          • key: K
          • map: Map<K, V>

          Returns void

    • Optional thisArg: any

    Returns void

get

  • get(key: K): V | undefined
  • Parameters

    • key: K

    Returns V | undefined

getMany

  • getMany(keys: K[]): (undefined | V)[]
  • Returns the matching values for the given keys inside the Collection.

    Parameters

    • keys: K[]

      Array of all keys to look for

    Returns (undefined | V)[]

getOrSet

  • getOrSet(key: K, value: V): V
  • Returns the item if exists or creates a new one and caches it.

    Parameters

    • key: K

      The key of the item

    • value: V

      The value of the item

    Returns V

has

  • has(key: K): boolean
  • Parameters

    • key: K

    Returns boolean

keys

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

    Returns IterableIterator<K>

map

  • map<R>(cb: (value: V, key: K, collection: this) => R): Collection<K, R>
  • Create a new Collection populated with the results of calling a provided callback on every element in the calling Collection. Identical to {@link Array.prototype.map}

    template

    V

    Type parameters

    • R

      is the type of the values the new Collection will contain

    Parameters

    • cb: (value: V, key: K, collection: this) => R

      Callback function. The returned value is added to the new Collection

        • (value: V, key: K, collection: this): R
        • Parameters

          • value: V
          • key: K
          • collection: this

          Returns R

    Returns Collection<K, R>

merge

  • merge(...collections: (Collection<K, V> | [K, V][])[]): void
  • ֛Merges collection(s) on top of this one. Replaces existing keys by newer Collections

    Parameters

    • Rest ...collections: (Collection<K, V> | [K, V][])[]

      The collection(s) to be merged on top of this one

    Returns void

pop

  • pop(): [K, V] | undefined

set

  • set(key: K, value: V, force?: undefined | false | true): this
  • Checks if the Collection has reached its limit and sets the item using {@link Map.prototype.set}

    Parameters

    • key: K

      The key to set

    • value: V

      The value to set

    • Optional force: undefined | false | true

      Whether to add the item to the Collection if its limit was reached

    Returns this

shift

  • shift(): [K, V] | undefined

some

  • some(cb: (value: V, key: K, collection: this) => boolean): boolean

values

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

    Returns IterableIterator<V>

Static isCollection

  • isCollection<K, V>(collection: unknown): collection is Collection<K, V>
  • Whether the given argument is a Collection

    Type parameters

    • K

    • V

    Parameters

    • collection: unknown

      Data to check if collection

    Returns collection is Collection<K, V>

Generated using TypeDoc