Utility data structure used in discord.js that extends JavaScript's native Map class with additional methods for enhanced functionality
npx @tessl/cli install tessl/npm-discordjs--collection@2.1.0Discord.js Collection is a powerful utility data structure that extends JavaScript's native Map class with additional methods for enhanced functionality. It serves as a core component of the discord.js ecosystem, offering optimized performance for handling collections of items with unique identifiers.
npm install @discordjs/collectionimport { Collection } from "@discordjs/collection";For CommonJS:
const { Collection } = require("@discordjs/collection");Import with types:
import { Collection, ReadonlyCollection, CollectionConstructor } from "@discordjs/collection";import { Collection } from "@discordjs/collection";
// Create a new collection
const users = new Collection<string, { name: string; age: number }>();
// Add items
users.set("123", { name: "Alice", age: 25 });
users.set("456", { name: "Bob", age: 30 });
// Use enhanced methods
const adults = users.filter(user => user.age >= 18);
const names = users.map(user => user.name);
const firstUser = users.first();
// Chain operations
const result = users
.filter(user => user.age > 20)
.map(user => user.name.toUpperCase())
.join(", ");Collection is built around several key patterns:
Collection<Key, Value> for type safetyrandom() and at() work efficiently with large datasetsEssential methods that extend Map functionality for enhanced data access and manipulation.
class Collection<Key, Value> extends Map<Key, Value> {
ensure(key: Key, defaultValueGenerator: (key: Key, collection: this) => Value): Value;
hasAll(...keys: Key[]): boolean;
hasAny(...keys: Key[]): boolean;
first(): Value | undefined;
first(amount: number): Value[];
last(): Value | undefined;
last(amount: number): Value[];
at(index: number): Value | undefined;
keyAt(index: number): Key | undefined;
random(): Value | undefined;
random(amount: number): Value[];
static combineEntries<Key, Value>(
entries: Iterable<[Key, Value]>,
combine: (firstValue: Value, secondValue: Value, key: Key) => Value
): Collection<Key, Value>;
}Functional programming methods that work similarly to Array methods but return Collections or maintain Collection context.
interface Collection<Key, Value> {
map<NewValue>(fn: (value: Value, key: Key, collection: this) => NewValue): NewValue[];
mapValues<NewValue>(fn: (value: Value, key: Key, collection: this) => NewValue): Collection<Key, NewValue>;
filter(fn: (value: Value, key: Key, collection: this) => unknown): Collection<Key, Value>;
find(fn: (value: Value, key: Key, collection: this) => unknown): Value | undefined;
reduce<InitialValue>(fn: (accumulator: InitialValue, value: Value, key: Key, collection: this) => InitialValue, initialValue: InitialValue): InitialValue;
some(fn: (value: Value, key: Key, collection: this) => unknown): boolean;
every(fn: (value: Value, key: Key, collection: this) => unknown): boolean;
}Set-like operations for combining, comparing, and manipulating multiple collections.
interface Collection<Key, Value> {
concat(...collections: ReadonlyCollection<Key, Value>[]): Collection<Key, Value>;
clone(): Collection<Key, Value>;
equals(collection: ReadonlyCollection<Key, Value>): boolean;
intersection(other: ReadonlyCollection<Key, any>): Collection<Key, Value>;
union<OtherValue>(other: ReadonlyCollection<Key, OtherValue>): Collection<Key, OtherValue | Value>;
difference(other: ReadonlyCollection<Key, any>): Collection<Key, Value>;
symmetricDifference<OtherValue>(other: ReadonlyCollection<Key, OtherValue>): Collection<Key, OtherValue | Value>;
}interface CollectionConstructor {
new (): Collection<unknown, unknown>;
new <Key, Value>(entries?: readonly (readonly [Key, Value])[] | null): Collection<Key, Value>;
new <Key, Value>(iterable: Iterable<readonly [Key, Value]>): Collection<Key, Value>;
readonly prototype: Collection<unknown, unknown>;
readonly [Symbol.species]: CollectionConstructor;
}
type ReadonlyCollection<Key, Value> = Omit<
Collection<Key, Value>,
'clear' | 'delete' | 'ensure' | 'forEach' | 'get' | 'reverse' | 'set' | 'sort' | 'sweep'
> & ReadonlyMap<Key, Value>;