class LimitedCollection

extends

Collection<Key, Value>
export class LimitedCollection<Key, Value> extends Collection<Key, Value>

A Collection which holds a max amount of entries.

Constructors

constructor(
iterable?: Iterable<readonly [Key, Value]>
)

Type Parameters

Key

Value

keepOverLimit : ((value: Value, key: Key, collection: this) => boolean) | null

A function called to check if an entry should be kept when the Collection is at max size.

maxSize : number

The max size of the Collection.

external
at(
index: number
) : Value | undefined

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.

Inherited from: Collection

external
clone() : Collection<Key, Value>

Creates an identical shallow copy of this collection.

Examples:
const newColl = someColl.clone();

Inherited from: Collection

staticexternal
combineEntries<

Key

Value

>(
entries: Iterable<[Key, Value]>
combine: (firstValue: Value, secondValue: Value, key: Key) => Value
) : Collection<Key, Value>

Creates a Collection from a list of entries.

Examples:
Collection.combineEntries([["a", 1], ["b", 2], ["a", 2]], (x, y) => x + y);
// returns Collection { "a" => 3, "b" => 2 }

Inherited from: Collection

external
concat(
...collections: ReadonlyCollection<Key, Value>[]
) : Collection<Key, Value>

Combines this collection with others into a new collection. None of the source collections are modified.

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

Inherited from: Collection

external
difference() : Collection<Key, Value>

Returns a new collection containing the items where the key is present in this collection but not the other.

Examples:
const col1 = new Collection([['a', 1], ['b', 2]]);
const col2 = new Collection([['a', 1], ['c', 3]]);
console.log(col1.difference(col2));
// => Collection { 'b' => 2 }
console.log(col2.difference(col1));
// => Collection { 'c' => 3 }

Inherited from: Collection

external
each(
fn: (value: Value, key: Key, collection: this) => void
) : this

Identical to Map.forEach(), but returns the collection instead of undefined.

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

external
ensure(
key: Key
defaultValueGenerator: (key: Key, collection: this) => Value
) : Value

Obtains the value of the given key if it exists, otherwise sets and returns the value provided by the default value generator.

Examples:
collection.ensure(guildId, () => defaultGuildConfig);

Inherited from: Collection

external
equals() : 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.

Returns: Whether the collections have identical contents

Inherited from: Collection

external
every<

NewKey extends Key

>(
fn: (value: Value, key: Key, collection: this) => key is NewKey
) : this is Collection<NewKey, Value>

Checks if all items passes a test. Identical in behavior to Array.every().

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

external
filter<

NewKey extends Key

>(
fn: (value: Value, key: Key, collection: this) => key is NewKey
) : Collection<NewKey, Value>

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

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

external
find<

NewValue extends Value

>(
fn: (value: Value, key: Key, collection: this) => value is NewValue
) : NewValue | 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.

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

external
findKey<

NewKey extends Key

>(
fn: (value: Value, key: Key, collection: this) => key is NewKey
) : NewKey | 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.

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

external
findLast<

NewValue extends Value

>(
fn: (value: Value, key: Key, collection: this) => value is NewValue
) : NewValue | undefined

Searches for a last item where the given function returns a truthy value. This behaves like Array.findLast().

external
findLastKey<

NewKey extends Key

>(
fn: (value: Value, key: Key, collection: this) => key is NewKey
) : NewKey | undefined

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.

external
first() : Value | undefined

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 negative

external
firstKey() : Key | undefined

Obtains 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 negative

external
flatMap<

NewValue

>(
fn: (value: Value, key: Key, collection: this) => Collection<Key, NewValue>
) : Collection<Key, NewValue>

Maps each item into a Collection, then joins the results into a single Collection. Identical in behavior to Array.flatMap().

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

staticexternal
groupBy<

Key

Item

>(
items: Iterable<Item>
keySelector: (item: Item, index: number) => Key
) : Collection<Key, Item[]>

Identical to Map.groupBy() but returns a Collection instead of a Map.

Inherited from: Collection

external
hasAll(
...keys: Key[]
) : boolean

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.

Inherited from: Collection

external
hasAny(
...keys: Key[]
) : boolean

Checks if any of the elements exist in the collection.

Returns: true if any of the elements exist, false if none exist.

Inherited from: Collection

external
intersection() : Collection<Key, Value>

The intersection method returns a new collection containing the items where the key is present in both collections.

Examples:
const col1 = new Collection([['a', 1], ['b', 2]]);
const col2 = new Collection([['a', 1], ['c', 3]]);
const intersection = col1.intersection(col2);
console.log(col1.intersection(col2));
// => Collection { 'a' => 1 }

Inherited from: Collection

external
keyAt(
index: number
) : Key | undefined

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.

Inherited from: Collection

external
last() : Value | undefined

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 negative

external
lastKey() : Key | undefined

Obtains 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 negative

external
map<

NewValue

>(
fn: (value: Value, key: Key, collection: this) => NewValue
) : NewValue[]

Maps each item to another value into an array. Identical in behavior to Array.map().

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

external
mapValues<

NewValue

>(
fn: (value: Value, key: Key, collection: this) => NewValue
) : Collection<Key, NewValue>

Maps each item to another value into a collection. Identical in behavior to Array.map().

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

external
merge<

OtherValue

ResultValue

>(
other: ReadonlyCollection<Key, OtherValue>
whenInSelf: (value: Value, key: Key) => Keep<ResultValue>
whenInOther: (valueOther: OtherValue, key: Key) => Keep<ResultValue>
whenInBoth: (value: Value, valueOther: OtherValue, key: Key) => Keep<ResultValue>
) : Collection<Key, ResultValue>

Merges two Collections together into a new Collection.

Examples:
// Sums up the entries in two collections.
coll.merge(
 other,
 x => ({ keep: true, value: x }),
 y => ({ keep: true, value: y }),
 (x, y) => ({ keep: true, value: x + y }),
);
// Intersects two collections in a left-biased manner.
coll.merge(
 other,
 x => ({ keep: false }),
 y => ({ keep: false }),
 (x, _) => ({ keep: true, value: x }),
);

Inherited from: Collection

external
partition<

NewKey extends Key

>(
fn: (value: Value, key: Key, collection: this) => key is NewKey
) : [Collection<NewKey, Value>, Collection<Exclude<Key, NewKey>, Value>]

Partitions the collection into two collections where the first collection contains the items that passed and the second contains the items that failed.

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

external
random() : Value | undefined

Obtains unique random value(s) from this collection.

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

external
randomKey() : Key | undefined

Obtains unique random key(s) from this collection.

Returns: A single key if no amount is provided or an array

external
reduce(
fn: (accumulator: Value, value: Value, key: Key, collection: this) => Value
initialValue?: Value
) : Value

Applies a function to produce a single value. Identical in behavior to Array.reduce().

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

external
reduceRight(
fn: (accumulator: Value, value: Value, key: Key, collection: this) => Value
initialValue?: Value
) : Value

Applies a function to produce a single value. Identical in behavior to Array.reduceRight().

external
reverse() : this

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

Inherited from: Collection

external
some(
fn: (value: Value, key: Key, collection: this) => unknown
) : boolean

Checks if there exists an item that passes a test. Identical in behavior to Array.some().

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

external
sort(
compareFunction?: Comparator<Key, Value>
) : this

The sort method sorts the items of a collection in place and returns it. If a comparison function is not provided, the function sorts by element values, using the same stringwise comparison algorithm as Array.sort().

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

Inherited from: Collection

external
sweep(
fn: (value: Value, key: Key, collection: this) => unknown
) : number

Removes items that satisfy the provided filter function.

Returns: The number of removed entries

external
symmetricDifference<

OtherValue

>(
other: ReadonlyCollection<Key, OtherValue>
) : Collection<Key, OtherValue | Value>

Returns a new collection containing only the items where the keys are present in either collection, but not both.

Examples:
const col1 = new Collection([['a', 1], ['b', 2]]);
const col2 = new Collection([['a', 1], ['c', 3]]);
const symmetricDifference = col1.symmetricDifference(col2);
console.log(col1.symmetricDifference(col2));
// => Collection { 'b' => 2, 'c' => 3 }

Inherited from: Collection

external
tap(
fn: (collection: this) => void
) : this

Runs a function on the collection and returns the collection.

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

external
toJSON() : [Key, Value][]

Inherited from: Collection

external
toReversed() : Collection<Key, Value>

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

Inherited from: Collection

external
toSorted(
compareFunction?: Comparator<Key, Value>
) : Collection<Key, Value>

The toSorted method returns a shallow copy of the collection with the items sorted. If a comparison function is not provided, the function sorts by element values, using the same stringwise comparison algorithm as Array.sort().

Examples:
const sortedCollection = collection.toSorted((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);

Inherited from: Collection

external
union<

OtherValue

>(
other: ReadonlyCollection<Key, OtherValue>
) : Collection<Key, OtherValue | Value>

Returns a new collection containing the items where the key is present in either of the collections.

Examples:
const col1 = new Collection([['a', 1], ['b', 2]]);
const col2 = new Collection([['a', 1], ['b', 3], ['c', 3]]);
const union = col1.union(col2);
console.log(union);
// => Collection { 'a' => 1, 'b' => 2, 'c' => 3 }

Inherited from: Collection