or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

array-like-methods.mdcollection-operations.mdcore-methods.mdindex.md
tile.json

tessl/npm-discordjs--collection

Utility data structure used in discord.js that extends JavaScript's native Map class with additional methods for enhanced functionality

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@discordjs/collection@2.1.x

To install, run

npx @tessl/cli install tessl/npm-discordjs--collection@2.1.0

index.mddocs/

Discord.js Collection

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

Package Information

  • Package Name: @discordjs/collection
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @discordjs/collection

Core Imports

import { Collection } from "@discordjs/collection";

For CommonJS:

const { Collection } = require("@discordjs/collection");

Import with types:

import { Collection, ReadonlyCollection, CollectionConstructor } from "@discordjs/collection";

Basic Usage

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(", ");

Architecture

Collection is built around several key patterns:

  • Map Extension: Inherits all native Map functionality while adding 40+ utility methods
  • Generic Types: Full TypeScript support with Collection<Key, Value> for type safety
  • Functional Programming: Chainable methods supporting map, filter, reduce operations
  • Lazy Evaluation: Methods like random() and at() work efficiently with large datasets
  • Immutable Operations: Many methods return new collections without modifying the original

Capabilities

Core Collection Methods

Essential 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>;
}

Core Methods

Array-like Operations

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;
}

Array-like Operations

Collection Operations

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>;
}

Collection Operations

Types

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