Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/collection/src/Collection.ts>default

Utility data structure for Deno (Discord.js Collection).
Latest
class default
extends Map<K, V>
import { default } from "https://deno.land/x/collection@v1.0.1/src/Collection.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.

Constructors

new
default(entries?: ReadonlyArray<readonly [K, V]> | null)

Properties

private
_array: V[] | null
private
_keyArray: K[] | null
['constructor']: Collection

Methods

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.

clear(): void

Identical to Map.clear(). Removes all elements from the collection.

clone(): this

Creates an identical shallow copy of this collection.

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

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

delete(key: K): boolean

Identical to Map.delete(). Deletes an element from the collection.

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
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.

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

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

every<T>(fn: (
this: T,
value: V,
key: K,
collection: this,
) => boolean
, thisArg: T
): boolean
filter(fn: (
value: V,
key: K,
collection: this,
) => boolean
): this

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

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

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

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

has(key: K): boolean

Identical to Map.has(). Checks if an element exists in the collection.

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

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.

last(): V | undefined

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

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

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

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>
partition(fn: (
value: V,
key: K,
collection: this,
) => boolean
): [this, this]

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

partition<T>(fn: (
this: T,
value: V,
key: K,
collection: this,
) => boolean
, thisArg: T
): [this, this]

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

random(amount: number): V[]

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

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().

set(key: K, value: V): this

Identical to Map.set(). Sets a new element in the collection with the specified key and value.

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?: (
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.

sorted(compareFunction?: (
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.

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

Static Properties

readonly
default: Collection