@discordjs/collection 0.8.0 → 1.0.1-dev.1658330950-a05386a.0

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