Collection

Collection

Inherited from Discord.js. A Map with additional utility methods. This is used throughout ThunderAPI rather than Arrays for significantly improved performance and ease-of-use.

Constructor

new Collection()

Source:

Extends

  • Map

Members

(private, nullable) _array :Array

Source:

Cached array for the array() method - will be reset to null whenever set() or delete() are called

Type:
  • Array

(private, nullable) _keyArray :Array

Source:

Cached array for the keyArray() method - will be reset to null whenever set() or delete() are called

Type:
  • Array

Methods

array() → {Array}

Source:

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 behaviour, use [...collection.values()] or Array.from(collection.values()) instead.

Returns:
Type
Array

clone() → {Collection}

Source:

Creates an identical shallow copy of this collection.

Example
const newColl = someColl.clone();
Returns:
Type
Collection

concat(…collections) → {Collection}

Source:

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:
Name Type Attributes Description
collections Collection <repeatable>

Collections to merge

Returns:
Type
Collection

deleteAll() → {Array.<Promise>}

Source:

Calls the delete() method on all items that have it.

Returns:
Type
Array.<Promise>

equals(collection) → {boolean}

Source:

Checks if this collection shares identical key-value pairings 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:
Name Type Description
collection Collection

Collection to compare with

Returns:

Whether the collections have identical contents

Type
boolean

every(fn, thisArgopt) → {boolean}

Source:

Identical to Array.every().

Parameters:
Name Type Attributes Description
fn function

Function used to test (should return a boolean)

thisArg Object <optional>

Value to use as this when executing function

Returns:
Type
boolean

exists(propOrFn, valueopt) → {boolean}

Source:

Searches for the existence of a single item where its specified property"s value is identical to the given value (item[prop] === value), or the given function returns a truthy value.

Do not use this to check for an item by its ID. Instead, use collection.has(id). See MDN for details.
Examples
if (collection.exists("username", "Bob")) {
 console.log("user here!");
}
if (collection.exists(user => user.username === "Bob")) {
 console.log("user here!");
}
Parameters:
Name Type Attributes Description
propOrFn string | function

The property to test against, or the function to test with

value * <optional>

The expected value - only applicable and required if using a property for the first argument

Returns:
Type
boolean

filter(fn, thisArgopt) → {Collection}

Source:

Identical to Array.filter(), but returns a Collection instead of an Array.

Parameters:
Name Type Attributes Description
fn function

Function used to test (should return a boolean)

thisArg Object <optional>

Value to use as this when executing function

Returns:
Type
Collection

filterArray(fn, thisArgopt) → {Array}

Source:

Identical to Array.filter().

Parameters:
Name Type Attributes Description
fn function

Function used to test (should return a boolean)

thisArg Object <optional>

Value to use as this when executing function

Returns:
Type
Array

find(propOrFn, valueopt) → {*}

Source:

Searches for a single item where its specified property"s value is identical to the given value (item[prop] === value), or the given function returns a truthy value. In the latter case, this is identical to 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.
Examples
collection.find("username", "Bob");
collection.find(val => val.username === "Bob");
Parameters:
Name Type Attributes Description
propOrFn string | function

The property to test against, or the function to test with

value * <optional>

The expected value - only applicable and required if using a property for the first argument

Returns:
Type
*

findAll(prop, value) → {Array}

Source:

Searches for all items where their specified property"s value is identical to the given value (item[prop] === value).

Example
collection.findAll("username", "Bob");
Parameters:
Name Type Description
prop string

The property to test against

value *

The expected value

Returns:
Type
Array

first(amountopt) → {*|Array.<*>}

Source:

Obtains the first value(s) in this collection.

Parameters:
Name Type Attributes Description
amount number <optional>

Amount of values to obtain from the beginning

Returns:

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

Type
* | Array.<*>

firstKey(amountopt) → {*|Array.<*>}

Source:

Obtains the first key(s) in this collection.

Parameters:
Name Type Attributes Description
amount number <optional>

Amount of keys to obtain from the beginning

Returns:

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

Type
* | Array.<*>

keyArray() → {Array}

Source:

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 behaviour, use [...collection.keys()] or Array.from(collection.keys()) instead.

Returns:
Type
Array

last(amountopt) → {*|Array.<*>}

Source:

Obtains the last value(s) in this collection. This relies on Collection#array, and thus the caching mechanism applies here as well.

Parameters:
Name Type Attributes Description
amount number <optional>

Amount of values to obtain from the end

Returns:

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

Type
* | Array.<*>

lastKey(amountopt) → {*|Array.<*>}

Source:

Obtains the last key(s) in this collection. This relies on Collection#keyArray, and thus the caching mechanism applies here as well.

Parameters:
Name Type Attributes Description
amount number <optional>

Amount of keys to obtain from the end

Returns:

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

Type
* | Array.<*>

map(fn, thisArgopt) → {Array}

Source:

Identical to Array.map().

Parameters:
Name Type Attributes Description
fn function

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

thisArg * <optional>

Value to use as this when executing function

Returns:
Type
Array

random(amountopt) → {*|Array.<*>}

Source:

Obtains random value(s) from this collection. This relies on Collection#array, and thus the caching mechanism applies here as well.

Parameters:
Name Type Attributes Description
amount number <optional>

Amount of values to obtain randomly

Returns:

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

Type
* | Array.<*>

randomKey(amountopt) → {*|Array.<*>}

Source:

Obtains random key(s) from this collection. This relies on Collection#keyArray, and thus the caching mechanism applies here as well.

Parameters:
Name Type Attributes Description
amount number <optional>

Amount of keys to obtain randomly

Returns:

A single key if no amount is provided or an array

Type
* | Array.<*>

reduce(fn, initialValueopt) → {*}

Source:

Identical to Array.reduce().

Parameters:
Name Type Attributes Description
fn function

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

initialValue * <optional>

Starting value for the accumulator

Returns:
Type
*

some(fn, thisArgopt) → {boolean}

Source:

Identical to Array.some().

Parameters:
Name Type Attributes Description
fn function

Function used to test (should return a boolean)

thisArg Object <optional>

Value to use as this when executing function

Returns:
Type
boolean

sort(compareFunctionopt) → {Collection}

Source:

The sort() method sorts the elements of a collection and returns it. The sort is not necessarily stable. The default sort order is according to string Unicode code points.

Parameters:
Name Type Attributes Description
compareFunction function <optional>

Specifies a function that defines the sort order. If omitted, the collection is sorted according to each character"s Unicode code point value, according to the string conversion of each element.

Returns:
Type
Collection