Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Collection<K, V>

A Map with additional utility methods. This is used throughout DogeHQ rather than Arrays for anything that has an ID, for significantly improved performance and ease-of-use.

property

{number} size - The amount of elements in this collection.

Type parameters

  • K

  • V

Hierarchy

  • Map<K, V>
    • Collection

Index

Constructors

constructor

  • new Collection<K, V>(entries?: null | readonly readonly [K, V][]): Collection<K, V>

Properties

Readonly [Symbol.toStringTag]

[Symbol.toStringTag]: string

Private _array

_array: null | V[]

Private _keyArray

_keyArray: null | K[]

constructor

constructor: typeof Collection

Readonly size

size: number

Static Readonly [Symbol.species]

[Symbol.species]: MapConstructor

Static Readonly default

default: typeof Collection = ...

Methods

[Symbol.iterator]

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

    Returns IterableIterator<[K, V]>

array

  • array(): V[]
  • Creates an ordered array of the values of this collection, and caches it internally. The array will only be reconstructed if an item is added to or removed from the collection, or if you change the length of the array itself. If you don't want this caching behavior, use [...collection.values()] or Array.from(collection.values()) instead.

    Returns V[]

clear

  • clear(): void

clone

concat

  • concat(...collections: Collection<K, V>[]): Collection<K, V>
  • Combines this collection with others into a new collection. None of the source collections are modified.

    example

    const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl);

    Parameters

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

      Collections to merge

    Returns Collection<K, V>

delete

  • delete(key: K): boolean
  • Identical to Map.delete(). Deletes an element from the collection.

    Parameters

    • key: K

      The key to delete from the collection

    Returns boolean

    true if the element was removed, false if the element does not exist.

difference

  • The difference method returns a new structure containing items where the key is present in one of the original structures but not the other.

    Parameters

    • other: Collection<K, V>

      The other Collection to filter against

    Returns Collection<K, V>

each

entries

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

    Returns IterableIterator<[K, V]>

equals

  • Checks if this collection shares identical items with another. This is different to checking for equality using equal-signs, because the collections may be different objects, but contain the same data.

    Parameters

    • collection: Collection<K, V>

      Collection to compare with

    Returns boolean

    Whether the collections have identical contents

every

  • every(fn: (value: V, key: K, collection: Collection<K, V>) => boolean): boolean
  • every<T>(fn: (value: V, key: K, collection: Collection<K, V>) => boolean, thisArg: T): boolean

filter

find

  • find(fn: (value: V, key: K, collection: Collection<K, V>) => boolean): undefined | V
  • find<T>(fn: (value: V, key: K, collection: Collection<K, V>) => boolean, thisArg: T): undefined | V
  • Searches for a single item where the given function returns a truthy value. This behaves like Array.find(). All collections used in Discord.js are mapped using their id property, and if you want to find by id you should use the get method. See MDN for details.

    Parameters

    • fn: (value: V, key: K, collection: Collection<K, V>) => boolean

      The function to test with (should return boolean)

        • (value: V, key: K, collection: Collection<K, V>): boolean
        • Parameters

          Returns boolean

    Returns undefined | V

  • Type parameters

    • T

    Parameters

    • fn: (value: V, key: K, collection: Collection<K, V>) => boolean
        • (value: V, key: K, collection: Collection<K, V>): boolean
        • Parameters

          Returns boolean

    • thisArg: T

    Returns undefined | V

findKey

  • findKey(fn: (value: V, key: K, collection: Collection<K, V>) => boolean): undefined | K
  • findKey<T>(fn: (value: V, key: K, collection: Collection<K, V>) => boolean, thisArg: T): undefined | K
  • Searches for the key of a single item where the given function returns a truthy value. This behaves like Array.findIndex(), but returns the key rather than the positional index.

    Parameters

    • fn: (value: V, key: K, collection: Collection<K, V>) => boolean

      The function to test with (should return boolean)

        • (value: V, key: K, collection: Collection<K, V>): boolean
        • Parameters

          Returns boolean

    Returns undefined | K

  • Type parameters

    • T

    Parameters

    • fn: (value: V, key: K, collection: Collection<K, V>) => boolean
        • (value: V, key: K, collection: Collection<K, V>): boolean
        • Parameters

          Returns boolean

    • thisArg: T

    Returns undefined | K

first

  • first(): undefined | V
  • first(amount: number): V[]

firstKey

  • firstKey(): undefined | K
  • firstKey(amount: number): K[]

flatMap

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): undefined | V
  • Identical to Map.get(). Gets an element with the specified key, and returns its value, or undefined if the element does not exist.

    Parameters

    • key: K

      The key to get from this collection

    Returns undefined | V

has

  • has(key: K): boolean
  • Identical to Map.has(). Checks if an element exists in the collection.

    Parameters

    • key: K

      The key of the element to check for

    Returns boolean

    true if the element exists, false if it does not exist.

intersect

  • The intersect method returns a new structure containing items where the keys are present in both original structures.

    Parameters

    • other: Collection<K, V>

      The other Collection to filter against

    Returns Collection<K, V>

keyArray

  • keyArray(): K[]
  • Creates an ordered array of the keys of this collection, and caches it internally. The array will only be reconstructed if an item is added to or removed from the collection, or if you change the length of the array itself. If you don't want this caching behavior, use [...collection.keys()] or Array.from(collection.keys()) instead.

    Returns K[]

keys

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

    Returns IterableIterator<K>

last

  • last(): undefined | V
  • last(amount: number): V[]
  • Obtains the last value(s) in this collection. This relies on {@link Collection#array}, and thus the caching mechanism applies here as well.

    Returns undefined | V

    A single value if no amount is provided or an array of values, starting from the start if amount is negative

  • Parameters

    • amount: number

    Returns V[]

lastKey

  • lastKey(): undefined | K
  • lastKey(amount: number): K[]
  • Obtains the last key(s) in this collection. This relies on {@link Collection#keyArray}, and thus the caching mechanism applies here as well.

    Returns undefined | K

    A single key if no amount is provided or an array of keys, starting from the start if amount is negative

  • Parameters

    • amount: number

    Returns K[]

map

  • map<T>(fn: (value: V, key: K, collection: Collection<K, V>) => T): T[]
  • map<This, T>(fn: (value: V, key: K, collection: Collection<K, V>) => T, thisArg: This): T[]
  • Maps each item to another value into an array. Identical in behavior to Array.map().

    Type parameters

    • T

    Parameters

    • fn: (value: V, key: K, collection: Collection<K, V>) => T

      Function that produces an element of the new array, taking three arguments

        • (value: V, key: K, collection: Collection<K, V>): T
        • Parameters

          Returns T

    Returns T[]

  • Type parameters

    • This

    • T

    Parameters

    • fn: (value: V, key: K, collection: Collection<K, V>) => T
        • (value: V, key: K, collection: Collection<K, V>): T
        • Parameters

          Returns T

    • thisArg: This

    Returns T[]

mapValues

  • mapValues<T>(fn: (value: V, key: K, collection: Collection<K, V>) => T): Collection<K, T>
  • mapValues<This, T>(fn: (value: V, key: K, collection: Collection<K, V>) => T, thisArg: This): Collection<K, T>

partition

random

  • random(): V
  • random(amount: number): V[]
  • Obtains unique random value(s) from this collection. This relies on {@link Collection#array}, and thus the caching mechanism applies here as well.

    Returns V

    A single value if no amount is provided or an array of values

  • Parameters

    • amount: number

    Returns V[]

randomKey

  • randomKey(): K
  • randomKey(amount: number): K[]
  • Obtains unique random key(s) from this collection. This relies on {@link Collection#keyArray}, and thus the caching mechanism applies here as well.

    Returns K

    A single key if no amount is provided or an array

  • Parameters

    • amount: number

    Returns K[]

reduce

  • reduce<T>(fn: (accumulator: T, value: V, key: K, collection: Collection<K, V>) => T, initialValue?: T): T
  • Applies a function to produce a single value. Identical in behavior to Array.reduce().

    Type parameters

    • T

    Parameters

    • fn: (accumulator: T, value: V, key: K, collection: Collection<K, V>) => T

      Function used to reduce, taking four arguments; accumulator, currentValue, currentKey, and collection

        • (accumulator: T, value: V, key: K, collection: Collection<K, V>): T
        • Parameters

          • accumulator: T
          • value: V
          • key: K
          • collection: Collection<K, V>

          Returns T

    • Optional initialValue: T

    Returns T

set

  • Identical to Map.set(). Sets a new element in the collection with the specified key and value.

    Parameters

    • key: K

      The key of the element to add

    • value: V

      The value of the element to add

    Returns Collection<K, V>

some

  • some(fn: (value: V, key: K, collection: Collection<K, V>) => boolean): boolean
  • some<T>(fn: (value: V, key: K, collection: Collection<K, V>) => boolean, thisArg: T): boolean
  • Checks if there exists an item that passes a test. Identical in behavior to Array.some().

    Parameters

    • fn: (value: V, key: K, collection: Collection<K, V>) => boolean

      Function used to test (should return a boolean)

        • (value: V, key: K, collection: Collection<K, V>): boolean
        • Parameters

          Returns boolean

    Returns boolean

  • Type parameters

    • T

    Parameters

    • fn: (value: V, key: K, collection: Collection<K, V>) => boolean
        • (value: V, key: K, collection: Collection<K, V>): boolean
        • Parameters

          Returns boolean

    • thisArg: T

    Returns boolean

sort

  • sort(compareFunction?: (firstValue: V, secondValue: V, firstKey: K, secondKey: K) => number): Collection<K, V>
  • The sort method sorts the items of a collection in place and returns it. The sort is not necessarily stable in Node 10 or older. The default sort order is according to string Unicode code points.

    Parameters

    • compareFunction: (firstValue: V, secondValue: V, firstKey: K, secondKey: K) => number = ...
        • (firstValue: V, secondValue: V, firstKey: K, secondKey: K): number
        • Parameters

          • firstValue: V
          • secondValue: V
          • firstKey: K
          • secondKey: K

          Returns number

    Returns Collection<K, V>

sorted

  • sorted(compareFunction?: (firstValue: V, secondValue: V, firstKey: K, secondKey: K) => number): Collection<K, V>
  • The sorted method sorts the items of a collection and returns it. The sort is not necessarily stable in Node 10 or older. The default sort order is according to string Unicode code points.

    Parameters

    • compareFunction: (firstValue: V, secondValue: V, firstKey: K, secondKey: K) => number = ...
        • (firstValue: V, secondValue: V, firstKey: K, secondKey: K): number
        • Parameters

          • firstValue: V
          • secondValue: V
          • firstKey: K
          • secondKey: K

          Returns number

    Returns Collection<K, V>

sweep

  • sweep(fn: (value: V, key: K, collection: Collection<K, V>) => boolean): number
  • sweep<T>(fn: (value: V, key: K, collection: Collection<K, V>) => boolean, thisArg: T): number
  • Removes items that satisfy the provided filter function.

    Parameters

    • fn: (value: V, key: K, collection: Collection<K, V>) => boolean

      Function used to test (should return a boolean)

        • (value: V, key: K, collection: Collection<K, V>): boolean
        • Parameters

          Returns boolean

    Returns number

    The number of removed entries

  • Type parameters

    • T

    Parameters

    • fn: (value: V, key: K, collection: Collection<K, V>) => boolean
        • (value: V, key: K, collection: Collection<K, V>): boolean
        • Parameters

          Returns boolean

    • thisArg: T

    Returns number

tap

values

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

    Returns IterableIterator<V>

Generated using TypeDoc