A Collection which holds a max amount of entries.
Extends
Collection<K, V>Name | Constraints | Optional | Default | Description |
---|---|---|---|---|
K | No | None | ||
V | No | None |
Name | Type | Optional | Description |
---|---|---|---|
options | LimitedCollectionOptions<K, V> | Yes | None |
iterable | Iterable<readonly [K, V]> | Yes | None |
Identical to Array.at(). Returns the item at a given index, allowing for positive and negative integers. Negative integers count back from the last item in the collection.
Name | Type | Optional | Description |
---|---|---|---|
index | number | No | The index of the element to obtain |
Creates an identical shallow copy of this collection.
Inherited from CollectionExample
Creates a Collection from a list of entries.
Example
Name | Type | Optional | Description |
---|---|---|---|
entries | Iterable<[K, V]> | No | The list of entries |
combine | (firstValue: V, secondValue: V, key: K) => V | No | Function to combine an existing entry with a new one |
Combines this collection with others into a new collection. None of the source collections are modified.
Example
Name | Type | Optional | Description |
---|---|---|---|
...collections | ReadonlyCollection<K, V>[] | No | Collections to merge |
Returns a new collection containing the items where the key is present in this collection but not the other.
Example
Name | Type | Optional | Description |
---|---|---|---|
other | ReadonlyCollection<K, T> | No | The other Collection to filter against |
Identical to Map.forEach(), but returns the collection instead of undefined.
Example
Name | Type | Optional | Description |
---|---|---|---|
fn | (value: V, key: K, collection: this) => void | No | Function to execute for each element |
Obtains the value of the given key if it exists, otherwise sets and returns the value provided by the default value generator.
Example
Name | Type | Optional | Description |
---|---|---|---|
key | K | No | The key to get if it exists, or set otherwise |
defaultValueGenerator | (key: K, collection: this) => V | No | A function that generates the default value |
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.
Returns
Whether the collections have identical contentsName | Type | Optional | Description |
---|---|---|---|
collection | ReadonlyCollection<K, V> | No | Collection to compare with |
Checks if all items passes a test. Identical in behavior to Array.every().
Example
Name | Type | Optional | Description |
---|---|---|---|
fn | (value: V, key: K, collection: this) => key is K2 | No | Function used to test (should return a boolean) |
Identical to Array.filter(), but returns a Collection instead of an Array.
Example
Name | Type | Optional | Description |
---|---|---|---|
fn | (value: V, key: K, collection: this) => key is K2 | No | The function to test with (should return a boolean) |
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
Name | Type | Optional | Description |
---|---|---|---|
fn | (value: V, key: K, collection: this) => value is V2 | No | The function to test with (should return a boolean) |
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
Name | Type | Optional | Description |
---|---|---|---|
fn | (value: V, key: K, collection: this) => key is K2 | No | The function to test with (should return a boolean) |
Searches for a last item where the given function returns a truthy value. This behaves like Array.findLast().
Name | Type | Optional | Description |
---|---|---|---|
fn | (value: V, key: K, collection: this) => value is V2 | No | The function to test with (should return a boolean) |
Searches for the key of a last item where the given function returns a truthy value. This behaves like Array.findLastIndex(), but returns the key rather than the positional index.
Name | Type | Optional | Description |
---|---|---|---|
fn | (value: V, key: K, collection: this) => key is K2 | No | The function to test with (should return a boolean) |
Obtains the first value(s) in this collection.
Returns
A single value if no amount is provided or an array of values, starting from the end if amount is negativeObtains the first key(s) in this collection.
Returns
A single key if no amount is provided or an array of keys, starting from the end if amount is negativeMaps each item into a Collection, then joins the results into a single Collection. Identical in behavior to Array.flatMap().
Example
Name | Type | Optional | Description |
---|---|---|---|
fn | (value: V, key: K, collection: this) => Collection<K, T> | No | Function that produces a new Collection |
Checks if all of the elements exist in the collection.
Returns
true
if all of the elements exist, false
if at least one does not exist.Name | Type | Optional | Description |
---|---|---|---|
...keys | K[] | No | The keys of the elements to check for |
Checks if any of the elements exist in the collection.
Returns
true
if any of the elements exist, false
if none exist.Name | Type | Optional | Description |
---|---|---|---|
...keys | K[] | No | The keys of the elements to check for |
The intersection method returns a new collection containing the items where the key is present in both collections.
Example
Name | Type | Optional | Description |
---|---|---|---|
other | ReadonlyCollection<K, T> | No | The other Collection to filter against |
Identical to Array.at(). Returns the key at a given index, allowing for positive and negative integers. Negative integers count back from the last item in the collection.
Name | Type | Optional | Description |
---|---|---|---|
index | number | No | The index of the key to obtain |
Obtains the last value(s) in this collection.
Returns
A single value if no amount is provided or an array of values, starting from the start if amount is negativeObtains the last key(s) in this collection.
Returns
A single key if no amount is provided or an array of keys, starting from the start if amount is negativeMaps each item to another value into an array. Identical in behavior to Array.map().
Example
Name | Type | Optional | Description |
---|---|---|---|
fn | (value: V, key: K, collection: this) => T | No | Function that produces an element of the new array, taking three arguments |
Maps each item to another value into a collection. Identical in behavior to Array.map().
Example
Name | Type | Optional | Description |
---|---|---|---|
fn | (value: V, key: K, collection: this) => T | No | Function that produces an element of the new collection, taking three arguments |
Merges two Collections together into a new Collection.
Example
Example
Name | Type | Optional | Description |
---|---|---|---|
other | ReadonlyCollection<K, T> | No | The other Collection to merge with |
whenInSelf | (value: V, key: K) => Keep<R> | No | Function getting the result if the entry only exists in this Collection |
whenInOther | (valueOther: T, key: K) => Keep<R> | No | Function getting the result if the entry only exists in the other Collection |
whenInBoth | (value: V, valueOther: T, key: K) => Keep<R> | No | Function getting the result if the entry exists in both Collections |
Partitions the collection into two collections where the first collection contains the items that passed and the second contains the items that failed.
Example
Name | Type | Optional | Description |
---|---|---|---|
fn | (value: V, key: K, collection: this) => key is K2 | No | Function used to test (should return a boolean) |
Obtains unique random value(s) from this collection.
Returns
A single value if no amount is provided or an array of valuesObtains unique random key(s) from this collection.
Returns
A single key if no amount is provided or an arrayApplies a function to produce a single value. Identical in behavior to Array.reduce().
Example
Name | Type | Optional | Description |
---|---|---|---|
fn | (accumulator: T, value: V, key: K, collection: this) => T | No | Function used to reduce, taking four arguments; accumulator , currentValue , currentKey , and collection |
initialValue | T | Yes | Starting value for the accumulator |
Applies a function to produce a single value. Identical in behavior to Array.reduceRight().
Name | Type | Optional | Description |
---|---|---|---|
fn | (accumulator: T, value: V, key: K, collection: this) => T | No | Function used to reduce, taking four arguments; accumulator , value , key , and collection |
initialValue | T | Yes | Starting value for the accumulator |
Identical to Array.reverse() but returns a Collection instead of an Array.
Inherited from CollectionChecks if there exists an item that passes a test. Identical in behavior to Array.some().
Example
Name | Type | Optional | Description |
---|---|---|---|
fn | (value: V, key: K, collection: this) => unknown | No | Function used to test (should return a boolean) |
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
Name | Type | Optional | Description |
---|---|---|---|
compareFunction | Comparator<K, V> | Yes | 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. |
Removes items that satisfy the provided filter function.
Returns
The number of removed entriesName | Type | Optional | Description |
---|---|---|---|
fn | (value: V, key: K, collection: this) => unknown | No | Function used to test (should return a boolean) |
Returns a new collection containing only the items where the keys are present in either collection, but not both.
Example
Name | Type | Optional | Description |
---|---|---|---|
other | ReadonlyCollection<K, T> | No | The other Collection to filter against |
Runs a function on the collection and returns the collection.
Example
Name | Type | Optional | Description |
---|---|---|---|
fn | (collection: this) => void | No | Function to execute |
Identical to Array.toReversed() but returns a Collection instead of an Array.
Inherited from CollectionThe 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
Name | Type | Optional | Description |
---|---|---|---|
compareFunction | Comparator<K, V> | Yes | 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 a new collection containing the items where the key is present in either of the collections.
Remarks
If the collections have any items with the same key, the value from the first collection will be used.Example
Name | Type | Optional | Description |
---|---|---|---|
other | ReadonlyCollection<K, T> | No | The other Collection to filter against |