UNPKG

21.2 kBTypeScriptView Raw
1/**
2 * @internal
3 */
4interface CollectionConstructor {
5 new (): Collection<unknown, unknown>;
6 new <K, V>(entries?: readonly (readonly [K, V])[] | null): Collection<K, V>;
7 new <K, V>(iterable: Iterable<readonly [K, V]>): Collection<K, V>;
8 readonly prototype: Collection<unknown, unknown>;
9 readonly [Symbol.species]: CollectionConstructor;
10}
11/**
12 * Represents an immutable version of a collection
13 */
14type ReadonlyCollection<K, V> = Omit<Collection<K, V>, 'delete' | 'ensure' | 'forEach' | 'get' | 'reverse' | 'set' | 'sort' | 'sweep'> & ReadonlyMap<K, V>;
15/**
16 * Separate interface for the constructor so that emitted js does not have a constructor that overwrites itself
17 *
18 * @internal
19 */
20interface Collection<K, V> extends Map<K, V> {
21 constructor: CollectionConstructor;
22}
23/**
24 * A Map with additional utility methods. This is used throughout discord.js rather than Arrays for anything that has
25 * an ID, for significantly improved performance and ease-of-use.
26 *
27 * @typeParam K - The key type this collection holds
28 * @typeParam V - The value type this collection holds
29 */
30declare class Collection<K, V> extends Map<K, V> {
31 /**
32 * Obtains the value of the given key if it exists, otherwise sets and returns the value provided by the default value generator.
33 *
34 * @param key - The key to get if it exists, or set otherwise
35 * @param defaultValueGenerator - A function that generates the default value
36 * @example
37 * ```ts
38 * collection.ensure(guildId, () => defaultGuildConfig);
39 * ```
40 */
41 ensure(key: K, defaultValueGenerator: (key: K, collection: this) => V): V;
42 /**
43 * Checks if all of the elements exist in the collection.
44 *
45 * @param keys - The keys of the elements to check for
46 * @returns `true` if all of the elements exist, `false` if at least one does not exist.
47 */
48 hasAll(...keys: K[]): boolean;
49 /**
50 * Checks if any of the elements exist in the collection.
51 *
52 * @param keys - The keys of the elements to check for
53 * @returns `true` if any of the elements exist, `false` if none exist.
54 */
55 hasAny(...keys: K[]): boolean;
56 /**
57 * Obtains the first value(s) in this collection.
58 *
59 * @param amount - Amount of values to obtain from the beginning
60 * @returns A single value if no amount is provided or an array of values, starting from the end if amount is negative
61 */
62 first(): V | undefined;
63 first(amount: number): V[];
64 /**
65 * Obtains the first key(s) in this collection.
66 *
67 * @param amount - Amount of keys to obtain from the beginning
68 * @returns A single key if no amount is provided or an array of keys, starting from the end if
69 * amount is negative
70 */
71 firstKey(): K | undefined;
72 firstKey(amount: number): K[];
73 /**
74 * Obtains the last value(s) in this collection.
75 *
76 * @param amount - Amount of values to obtain from the end
77 * @returns A single value if no amount is provided or an array of values, starting from the start if
78 * amount is negative
79 */
80 last(): V | undefined;
81 last(amount: number): V[];
82 /**
83 * Obtains the last key(s) in this collection.
84 *
85 * @param amount - Amount of keys to obtain from the end
86 * @returns A single key if no amount is provided or an array of keys, starting from the start if
87 * amount is negative
88 */
89 lastKey(): K | undefined;
90 lastKey(amount: number): K[];
91 /**
92 * Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
93 * Returns the item at a given index, allowing for positive and negative integers.
94 * Negative integers count back from the last item in the collection.
95 *
96 * @param index - The index of the element to obtain
97 */
98 at(index: number): V | undefined;
99 /**
100 * Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
101 * Returns the key at a given index, allowing for positive and negative integers.
102 * Negative integers count back from the last item in the collection.
103 *
104 * @param index - The index of the key to obtain
105 */
106 keyAt(index: number): K | undefined;
107 /**
108 * Obtains unique random value(s) from this collection.
109 *
110 * @param amount - Amount of values to obtain randomly
111 * @returns A single value if no amount is provided or an array of values
112 */
113 random(): V | undefined;
114 random(amount: number): V[];
115 /**
116 * Obtains unique random key(s) from this collection.
117 *
118 * @param amount - Amount of keys to obtain randomly
119 * @returns A single key if no amount is provided or an array
120 */
121 randomKey(): K | undefined;
122 randomKey(amount: number): K[];
123 /**
124 * Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse | Array.reverse()}
125 * but returns a Collection instead of an Array.
126 */
127 reverse(): this;
128 /**
129 * Searches for a single item where the given function returns a truthy value. This behaves like
130 * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find | Array.find()}.
131 * All collections used in Discord.js are mapped using their `id` property, and if you want to find by id you
132 * should use the `get` method. See
133 * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get | MDN} for details.
134 *
135 * @param fn - The function to test with (should return boolean)
136 * @param thisArg - Value to use as `this` when executing function
137 * @example
138 * ```ts
139 * collection.find(user => user.username === 'Bob');
140 * ```
141 */
142 find<V2 extends V>(fn: (value: V, key: K, collection: this) => value is V2): V2 | undefined;
143 find(fn: (value: V, key: K, collection: this) => unknown): V | undefined;
144 find<This, V2 extends V>(fn: (this: This, value: V, key: K, collection: this) => value is V2, thisArg: This): V2 | undefined;
145 find<This>(fn: (this: This, value: V, key: K, collection: this) => unknown, thisArg: This): V | undefined;
146 /**
147 * Searches for the key of a single item where the given function returns a truthy value. This behaves like
148 * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex | Array.findIndex()},
149 * but returns the key rather than the positional index.
150 *
151 * @param fn - The function to test with (should return boolean)
152 * @param thisArg - Value to use as `this` when executing function
153 * @example
154 * ```ts
155 * collection.findKey(user => user.username === 'Bob');
156 * ```
157 */
158 findKey<K2 extends K>(fn: (value: V, key: K, collection: this) => key is K2): K2 | undefined;
159 findKey(fn: (value: V, key: K, collection: this) => unknown): K | undefined;
160 findKey<This, K2 extends K>(fn: (this: This, value: V, key: K, collection: this) => key is K2, thisArg: This): K2 | undefined;
161 findKey<This>(fn: (this: This, value: V, key: K, collection: this) => unknown, thisArg: This): K | undefined;
162 /**
163 * Removes items that satisfy the provided filter function.
164 *
165 * @param fn - Function used to test (should return a boolean)
166 * @param thisArg - Value to use as `this` when executing function
167 * @returns The number of removed entries
168 */
169 sweep(fn: (value: V, key: K, collection: this) => unknown): number;
170 sweep<T>(fn: (this: T, value: V, key: K, collection: this) => unknown, thisArg: T): number;
171 /**
172 * Identical to
173 * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter | Array.filter()},
174 * but returns a Collection instead of an Array.
175 *
176 * @param fn - The function to test with (should return boolean)
177 * @param thisArg - Value to use as `this` when executing function
178 * @example
179 * ```ts
180 * collection.filter(user => user.username === 'Bob');
181 * ```
182 */
183 filter<K2 extends K>(fn: (value: V, key: K, collection: this) => key is K2): Collection<K2, V>;
184 filter<V2 extends V>(fn: (value: V, key: K, collection: this) => value is V2): Collection<K, V2>;
185 filter(fn: (value: V, key: K, collection: this) => unknown): Collection<K, V>;
186 filter<This, K2 extends K>(fn: (this: This, value: V, key: K, collection: this) => key is K2, thisArg: This): Collection<K2, V>;
187 filter<This, V2 extends V>(fn: (this: This, value: V, key: K, collection: this) => value is V2, thisArg: This): Collection<K, V2>;
188 filter<This>(fn: (this: This, value: V, key: K, collection: this) => unknown, thisArg: This): Collection<K, V>;
189 /**
190 * Partitions the collection into two collections where the first collection
191 * contains the items that passed and the second contains the items that failed.
192 *
193 * @param fn - Function used to test (should return a boolean)
194 * @param thisArg - Value to use as `this` when executing function
195 * @example
196 * ```ts
197 * const [big, small] = collection.partition(guild => guild.memberCount > 250);
198 * ```
199 */
200 partition<K2 extends K>(fn: (value: V, key: K, collection: this) => key is K2): [Collection<K2, V>, Collection<Exclude<K, K2>, V>];
201 partition<V2 extends V>(fn: (value: V, key: K, collection: this) => value is V2): [Collection<K, V2>, Collection<K, Exclude<V, V2>>];
202 partition(fn: (value: V, key: K, collection: this) => unknown): [Collection<K, V>, Collection<K, V>];
203 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>];
204 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>>];
205 partition<This>(fn: (this: This, value: V, key: K, collection: this) => unknown, thisArg: This): [Collection<K, V>, Collection<K, V>];
206 /**
207 * Maps each item into a Collection, then joins the results into a single Collection. Identical in behavior to
208 * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap | Array.flatMap()}.
209 *
210 * @param fn - Function that produces a new Collection
211 * @param thisArg - Value to use as `this` when executing function
212 * @example
213 * ```ts
214 * collection.flatMap(guild => guild.members.cache);
215 * ```
216 */
217 flatMap<T>(fn: (value: V, key: K, collection: this) => Collection<K, T>): Collection<K, T>;
218 flatMap<T, This>(fn: (this: This, value: V, key: K, collection: this) => Collection<K, T>, thisArg: This): Collection<K, T>;
219 /**
220 * Maps each item to another value into an array. Identical in behavior to
221 * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map | Array.map()}.
222 *
223 * @param fn - Function that produces an element of the new array, taking three arguments
224 * @param thisArg - Value to use as `this` when executing function
225 * @example
226 * ```ts
227 * collection.map(user => user.tag);
228 * ```
229 */
230 map<T>(fn: (value: V, key: K, collection: this) => T): T[];
231 map<This, T>(fn: (this: This, value: V, key: K, collection: this) => T, thisArg: This): T[];
232 /**
233 * Maps each item to another value into a collection. Identical in behavior to
234 * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map | Array.map()}.
235 *
236 * @param fn - Function that produces an element of the new collection, taking three arguments
237 * @param thisArg - Value to use as `this` when executing function
238 * @example
239 * ```ts
240 * collection.mapValues(user => user.tag);
241 * ```
242 */
243 mapValues<T>(fn: (value: V, key: K, collection: this) => T): Collection<K, T>;
244 mapValues<This, T>(fn: (this: This, value: V, key: K, collection: this) => T, thisArg: This): Collection<K, T>;
245 /**
246 * Checks if there exists an item that passes a test. Identical in behavior to
247 * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some | Array.some()}.
248 *
249 * @param fn - Function used to test (should return a boolean)
250 * @param thisArg - Value to use as `this` when executing function
251 * @example
252 * ```ts
253 * collection.some(user => user.discriminator === '0000');
254 * ```
255 */
256 some(fn: (value: V, key: K, collection: this) => unknown): boolean;
257 some<T>(fn: (this: T, value: V, key: K, collection: this) => unknown, thisArg: T): boolean;
258 /**
259 * Checks if all items passes a test. Identical in behavior to
260 * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every | Array.every()}.
261 *
262 * @param fn - Function used to test (should return a boolean)
263 * @param thisArg - Value to use as `this` when executing function
264 * @example
265 * ```ts
266 * collection.every(user => !user.bot);
267 * ```
268 */
269 every<K2 extends K>(fn: (value: V, key: K, collection: this) => key is K2): this is Collection<K2, V>;
270 every<V2 extends V>(fn: (value: V, key: K, collection: this) => value is V2): this is Collection<K, V2>;
271 every(fn: (value: V, key: K, collection: this) => unknown): boolean;
272 every<This, K2 extends K>(fn: (this: This, value: V, key: K, collection: this) => key is K2, thisArg: This): this is Collection<K2, V>;
273 every<This, V2 extends V>(fn: (this: This, value: V, key: K, collection: this) => value is V2, thisArg: This): this is Collection<K, V2>;
274 every<This>(fn: (this: This, value: V, key: K, collection: this) => unknown, thisArg: This): boolean;
275 /**
276 * Applies a function to produce a single value. Identical in behavior to
277 * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce | Array.reduce()}.
278 *
279 * @param fn - Function used to reduce, taking four arguments; `accumulator`, `currentValue`, `currentKey`,
280 * and `collection`
281 * @param initialValue - Starting value for the accumulator
282 * @example
283 * ```ts
284 * collection.reduce((acc, guild) => acc + guild.memberCount, 0);
285 * ```
286 */
287 reduce<T>(fn: (accumulator: T, value: V, key: K, collection: this) => T, initialValue?: T): T;
288 /**
289 * Identical to
290 * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/forEach | Map.forEach()},
291 * but returns the collection instead of undefined.
292 *
293 * @param fn - Function to execute for each element
294 * @param thisArg - Value to use as `this` when executing function
295 * @example
296 * ```ts
297 * collection
298 * .each(user => console.log(user.username))
299 * .filter(user => user.bot)
300 * .each(user => console.log(user.username));
301 * ```
302 */
303 each(fn: (value: V, key: K, collection: this) => void): this;
304 each<T>(fn: (this: T, value: V, key: K, collection: this) => void, thisArg: T): this;
305 /**
306 * Runs a function on the collection and returns the collection.
307 *
308 * @param fn - Function to execute
309 * @param thisArg - Value to use as `this` when executing function
310 * @example
311 * ```ts
312 * collection
313 * .tap(coll => console.log(coll.size))
314 * .filter(user => user.bot)
315 * .tap(coll => console.log(coll.size))
316 * ```
317 */
318 tap(fn: (collection: this) => void): this;
319 tap<T>(fn: (this: T, collection: this) => void, thisArg: T): this;
320 /**
321 * Creates an identical shallow copy of this collection.
322 *
323 * @example
324 * ```ts
325 * const newColl = someColl.clone();
326 * ```
327 */
328 clone(): Collection<K, V>;
329 /**
330 * Combines this collection with others into a new collection. None of the source collections are modified.
331 *
332 * @param collections - Collections to merge
333 * @example
334 * ```ts
335 * const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl);
336 * ```
337 */
338 concat(...collections: ReadonlyCollection<K, V>[]): Collection<K, V>;
339 /**
340 * Checks if this collection shares identical items with another.
341 * This is different to checking for equality using equal-signs, because
342 * the collections may be different objects, but contain the same data.
343 *
344 * @param collection - Collection to compare with
345 * @returns Whether the collections have identical contents
346 */
347 equals(collection: ReadonlyCollection<K, V>): boolean;
348 /**
349 * The sort method sorts the items of a collection in place and returns it.
350 * The sort is not necessarily stable in Node 10 or older.
351 * The default sort order is according to string Unicode code points.
352 *
353 * @param compareFunction - Specifies a function that defines the sort order.
354 * If omitted, the collection is sorted according to each character's Unicode code point value, according to the string conversion of each element.
355 * @example
356 * ```ts
357 * collection.sort((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
358 * ```
359 */
360 sort(compareFunction?: Comparator<K, V>): this;
361 /**
362 * The intersect method returns a new structure containing items where the keys and values are present in both original structures.
363 *
364 * @param other - The other Collection to filter against
365 */
366 intersect<T>(other: ReadonlyCollection<K, T>): Collection<K, T>;
367 /**
368 * The subtract method returns a new structure containing items where the keys and values of the original structure are not present in the other.
369 *
370 * @param other - The other Collection to filter against
371 */
372 subtract<T>(other: ReadonlyCollection<K, T>): Collection<K, V>;
373 /**
374 * The difference method returns a new structure containing items where the key is present in one of the original structures but not the other.
375 *
376 * @param other - The other Collection to filter against
377 */
378 difference<T>(other: ReadonlyCollection<K, T>): Collection<K, T | V>;
379 /**
380 * Merges two Collections together into a new Collection.
381 *
382 * @param other - The other Collection to merge with
383 * @param whenInSelf - Function getting the result if the entry only exists in this Collection
384 * @param whenInOther - Function getting the result if the entry only exists in the other Collection
385 * @param whenInBoth - Function getting the result if the entry exists in both Collections
386 * @example
387 * ```ts
388 * // Sums up the entries in two collections.
389 * coll.merge(
390 * other,
391 * x => ({ keep: true, value: x }),
392 * y => ({ keep: true, value: y }),
393 * (x, y) => ({ keep: true, value: x + y }),
394 * );
395 * ```
396 * @example
397 * ```ts
398 * // Intersects two collections in a left-biased manner.
399 * coll.merge(
400 * other,
401 * x => ({ keep: false }),
402 * y => ({ keep: false }),
403 * (x, _) => ({ keep: true, value: x }),
404 * );
405 * ```
406 */
407 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>;
408 /**
409 * The sorted method sorts the items of a collection and returns it.
410 * The sort is not necessarily stable in Node 10 or older.
411 * The default sort order is according to string Unicode code points.
412 *
413 * @param compareFunction - Specifies a function that defines the sort order.
414 * If omitted, the collection is sorted according to each character's Unicode code point value,
415 * according to the string conversion of each element.
416 * @example
417 * ```ts
418 * collection.sorted((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
419 * ```
420 */
421 sorted(compareFunction?: Comparator<K, V>): Collection<K, V>;
422 toJSON(): V[];
423 private static defaultSort;
424 /**
425 * Creates a Collection from a list of entries.
426 *
427 * @param entries - The list of entries
428 * @param combine - Function to combine an existing entry with a new one
429 * @example
430 * ```ts
431 * Collection.combineEntries([["a", 1], ["b", 2], ["a", 2]], (x, y) => x + y);
432 * // returns Collection { "a" => 3, "b" => 2 }
433 * ```
434 */
435 static combineEntries<K, V>(entries: Iterable<[K, V]>, combine: (firstValue: V, secondValue: V, key: K) => V): Collection<K, V>;
436}
437/**
438 * @internal
439 */
440type Keep<V> = {
441 keep: false;
442} | {
443 keep: true;
444 value: V;
445};
446/**
447 * @internal
448 */
449type Comparator<K, V> = (firstValue: V, secondValue: V, firstKey: K, secondKey: K) => number;
450
451/**
452 * The {@link https://github.com/discordjs/discord.js/blob/main/packages/collection/#readme | @discordjs/collection} version
453 * that you are currently using.
454 */
455declare const version: string;
456
457export { Collection, CollectionConstructor, Comparator, Keep, ReadonlyCollection, version };
458
\No newline at end of file