Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Stack<K, V>

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

Type parameters

  • K: string | number | unknown

  • V

Hierarchy

  • Collection<K, V>
    • Stack

Index

Properties

constructor

  • new Stack(entries?: ReadonlyArray<keyof []> | null): Stack

Readonly size

size: number

Static Readonly default

default: typeof Stack = Stack

Methods

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
  • Identical to Map.clear(). Removes all elements from the collection.

    Returns void

clone

  • clone(): this
  • Creates an identical shallow copy of this collection.

    example

    const newColl = someColl.clone();

    Returns this

concat

  • concat(...collections: Collection<K, V>[]): this
  • 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 this

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

  • difference(other: Collection<K, V>): Collection<K, V>
  • 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

  • each(fn: (value: V, key: K, collection: this) => void): this
  • each<T>(fn: (this: T, value: V, key: K, collection: this) => void, thisArg: T): this
  • Identical to Map.forEach(), but returns the collection instead of undefined.

    example

    collection .each(user => console.log(user.username)) .filter(user => user.bot) .each(user => console.log(user.username));

    Parameters

    • fn: (value: V, key: K, collection: this) => void

      Function to execute for each element

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

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

          Returns void

    Returns this

  • Type parameters

    • T

    Parameters

    • fn: (this: T, value: V, key: K, collection: this) => void
        • (this: T, value: V, key: K, collection: this): void
        • Parameters

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

          Returns void

    • thisArg: T

    Returns this

equals

  • equals(collection: Collection<K, V>): boolean
  • 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: this) => boolean): boolean
  • every<T>(fn: (this: T, value: V, key: K, collection: this) => boolean, thisArg: T): boolean
  • Checks if all items passes a test. Identical in behavior to Array.every().

    example

    collection.every(user => !user.bot);

    Parameters

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

      Function used to test (should return a boolean)

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

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

          Returns boolean

    Returns boolean

  • Type parameters

    • T

    Parameters

    • fn: (this: T, value: V, key: K, collection: this) => boolean
        • (this: T, value: V, key: K, collection: this): boolean
        • Parameters

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

          Returns boolean

    • thisArg: T

    Returns boolean

filter

  • filter(fn: (value: V, key: K, collection: this) => boolean): this
  • filter<T>(fn: (this: T, value: V, key: K, collection: this) => boolean, thisArg: T): this
  • Identical to Array.filter(), but returns a Collection instead of an Array.

    example

    collection.filter(user => user.username === 'Bob');

    Parameters

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

      The function to test with (should return boolean)

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

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

          Returns boolean

    Returns this

  • Type parameters

    • T

    Parameters

    • fn: (this: T, value: V, key: K, collection: this) => boolean
        • (this: T, value: V, key: K, collection: this): boolean
        • Parameters

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

          Returns boolean

    • thisArg: T

    Returns this

find

  • find(fn: (value: V, key: K, collection: this) => boolean): V | undefined
  • find<T>(fn: (this: T, value: V, key: K, collection: this) => boolean, thisArg: T): V | undefined
  • 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.

    example

    collection.find(user => user.username === 'Bob');

    Parameters

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

      The function to test with (should return boolean)

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

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

          Returns boolean

    Returns V | undefined

  • Type parameters

    • T

    Parameters

    • fn: (this: T, value: V, key: K, collection: this) => boolean
        • (this: T, value: V, key: K, collection: this): boolean
        • Parameters

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

          Returns boolean

    • thisArg: T

    Returns V | undefined

findKey

  • findKey(fn: (value: V, key: K, collection: this) => boolean): K | undefined
  • findKey<T>(fn: (this: T, value: V, key: K, collection: this) => boolean, thisArg: T): K | undefined
  • 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.

    example

    collection.findKey(user => user.username === 'Bob');

    Parameters

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

      The function to test with (should return boolean)

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

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

          Returns boolean

    Returns K | undefined

  • Type parameters

    • T

    Parameters

    • fn: (this: T, value: V, key: K, collection: this) => boolean
        • (this: T, value: V, key: K, collection: this): boolean
        • Parameters

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

          Returns boolean

    • thisArg: T

    Returns K | undefined

first

  • first(): V | undefined
  • first(amount: number): V[]
  • Obtains the first value(s) in this collection.

    Returns V | undefined

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

  • Parameters

    • amount: number

    Returns V[]

firstKey

  • firstKey(): K | undefined
  • firstKey(amount: number): K[]
  • Obtains the first key(s) in this collection.

    Returns K | undefined

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

  • Parameters

    • amount: number

    Returns K[]

flatMap

  • flatMap<T>(fn: (value: V, key: K, collection: this) => Collection<K, T>): Collection<K, T>
  • flatMap<T, This>(fn: (this: This, value: V, key: K, collection: this) => Collection<K, T>, thisArg: This): Collection<K, T>
  • Maps each item into a Collection, then joins the results into a single Collection. Identical in behavior to Array.flatMap().

    example

    collection.flatMap(guild => guild.members.cache);

    Type parameters

    • T

    Parameters

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

      Function that produces a new Collection

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

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

          Returns Collection<K, T>

    Returns Collection<K, T>

  • Type parameters

    • T

    • This

    Parameters

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

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

          Returns Collection<K, T>

    • thisArg: This

    Returns Collection<K, T>

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
  • 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 V | undefined

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

  • intersect(other: Collection<K, V>): Collection<K, V>
  • 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[]

last

  • last(): V | undefined
  • 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 V | undefined

    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(): K | undefined
  • 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 K | undefined

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

    example

    collection.map(user => user.tag);

    Type parameters

    • T

    Parameters

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

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

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

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

          Returns T

    Returns T[]

  • Type parameters

    • This

    • T

    Parameters

    • fn: (this: This, value: V, key: K, collection: this) => T
        • (this: This, value: V, key: K, collection: this): T
        • Parameters

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

          Returns T

    • thisArg: This

    Returns T[]

mapValues

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

    example

    collection.mapValues(user => user.tag);

    Type parameters

    • T

    Parameters

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

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

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

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

          Returns T

    Returns Collection<K, T>

  • Type parameters

    • This

    • T

    Parameters

    • fn: (this: This, value: V, key: K, collection: this) => T
        • (this: This, value: V, key: K, collection: this): T
        • Parameters

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

          Returns T

    • thisArg: This

    Returns Collection<K, T>

partition

  • partition(fn: (value: V, key: K, collection: this) => boolean): []
  • partition<T>(fn: (this: T, value: V, key: K, collection: this) => boolean, thisArg: T): []
  • Partitions the collection into two collections where the first collection contains the items that passed and the second contains the items that failed.

    example

    const [big, small] = collection.partition(guild => guild.memberCount > 250);

    Parameters

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

      Function used to test (should return a boolean)

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

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

          Returns boolean

    Returns []

  • Type parameters

    • T

    Parameters

    • fn: (this: T, value: V, key: K, collection: this) => boolean
        • (this: T, value: V, key: K, collection: this): boolean
        • Parameters

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

          Returns boolean

    • thisArg: T

    Returns []

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: this) => T, initialValue?: T): T
  • Applies a function to produce a single value. Identical in behavior to Array.reduce().

    example

    collection.reduce((acc, guild) => acc + guild.memberCount, 0);

    Type parameters

    • T

    Parameters

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

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

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

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

          Returns T

    • Optional initialValue: T

    Returns T

set

  • set(key: K, value: V): this
  • 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 this

some

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

    example

    collection.some(user => user.discriminator === '0000');

    Parameters

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

      Function used to test (should return a boolean)

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

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

          Returns boolean

    Returns boolean

  • Type parameters

    • T

    Parameters

    • fn: (this: T, value: V, key: K, collection: this) => boolean
        • (this: T, value: V, key: K, collection: this): boolean
        • Parameters

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

          Returns boolean

    • thisArg: T

    Returns boolean

sort

  • sort(compareFunction?: undefined | ((firstValue: V, secondValue: V, firstKey: K, secondKey: K) => number)): this
  • 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.

    example

    collection.sort((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);

    Parameters

    • Optional compareFunction: undefined | ((firstValue: V, secondValue: V, firstKey: K, secondKey: K) => number)

    Returns this

sorted

  • sorted(compareFunction?: undefined | ((firstValue: V, secondValue: V, firstKey: K, secondKey: K) => number)): this
  • 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.

    example

    collection.sorted((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);

    Parameters

    • Optional compareFunction: undefined | ((firstValue: V, secondValue: V, firstKey: K, secondKey: K) => number)

    Returns this

sweep

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

    Parameters

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

      Function used to test (should return a boolean)

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

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

          Returns boolean

    Returns number

    The number of removed entries

  • Type parameters

    • T

    Parameters

    • fn: (this: T, value: V, key: K, collection: this) => boolean
        • (this: T, value: V, key: K, collection: this): boolean
        • Parameters

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

          Returns boolean

    • thisArg: T

    Returns number

tap

  • tap(fn: (collection: this) => void): this
  • tap<T>(fn: (this: T, collection: this) => void, thisArg: T): this
  • Runs a function on the collection and returns the collection.

    example

    collection .tap(coll => console.log(coll.size)) .filter(user => user.bot) .tap(coll => console.log(coll.size))

    Parameters

    • fn: (collection: this) => void

      Function to execute

        • (collection: this): void
        • Parameters

          • collection: this

          Returns void

    Returns this

  • Type parameters

    • T

    Parameters

    • fn: (this: T, collection: this) => void
        • (this: T, collection: this): void
        • Parameters

          • this: T
          • collection: this

          Returns void

    • thisArg: T

    Returns this

toJSON

  • toJSON(): V[]
  • Creates a JSON representation of the Stack This is used internally by Redux and ensures that Stacks can be used for Redux.

    Returns V[]

Generated using TypeDoc