type Entry = [K, V]; interface IGroupedValues { /** * The key associated with the grouped values. */ key: K; /** * An array of values grouped under the key. */ values: V[]; } /** * A Map with additional utility methods. * This is used throughout telegram.ts rather than Arrays for anything that has * an ID, for significantly improved performance and ease-of-use. * * @template K - The type of keys in the collection * @template V - The type of values in the collection */ declare class Collection extends Map { /** * Cached array of entries for performance optimization */ private _cache; /** * Creates a new Collection instance. * * @param entries - An optional iterable of [key, value] pairs to initialize the collection * @example * ```ts * const collection = new Collection([['key1', 'value1'], ['key2', 'value2']]); * ``` */ constructor(entries?: readonly Entry[] | Iterable>); /** * Gets the Collection constructor */ get [Symbol.species](): typeof Collection; /** * Identical to Map.get(). * Gets an element from the collection by key. */ get(key: K): V | undefined; /** * Identical to Map.set(). * Sets a new element in the collection with the specified key and value. * * @param key - The key of the element to add * @param value - The value of the element to add * @returns The collection instance (for chaining) */ set(key: K, value: V): this; /** * Identical to Map.has(). * Checks if an element exists in the collection. * * @param key - The key to check for * @returns Whether the key exists in the collection */ has(key: K): boolean; /** * Identical to Map.delete(). * Deletes an element from the collection by key. * * @param key - The key to delete * @returns Whether the element was deleted */ delete(key: K): boolean; /** * Identical to Map.clear(). * Removes all elements from the collection. */ clear(): void; /** * Checks if keys exist and their corresponding values satisfy a condition. * * @param keys - An array of keys to check * @param isEnabled - If true, returns true if any key doesn't exist; otherwise returns an object with existence status * @returns An object containing keys and their existence status, or a boolean if isEnabled is true */ hasKeys(keys: K[], isEnabled?: boolean): Record | boolean; /** * Checks if values exist and their corresponding keys satisfy a condition. * * @param values - An array of values to check * @param isEnabled - If true, returns true if any value doesn't exist; otherwise returns an object with existence status * @returns An object containing values and their existence status, or a boolean if isEnabled is true */ hasValues(values: V[], isEnabled?: boolean): Record | boolean; /** * Checks if all of the elements exist in the collection. * * @param keys - The keys to check for * @returns True if all keys exist, false otherwise * @example * ```ts * collection.set('a', 1); * collection.set('b', 2); * collection.hasAll('a', 'b'); // true * collection.hasAll('a', 'c'); // false * ``` */ hasAll(...keys: K[]): boolean; /** * Checks if any of the elements exist in the collection. * * @param keys - The keys to check for * @returns True if at least one key exists, false otherwise * @example * ```ts * collection.set('a', 1); * collection.hasAny('a', 'b'); // true * collection.hasAny('c', 'd'); // false * ``` */ hasAny(...keys: K[]): boolean; /** * Obtains the first value(s) in this collection. * * @param amount - Amount of values to obtain from the beginning * @returns A single value if no amount is provided or an array of values * @example * ```ts * collection.first(); // value * collection.first(2); // [value1, value2] * ``` */ first(): V | undefined; first(amount: number): V[]; /** * Obtains the first key(s) in this collection. * * @param amount - Amount of keys to obtain from the beginning * @returns A single key if no amount is provided or an array of keys */ firstKey(): K | undefined; firstKey(amount: number): K[]; /** * Obtains the last value(s) in this collection. * * @param amount - Amount of values to obtain from the end * @returns A single value if no amount is provided or an array of values */ last(): V | undefined; last(amount: number): V[]; /** * Obtains the last key(s) in this collection. * * @param amount - Amount of keys to obtain from the end * @returns A single key if no amount is provided or an array of keys */ lastKey(): K | undefined; lastKey(amount: number): K[]; /** * 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. * * @param index - The index of the element to obtain * @returns The element at the given index */ at(index: number): V | 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. * * @param index - The index of the key to obtain * @returns The key at the given index */ keyAt(index: number): K | undefined; /** * Obtains unique random value(s) from this collection. * * @param amount - Amount of values to obtain randomly * @returns A single value if no amount is provided or an array of values */ random(): V | undefined; random(amount: number): V[]; /** * Obtains unique random key(s) from this collection. * * @param amount - Amount of keys to obtain randomly * @returns A single key if no amount is provided or an array of keys */ randomKey(): K | undefined; randomKey(amount: number): K[]; /** * Identical to Array.reverse() but returns a Collection instead of an Array. * The collection is reversed in place. * * @returns The collection instance (for chaining) */ reverse(): this; /** * Returns a new Collection with the elements in reverse order. * * @returns A new reversed collection */ toReversed(): Collection; /** * Searches for a single item where the given function returns a truthy value. * Identical to Array.find(). * * @param fn - The function to test with (should return boolean) * @param thisArg - Value to use as `this` when executing function * @returns The first element in the collection that passes the test * @example * ```ts * collection.find(user => user.username === 'Bob'); * ``` */ find(fn: (value: V, key: K, collection: this) => value is V2, thisArg?: unknown): V2 | undefined; find(fn: (value: V, key: K, collection: this) => unknown, thisArg?: unknown): V | undefined; find(fn: (this: This, value: V, key: K, collection: this) => unknown, thisArg: This): V | undefined; /** * Returns the value of the last element in the collection that satisfies the provided testing function. * * @param fn - Function to test for each element * @param thisArg - Object to use as 'this' when executing the callback * @returns The value of the last element that passes the test, or undefined if no element passes */ findLast(fn: (value: V, key: K, collection: this) => value is V2, thisArg?: unknown): V2 | undefined; findLast(fn: (value: V, key: K, collection: this) => unknown, thisArg?: unknown): V | undefined; findLast(fn: (this: This, value: V, key: K, collection: this) => unknown, thisArg: This): V | 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. * * @param fn - The function to test with (should return boolean) * @param thisArg - Value to use as `this` when executing function * @returns The key of the first element that passes the test */ findKey(fn: (value: V, key: K, collection: this) => key is K2): K2 | undefined; findKey(fn: (value: V, key: K, collection: this) => unknown): K | undefined; findKey(fn: (this: This, value: V, key: K, collection: this) => unknown, thisArg: This): K | undefined; /** * Searches for the key of the last item where the given function returns a truthy value. * * @param fn - The function to test with (should return boolean) * @param thisArg - Value to use as `this` when executing function * @returns The key of the last element that passes the test */ findLastKey(fn: (value: V, key: K, collection: this) => key is K2): K2 | undefined; findLastKey(fn: (value: V, key: K, collection: this) => unknown): K | undefined; findLastKey(fn: (this: This, value: V, key: K, collection: this) => unknown, thisArg: This): K | undefined; /** * Removes items that satisfy the provided filter function. * Returns the number of removed entries. * * @param fn - Function used to test (should return a boolean) * @param thisArg - Value to use as `this` when executing function * @returns The number of removed entries */ sweep(fn: (value: V, key: K, collection: this) => unknown, thisArg?: unknown): number; /** * Identical to Array.filter(), but returns a Collection instead of an Array. * * @param fn - The function to test with (should return boolean) * @param thisArg - Value to use as `this` when executing function * @returns A new collection with the filtered elements * @example * ```ts * collection.filter(user => user.username === 'Bob'); * ``` */ filter(fn: (value: V, key: K, collection: this) => key is K2): Collection; filter(fn: (value: V, key: K, collection: this) => value is V2): Collection; filter(fn: (value: V, key: K, collection: this) => unknown): Collection; filter(fn: (this: This, value: V, key: K, collection: this) => key is K2, thisArg: This): Collection; filter(fn: (this: This, value: V, key: K, collection: this) => value is V2, thisArg: This): Collection; filter(fn: (this: This, value: V, key: K, collection: this) => unknown, thisArg: This): Collection; /** * Partitions the collection into two collections where the first collection * contains the items that passed and the second contains the items that failed. * * @param fn - Function used to test (should return a boolean) * @param thisArg - Value to use as `this` when executing function * @returns A tuple containing two collections * @example * ```ts * const [big, small] = collection.partition(user => user.size > 10); * ``` */ partition(fn: (value: V, key: K, collection: this) => key is K2): [Collection, Collection, V>]; partition(fn: (value: V, key: K, collection: this) => value is V2): [Collection, Collection>]; partition(fn: (value: V, key: K, collection: this) => unknown): [Collection, Collection]; partition(fn: (this: This, value: V, key: K, collection: this) => key is K2, thisArg: This): [Collection, Collection, V>]; partition(fn: (this: This, value: V, key: K, collection: this) => value is V2, thisArg: This): [Collection, Collection>]; partition(fn: (this: This, value: V, key: K, collection: this) => unknown, thisArg: This): [Collection, Collection]; /** * Maps each item to another value into an array. * Identical to Array.map(). * * @param fn - Function that produces an element of the new array * @param thisArg - Value to use as `this` when executing function * @returns An array of the mapped values * @example * ```ts * collection.map(user => user.tag); * ``` */ map(fn: (value: V, key: K, collection: this) => T, thisArg?: unknown): T[]; map(fn: (this: This, value: V, key: K, collection: this) => T, thisArg: This): T[]; /** * Maps each item to another value into a collection. * Identical in behavior to Array.map(), but returns a Collection instead of an Array. * * @param fn - Function that produces an element of the new collection * @param thisArg - Value to use as `this` when executing function * @returns A new collection with the mapped values */ mapValues(fn: (value: V, key: K, collection: this) => T, thisArg?: unknown): Collection; mapValues(fn: (this: This, value: V, key: K, collection: this) => T, thisArg: This): Collection; /** * Maps each item into a Collection, then joins the results into a single Collection. * Identical in behavior to Array.flatMap(). * * @param fn - Function that produces a new Collection * @param thisArg - Value to use as `this` when executing function * @returns A new flattened collection */ flatMap(fn: (value: V, key: K, collection: this) => Collection, thisArg?: unknown): Collection; flatMap(fn: (this: This, value: V, key: K, collection: this) => Collection, thisArg: This): Collection; /** * Checks if there exists an item that passes a test. * Identical to Array.some(). * * @param fn - Function used to test (should return a boolean) * @param thisArg - Value to use as `this` when executing function * @returns True if at least one element passes the test * @example * ```ts * collection.some(user => user.discriminator === '0000'); * ``` */ some(fn: (value: V, key: K, collection: this) => unknown, thisArg?: unknown): boolean; some(fn: (this: This, value: V, key: K, collection: this) => unknown, thisArg: This): boolean; /** * Checks if all items pass a test. * Identical to Array.every(). * * @param fn - Function used to test (should return a boolean) * @param thisArg - Value to use as `this` when executing function * @returns True if all elements pass the test * @example * ```ts * collection.every(user => !user.bot); * ``` */ every(fn: (value: V, key: K, collection: this) => key is K2): this is Collection; every(fn: (value: V, key: K, collection: this) => value is V2): this is Collection; every(fn: (value: V, key: K, collection: this) => unknown): boolean; every(fn: (this: This, value: V, key: K, collection: this) => key is K2, thisArg: This): this is Collection; every(fn: (this: This, value: V, key: K, collection: this) => value is V2, thisArg: This): this is Collection; every(fn: (this: This, value: V, key: K, collection: this) => unknown, thisArg: This): boolean; /** * Applies a function to produce a single value. * Identical in behavior to Array.reduce(). * * @param fn - Function used to reduce, taking four arguments; accumulator, currentValue, currentKey, and collection * @param initialValue - Starting value for the accumulator * @returns The accumulated value * @example * ```ts * collection.reduce((acc, user) => acc + user.age, 0); * ``` */ reduce(fn: (accumulator: T, value: V, key: K, collection: this) => T, initialValue?: T): T; /** * Applies a function against an accumulator and each element in the collection (from right to left) * to reduce it to a single value. * * @param fn - Function to execute on each element in the collection * @param initialValue - Value to use as the first argument to the first call of the callback * @returns The reduced value */ reduceRight(fn: (accumulator: T, value: V, key: K, collection: this) => T, initialValue?: T): T; /** * Identical to Map.forEach(), but returns the collection instead of undefined. * * @param fn - Function to execute for each element * @param thisArg - Value to use as `this` when executing function * @returns The collection instance (for chaining) * @example * ```ts * collection * .each(user => console.log(user.username)) * .filter(user => user.bot) * .each(user => console.log(user.username)); * ``` */ each(fn: (value: V, key: K, collection: this) => void, thisArg?: unknown): this; each(fn: (this: This, value: V, key: K, collection: this) => void, thisArg: This): this; /** * Runs a function on the collection and returns the collection. * * @param fn - Function to execute * @param thisArg - Value to use as `this` when executing function * @returns The collection instance (for chaining) * @example * ```ts * collection * .tap(coll => console.log(coll.size)) * .filter(user => user.bot) * .tap(coll => console.log(coll.size)) * ``` */ tap(fn: (collection: this) => void, thisArg?: unknown): this; tap(fn: (this: This, collection: this) => void, thisArg: This): this; /** * Creates an identical shallow copy of this collection. * * @returns A new collection with the same entries * @example * ```ts * const newColl = someColl.clone(); * ``` */ clone(): Collection; /** * Combines this collection with others into a new collection. * None of the source collections are modified. * * @param collections - Collections to merge * @returns A new collection containing all entries * @example * ```ts * const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl); * ``` */ concat(...collections: ReadonlyCollection[]): Collection; /** * 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. * * @param collection - Collection to compare with * @returns Whether the collections have identical contents */ equals(collection: ReadonlyCollection): boolean; /** * The sort method sorts the items of a collection in place and returns it. * The sort is not necessarily stable. The default sort order is according to string Unicode code points. * * @param compareFunction - Specifies a function that defines the sort order. * If omitted, the collection is sorted according to each character's Unicode code point value, * according to the string conversion of each element. * @returns The sorted collection * @example * ```ts * collection.sort((userA, userB) => userA.createdTimestamp - userB.createdTimestamp); * ``` */ sort(compareFunction?: (firstValue: V, secondValue: V, firstKey: K, secondKey: K) => number): this; /** * Returns a new sorted collection. * The sort is not necessarily stable. The default sort order is according to string Unicode code points. * * @param compareFunction - Specifies a function that defines the sort order * @returns A new sorted collection */ toSorted(compareFunction?: (firstValue: V, secondValue: V, firstKey: K, secondKey: K) => number): Collection; /** * The intersect method returns a new structure containing items where the keys are present in both original structures. * * @param other - The other Collection to filter against * @returns A new collection containing the intersection */ intersect(other: ReadonlyCollection): Collection; /** * Returns a new structure containing items where the key is present in one of the original structures but not the other. * * @param other - The other Collection to filter against * @returns A new collection containing the difference */ difference(other: ReadonlyCollection): Collection; /** * Returns a new Collection containing only the elements where the key is present in this collection but not in the other. * * @param other - The other collection to compare against * @returns A new collection with the subtracted elements */ subtract(other: ReadonlyCollection): Collection; /** * Merges two Collections together into a new Collection. * * @param other - The other Collection to merge with * @param whenInSelf - Function getting the result if the entry only exists in this Collection * @param whenInOther - Function getting the result if the entry only exists in the other Collection * @param whenInBoth - Function getting the result if the entry exists in both Collections * @returns A new merged collection * @example * ```ts * // 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 }), * ); * ``` */ merge(other: ReadonlyCollection, whenInSelf: (value: V, key: K) => Keep, whenInOther: (valueOther: T, key: K) => Keep, whenInBoth: (value: V, valueOther: T, key: K) => Keep): Collection; /** * Identical to Array.at(). * The with() method changes the value of a given index in the collection, returning a new collection with the element at the given index replaced with the given value. * * @param index - The index of the element to replace * @param value - The new value to assign * @returns A new collection with the updated value */ with(index: number, value: V): Collection | undefined; /** * Returns a new Collection containing a subset of items. * Identical to Array.toSpliced(). * * @param start - The start index * @param deleteCount - The number of elements to remove * @param items - Elements to insert * @returns A new collection with the specified modifications */ toSpliced(start: number, deleteCount?: number, ...items: Entry[]): Collection; /** * Obtains the value of the given key if it exists, otherwise sets and returns the value provided by the default value generator. * * @param key - The key to get if it exists, or set otherwise * @param defaultValueGenerator - A function that generates the default value * @returns The value at the key or the generated default value * @example * ```ts * collection.ensure(guildId, () => defaultGuildSettings); * ``` */ ensure(key: K, defaultValueGenerator: (key: K, collection: this) => V): 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. * * @returns An array of the values */ toArray(): V[]; /** * Creates an ordered array of the keys of this collection, and caches it internally. * * @returns An array of the keys */ keyArray(): K[]; /** * Returns the first key found to be associated with the given value. * * @param value - The value to search for * @returns The key associated with the value, or undefined if not found */ keyOf(value: V): K | undefined; /** * Groups the values of the collection by the result of a provided function. * * @param fn - Function to transform each value into a key for grouping * @returns An array of objects representing the grouped values */ groupBy(fn: (value: V, key: K, collection: this) => T): IGroupedValues[]; /** * Converts the collection to a plain object. * * @returns An object containing the elements of the collection */ toJSON(): Record; /** * Converts the collection to a Map. * * @returns A Map containing all elements from the collection */ toMap(): Map; /** * Checks if the collection is empty. * * @returns True if the collection has no elements */ get isEmpty(): boolean; /** * When concatenating the name of a collection, it concatenates the name of the constructor * instead of `Map`. * * @returns The string tag for the collection */ get [Symbol.toStringTag](): string; /** * Default sort comparison function. * * @param firstValue - First value to compare * @param secondValue - Second value to compare * @returns Comparison result */ private static defaultSort; } /** * A read-only view of a Collection. */ type ReadonlyCollection = ReadonlyMap & Omit, "clear" | "delete" | "ensure" | "reverse" | "set" | "sort" | "sweep">; /** * Result type for merge operations. */ interface Keep { keep: boolean; value: V; } export { Collection, type Entry, type Keep, type IGroupedValues, type ReadonlyCollection, };