Skip to main content
Module

x/revoltio/mod.ts>Collection

No-nonsense Revolt library for nodejs and deno.
Latest
class Collection
extends Map<K, V>
Re-export
import { Collection } from "https://deno.land/x/revoltio@v1.0.0/mod.ts";

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

Methods

at(index: number): V | 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.

Creates an identical shallow copy of this collection.

concat(...collections: ReadonlyCollection<K, V>[]): Collection<K, V>

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

difference<T>(other: ReadonlyCollection<K, T>): Collection<K, V | T>

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

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

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

each<T>(fn: (
this: T,
value: V,
key: K,
collection: this,
) => void
, thisArg: T
): this
ensure(key: K, defaultValueGenerator: (key: K, collection: this) => V): V

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

equals(collection: ReadonlyCollection<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.

every<K2 extends K>(fn: (
value: V,
key: K,
collection: this,
) => key is K2
): this is Collection<K2, V>

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

every<V2 extends V>(fn: (
value: V,
key: K,
collection: this,
) => value is V2
): this is Collection<K, V2>
every(fn: (
value: V,
key: K,
collection: this,
) => boolean
): boolean
every<This, K2 extends K>(fn: (
this: This,
value: V,
key: K,
collection: this,
) => key is K2
, thisArg: This
): this is Collection<K2, V>
every<This, V2 extends V>(fn: (
this: This,
value: V,
key: K,
collection: this,
) => value is V2
, thisArg: This
): this is Collection<K, V2>
every<This>(fn: (
this: This,
value: V,
key: K,
collection: this,
) => boolean
, thisArg: This
): boolean
filter<K2 extends K>(fn: (
value: V,
key: K,
collection: this,
) => key is K2
): Collection<K2, V>

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

filter<V2 extends V>(fn: (
value: V,
key: K,
collection: this,
) => value is V2
): Collection<K, V2>
filter(fn: (
value: V,
key: K,
collection: this,
) => boolean
): Collection<K, V>
filter<This, K2 extends K>(fn: (
this: This,
value: V,
key: K,
collection: this,
) => key is K2
, thisArg: This
): Collection<K2, V>
filter<This, V2 extends V>(fn: (
this: This,
value: V,
key: K,
collection: this,
) => value is V2
, thisArg: This
): Collection<K, V2>
filter<This>(fn: (
this: This,
value: V,
key: K,
collection: this,
) => boolean
, thisArg: This
): Collection<K, V>
find<V2 extends V>(fn: (
value: V,
key: K,
collection: this,
) => value is V2
): V2 | 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.

find(fn: (
value: V,
key: K,
collection: this,
) => boolean
): V | undefined
find<This, V2 extends V>(fn: (
this: This,
value: V,
key: K,
collection: this,
) => value is V2
, thisArg: This
): V2 | undefined
find<This>(fn: (
this: This,
value: V,
key: K,
collection: this,
) => boolean
, thisArg: This
): V | undefined
findKey<K2 extends K>(fn: (
value: V,
key: K,
collection: this,
) => key is K2
): K2 | 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.

findKey(fn: (
value: V,
key: K,
collection: this,
) => boolean
): K | undefined
findKey<This, K2 extends K>(fn: (
this: This,
value: V,
key: K,
collection: this,
) => key is K2
, thisArg: This
): K2 | undefined
findKey<This>(fn: (
this: This,
value: V,
key: K,
collection: this,
) => boolean
, thisArg: This
): K | undefined
first(): V | undefined

Obtains the first value(s) in this collection.

first(amount: number): V[]
firstKey(): K | undefined

Obtains the first key(s) in this collection.

firstKey(amount: number): K[]
flatMap<T>(fn: (
value: V,
key: K,
collection: this,
) => Collection<K, T>
): Collection<K, T>

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

flatMap<T, This>(fn: (
this: This,
value: V,
key: K,
collection: this,
) => Collection<K, T>
, thisArg: This
): Collection<K, T>
hasAll(...keys: K[]): boolean

Checks if all of the elements exist in the collection.

hasAny(...keys: K[]): boolean

Checks if any of the elements exist in the collection.

intersect<T>(other: ReadonlyCollection<K, T>): Collection<K, T>

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

keyAt(index: number): K | 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.

last(): V | undefined

Obtains the last value(s) in this collection.

last(amount: number): V[]
lastKey(): K | undefined

Obtains the last key(s) in this collection.

lastKey(amount: number): K[]
map<T>(fn: (
value: V,
key: K,
collection: this,
) => T
): T[]

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

map<This, T>(fn: (
this: This,
value: V,
key: K,
collection: this,
) => T
, thisArg: This
): T[]
mapValues<T>(fn: (
value: V,
key: K,
collection: this,
) => T
): Collection<K, T>

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

mapValues<This, T>(fn: (
this: This,
value: V,
key: K,
collection: this,
) => T
, thisArg: This
): Collection<K, T>
merge<T, R>(
other: ReadonlyCollection<K, T>,
whenInSelf: (value: V, key: K) => Keep<R>,
whenInOther: (valueOther: T, key: K) => Keep<R>,
whenInBoth: (
value: V,
valueOther: T,
key: K,
) => Keep<R>
,
): Collection<K, R>

Merges two Collections together into a new Collection.

partition<K2 extends K>(fn: (
value: V,
key: K,
collection: this,
) => key is K2
): [Collection<K2, V>, Collection<Exclude<K, K2>, V>]

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

partition<V2 extends V>(fn: (
value: V,
key: K,
collection: this,
) => value is V2
): [Collection<K, V2>, Collection<K, Exclude<V, V2>>]
partition(fn: (
value: V,
key: K,
collection: this,
) => boolean
): [Collection<K, V>, Collection<K, V>]
partition<This, K2 extends K>(fn: (
this: This,
value: V,
key: K,
collection: this,
) => key is K2
, thisArg: This
): [Collection<K2, V>, Collection<Exclude<K, K2>, V>]
partition<This, V2 extends V>(fn: (
this: This,
value: V,
key: K,
collection: this,
) => value is V2
, thisArg: This
): [Collection<K, V2>, Collection<K, Exclude<V, V2>>]
partition<This>(fn: (
this: This,
value: V,
key: K,
collection: this,
) => boolean
, thisArg: This
): [Collection<K, V>, Collection<K, V>]
random(): V | undefined

Obtains unique random value(s) from this collection.

random(amount: number): V[]
randomKey(): K | undefined

Obtains unique random key(s) from this collection.

randomKey(amount: number): K[]
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().

reverse(): this

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

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

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

some<T>(fn: (
this: T,
value: V,
key: K,
collection: this,
) => boolean
, thisArg: T
): boolean
sort(compareFunction?: Comparator<K, V>): 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.

sorted(compareFunction?: Comparator<K, V>): 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.

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

Removes items that satisfy the provided filter function.

sweep<T>(fn: (
this: T,
value: V,
key: K,
collection: this,
) => boolean
, thisArg: T
): number
tap(fn: (collection: this) => void): this

Runs a function on the collection and returns the collection.

tap<T>(fn: (this: T, collection: this) => void, thisArg: T): this
toJSON(): V[]

Static Properties

private
defaultSort
readonly
default: Collection

Static Methods

combineEntries<K, V>(entries: Iterable<[K, V]>, combine: (
firstValue: V,
secondValue: V,
key: K,
) => V
): Collection<K, V>

Creates a Collection from a list of entries.