@discordjs/collection 1.4.0-dev.1677499493-ffdb197.0 → 1.4.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/CHANGELOG.md CHANGED
@@ -2,6 +2,20 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
4
 
5
+ # [@discordjs/collection@1.4.0](https://github.com/discordjs/discord.js/compare/@discordjs/collection@1.3.0...@discordjs/collection@1.4.0) - (2023-03-12)
6
+
7
+ ## Documentation
8
+
9
+ - Fix version export (#9049) ([8b70f49](https://github.com/discordjs/discord.js/commit/8b70f497a1207e30edebdecd12b926c981c13d28))
10
+
11
+ ## Features
12
+
13
+ - **website:** Add support for source file links (#9048) ([f6506e9](https://github.com/discordjs/discord.js/commit/f6506e99c496683ee0ab67db0726b105b929af38))
14
+
15
+ ## Refactor
16
+
17
+ - Compare with `undefined` directly (#9191) ([869153c](https://github.com/discordjs/discord.js/commit/869153c3fdf155783e7c0ecebd3627b087c3a026))
18
+
5
19
  # [@discordjs/collection@1.3.0](https://github.com/discordjs/discord.js/compare/@discordjs/collection@1.2.0...@discordjs/collection@1.3.0) - (2022-11-28)
6
20
 
7
21
  ## Bug Fixes
package/dist/index.js CHANGED
@@ -28,6 +28,16 @@ module.exports = __toCommonJS(src_exports);
28
28
 
29
29
  // src/collection.ts
30
30
  var Collection = class extends Map {
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
+ */
31
41
  ensure(key, defaultValueGenerator) {
32
42
  if (this.has(key))
33
43
  return this.get(key);
@@ -37,33 +47,51 @@ var Collection = class extends Map {
37
47
  this.set(key, defaultValue);
38
48
  return defaultValue;
39
49
  }
50
+ /**
51
+ * Checks if all of the elements exist in the collection.
52
+ *
53
+ * @param keys - The keys of the elements to check for
54
+ * @returns `true` if all of the elements exist, `false` if at least one does not exist.
55
+ */
40
56
  hasAll(...keys) {
41
57
  return keys.every((k) => super.has(k));
42
58
  }
59
+ /**
60
+ * Checks if any of the elements exist in the collection.
61
+ *
62
+ * @param keys - The keys of the elements to check for
63
+ * @returns `true` if any of the elements exist, `false` if none exist.
64
+ */
43
65
  hasAny(...keys) {
44
66
  return keys.some((k) => super.has(k));
45
67
  }
46
68
  first(amount) {
47
- if (typeof amount === "undefined")
69
+ if (amount === void 0)
48
70
  return this.values().next().value;
49
71
  if (amount < 0)
50
72
  return this.last(amount * -1);
51
73
  amount = Math.min(this.size, amount);
52
74
  const iter = this.values();
53
- return Array.from({ length: amount }, () => iter.next().value);
75
+ return Array.from({
76
+ length: amount
77
+ }, () => iter.next().value);
54
78
  }
55
79
  firstKey(amount) {
56
- if (typeof amount === "undefined")
80
+ if (amount === void 0)
57
81
  return this.keys().next().value;
58
82
  if (amount < 0)
59
83
  return this.lastKey(amount * -1);
60
84
  amount = Math.min(this.size, amount);
61
85
  const iter = this.keys();
62
- return Array.from({ length: amount }, () => iter.next().value);
86
+ return Array.from({
87
+ length: amount
88
+ }, () => iter.next().value);
63
89
  }
64
90
  last(amount) {
65
- const arr = [...this.values()];
66
- if (typeof amount === "undefined")
91
+ const arr = [
92
+ ...this.values()
93
+ ];
94
+ if (amount === void 0)
67
95
  return arr[arr.length - 1];
68
96
  if (amount < 0)
69
97
  return this.first(amount * -1);
@@ -72,8 +100,10 @@ var Collection = class extends Map {
72
100
  return arr.slice(-amount);
73
101
  }
74
102
  lastKey(amount) {
75
- const arr = [...this.keys()];
76
- if (typeof amount === "undefined")
103
+ const arr = [
104
+ ...this.keys()
105
+ ];
106
+ if (amount === void 0)
77
107
  return arr[arr.length - 1];
78
108
  if (amount < 0)
79
109
  return this.firstKey(amount * -1);
@@ -81,40 +111,66 @@ var Collection = class extends Map {
81
111
  return [];
82
112
  return arr.slice(-amount);
83
113
  }
114
+ /**
115
+ * Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
116
+ * Returns the item at a given index, allowing for positive and negative integers.
117
+ * Negative integers count back from the last item in the collection.
118
+ *
119
+ * @param index - The index of the element to obtain
120
+ */
84
121
  at(index) {
85
122
  index = Math.floor(index);
86
- const arr = [...this.values()];
123
+ const arr = [
124
+ ...this.values()
125
+ ];
87
126
  return arr.at(index);
88
127
  }
128
+ /**
129
+ * Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
130
+ * Returns the key at a given index, allowing for positive and negative integers.
131
+ * Negative integers count back from the last item in the collection.
132
+ *
133
+ * @param index - The index of the key to obtain
134
+ */
89
135
  keyAt(index) {
90
136
  index = Math.floor(index);
91
- const arr = [...this.keys()];
137
+ const arr = [
138
+ ...this.keys()
139
+ ];
92
140
  return arr.at(index);
93
141
  }
94
142
  random(amount) {
95
- const arr = [...this.values()];
96
- if (typeof amount === "undefined")
143
+ const arr = [
144
+ ...this.values()
145
+ ];
146
+ if (amount === void 0)
97
147
  return arr[Math.floor(Math.random() * arr.length)];
98
148
  if (!arr.length || !amount)
99
149
  return [];
100
- return Array.from(
101
- { length: Math.min(amount, arr.length) },
102
- () => arr.splice(Math.floor(Math.random() * arr.length), 1)[0]
103
- );
150
+ return Array.from({
151
+ length: Math.min(amount, arr.length)
152
+ }, () => arr.splice(Math.floor(Math.random() * arr.length), 1)[0]);
104
153
  }
105
154
  randomKey(amount) {
106
- const arr = [...this.keys()];
107
- if (typeof amount === "undefined")
155
+ const arr = [
156
+ ...this.keys()
157
+ ];
158
+ if (amount === void 0)
108
159
  return arr[Math.floor(Math.random() * arr.length)];
109
160
  if (!arr.length || !amount)
110
161
  return [];
111
- return Array.from(
112
- { length: Math.min(amount, arr.length) },
113
- () => arr.splice(Math.floor(Math.random() * arr.length), 1)[0]
114
- );
115
- }
162
+ return Array.from({
163
+ length: Math.min(amount, arr.length)
164
+ }, () => arr.splice(Math.floor(Math.random() * arr.length), 1)[0]);
165
+ }
166
+ /**
167
+ * Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse | Array.reverse()}
168
+ * but returns a Collection instead of an Array.
169
+ */
116
170
  reverse() {
117
- const entries = [...this.entries()].reverse();
171
+ const entries = [
172
+ ...this.entries()
173
+ ].reverse();
118
174
  this.clear();
119
175
  for (const [key, value] of entries)
120
176
  this.set(key, value);
@@ -123,7 +179,7 @@ var Collection = class extends Map {
123
179
  find(fn, thisArg) {
124
180
  if (typeof fn !== "function")
125
181
  throw new TypeError(`${fn} is not a function`);
126
- if (typeof thisArg !== "undefined")
182
+ if (thisArg !== void 0)
127
183
  fn = fn.bind(thisArg);
128
184
  for (const [key, val] of this) {
129
185
  if (fn(val, key, this))
@@ -134,7 +190,7 @@ var Collection = class extends Map {
134
190
  findKey(fn, thisArg) {
135
191
  if (typeof fn !== "function")
136
192
  throw new TypeError(`${fn} is not a function`);
137
- if (typeof thisArg !== "undefined")
193
+ if (thisArg !== void 0)
138
194
  fn = fn.bind(thisArg);
139
195
  for (const [key, val] of this) {
140
196
  if (fn(val, key, this))
@@ -145,7 +201,7 @@ var Collection = class extends Map {
145
201
  sweep(fn, thisArg) {
146
202
  if (typeof fn !== "function")
147
203
  throw new TypeError(`${fn} is not a function`);
148
- if (typeof thisArg !== "undefined")
204
+ if (thisArg !== void 0)
149
205
  fn = fn.bind(thisArg);
150
206
  const previousSize = this.size;
151
207
  for (const [key, val] of this) {
@@ -157,7 +213,7 @@ var Collection = class extends Map {
157
213
  filter(fn, thisArg) {
158
214
  if (typeof fn !== "function")
159
215
  throw new TypeError(`${fn} is not a function`);
160
- if (typeof thisArg !== "undefined")
216
+ if (thisArg !== void 0)
161
217
  fn = fn.bind(thisArg);
162
218
  const results = new this.constructor[Symbol.species]();
163
219
  for (const [key, val] of this) {
@@ -169,7 +225,7 @@ var Collection = class extends Map {
169
225
  partition(fn, thisArg) {
170
226
  if (typeof fn !== "function")
171
227
  throw new TypeError(`${fn} is not a function`);
172
- if (typeof thisArg !== "undefined")
228
+ if (thisArg !== void 0)
173
229
  fn = fn.bind(thisArg);
174
230
  const results = [
175
231
  new this.constructor[Symbol.species](),
@@ -191,10 +247,12 @@ var Collection = class extends Map {
191
247
  map(fn, thisArg) {
192
248
  if (typeof fn !== "function")
193
249
  throw new TypeError(`${fn} is not a function`);
194
- if (typeof thisArg !== "undefined")
250
+ if (thisArg !== void 0)
195
251
  fn = fn.bind(thisArg);
196
252
  const iter = this.entries();
197
- return Array.from({ length: this.size }, () => {
253
+ return Array.from({
254
+ length: this.size
255
+ }, () => {
198
256
  const [key, value] = iter.next().value;
199
257
  return fn(value, key, this);
200
258
  });
@@ -202,7 +260,7 @@ var Collection = class extends Map {
202
260
  mapValues(fn, thisArg) {
203
261
  if (typeof fn !== "function")
204
262
  throw new TypeError(`${fn} is not a function`);
205
- if (typeof thisArg !== "undefined")
263
+ if (thisArg !== void 0)
206
264
  fn = fn.bind(thisArg);
207
265
  const coll = new this.constructor[Symbol.species]();
208
266
  for (const [key, val] of this)
@@ -212,7 +270,7 @@ var Collection = class extends Map {
212
270
  some(fn, thisArg) {
213
271
  if (typeof fn !== "function")
214
272
  throw new TypeError(`${fn} is not a function`);
215
- if (typeof thisArg !== "undefined")
273
+ if (thisArg !== void 0)
216
274
  fn = fn.bind(thisArg);
217
275
  for (const [key, val] of this) {
218
276
  if (fn(val, key, this))
@@ -223,7 +281,7 @@ var Collection = class extends Map {
223
281
  every(fn, thisArg) {
224
282
  if (typeof fn !== "function")
225
283
  throw new TypeError(`${fn} is not a function`);
226
- if (typeof thisArg !== "undefined")
284
+ if (thisArg !== void 0)
227
285
  fn = fn.bind(thisArg);
228
286
  for (const [key, val] of this) {
229
287
  if (!fn(val, key, this))
@@ -231,11 +289,23 @@ var Collection = class extends Map {
231
289
  }
232
290
  return true;
233
291
  }
292
+ /**
293
+ * Applies a function to produce a single value. Identical in behavior to
294
+ * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce | Array.reduce()}.
295
+ *
296
+ * @param fn - Function used to reduce, taking four arguments; `accumulator`, `currentValue`, `currentKey`,
297
+ * and `collection`
298
+ * @param initialValue - Starting value for the accumulator
299
+ * @example
300
+ * ```ts
301
+ * collection.reduce((acc, guild) => acc + guild.memberCount, 0);
302
+ * ```
303
+ */
234
304
  reduce(fn, initialValue) {
235
305
  if (typeof fn !== "function")
236
306
  throw new TypeError(`${fn} is not a function`);
237
307
  let accumulator;
238
- if (typeof initialValue !== "undefined") {
308
+ if (initialValue !== void 0) {
239
309
  accumulator = initialValue;
240
310
  for (const [key, val] of this)
241
311
  accumulator = fn(accumulator, val, key, this);
@@ -264,14 +334,31 @@ var Collection = class extends Map {
264
334
  tap(fn, thisArg) {
265
335
  if (typeof fn !== "function")
266
336
  throw new TypeError(`${fn} is not a function`);
267
- if (typeof thisArg !== "undefined")
337
+ if (thisArg !== void 0)
268
338
  fn = fn.bind(thisArg);
269
339
  fn(this);
270
340
  return this;
271
341
  }
342
+ /**
343
+ * Creates an identical shallow copy of this collection.
344
+ *
345
+ * @example
346
+ * ```ts
347
+ * const newColl = someColl.clone();
348
+ * ```
349
+ */
272
350
  clone() {
273
351
  return new this.constructor[Symbol.species](this);
274
352
  }
353
+ /**
354
+ * Combines this collection with others into a new collection. None of the source collections are modified.
355
+ *
356
+ * @param collections - Collections to merge
357
+ * @example
358
+ * ```ts
359
+ * const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl);
360
+ * ```
361
+ */
275
362
  concat(...collections) {
276
363
  const newColl = this.clone();
277
364
  for (const coll of collections) {
@@ -280,6 +367,14 @@ var Collection = class extends Map {
280
367
  }
281
368
  return newColl;
282
369
  }
370
+ /**
371
+ * Checks if this collection shares identical items with another.
372
+ * This is different to checking for equality using equal-signs, because
373
+ * the collections may be different objects, but contain the same data.
374
+ *
375
+ * @param collection - Collection to compare with
376
+ * @returns Whether the collections have identical contents
377
+ */
283
378
  equals(collection) {
284
379
  if (!collection)
285
380
  return false;
@@ -294,8 +389,22 @@ var Collection = class extends Map {
294
389
  }
295
390
  return true;
296
391
  }
392
+ /**
393
+ * The sort method sorts the items of a collection in place and returns it.
394
+ * The sort is not necessarily stable in Node 10 or older.
395
+ * The default sort order is according to string Unicode code points.
396
+ *
397
+ * @param compareFunction - Specifies a function that defines the sort order.
398
+ * If omitted, the collection is sorted according to each character's Unicode code point value, according to the string conversion of each element.
399
+ * @example
400
+ * ```ts
401
+ * collection.sort((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
402
+ * ```
403
+ */
297
404
  sort(compareFunction = Collection.defaultSort) {
298
- const entries = [...this.entries()];
405
+ const entries = [
406
+ ...this.entries()
407
+ ];
299
408
  entries.sort((a, b) => compareFunction(a[1], b[1], a[0], b[0]));
300
409
  super.clear();
301
410
  for (const [k, v] of entries) {
@@ -303,6 +412,11 @@ var Collection = class extends Map {
303
412
  }
304
413
  return this;
305
414
  }
415
+ /**
416
+ * The intersect method returns a new structure containing items where the keys and values are present in both original structures.
417
+ *
418
+ * @param other - The other Collection to filter against
419
+ */
306
420
  intersect(other) {
307
421
  const coll = new this.constructor[Symbol.species]();
308
422
  for (const [k, v] of other) {
@@ -312,6 +426,11 @@ var Collection = class extends Map {
312
426
  }
313
427
  return coll;
314
428
  }
429
+ /**
430
+ * The subtract method returns a new structure containing items where the keys and values of the original structure are not present in the other.
431
+ *
432
+ * @param other - The other Collection to filter against
433
+ */
315
434
  subtract(other) {
316
435
  const coll = new this.constructor[Symbol.species]();
317
436
  for (const [k, v] of this) {
@@ -321,6 +440,11 @@ var Collection = class extends Map {
321
440
  }
322
441
  return coll;
323
442
  }
443
+ /**
444
+ * The difference method returns a new structure containing items where the key is present in one of the original structures but not the other.
445
+ *
446
+ * @param other - The other Collection to filter against
447
+ */
324
448
  difference(other) {
325
449
  const coll = new this.constructor[Symbol.species]();
326
450
  for (const [k, v] of other) {
@@ -333,9 +457,40 @@ var Collection = class extends Map {
333
457
  }
334
458
  return coll;
335
459
  }
460
+ /**
461
+ * Merges two Collections together into a new Collection.
462
+ *
463
+ * @param other - The other Collection to merge with
464
+ * @param whenInSelf - Function getting the result if the entry only exists in this Collection
465
+ * @param whenInOther - Function getting the result if the entry only exists in the other Collection
466
+ * @param whenInBoth - Function getting the result if the entry exists in both Collections
467
+ * @example
468
+ * ```ts
469
+ * // Sums up the entries in two collections.
470
+ * coll.merge(
471
+ * other,
472
+ * x => ({ keep: true, value: x }),
473
+ * y => ({ keep: true, value: y }),
474
+ * (x, y) => ({ keep: true, value: x + y }),
475
+ * );
476
+ * ```
477
+ * @example
478
+ * ```ts
479
+ * // Intersects two collections in a left-biased manner.
480
+ * coll.merge(
481
+ * other,
482
+ * x => ({ keep: false }),
483
+ * y => ({ keep: false }),
484
+ * (x, _) => ({ keep: true, value: x }),
485
+ * );
486
+ * ```
487
+ */
336
488
  merge(other, whenInSelf, whenInOther, whenInBoth) {
337
489
  const coll = new this.constructor[Symbol.species]();
338
- const keys = /* @__PURE__ */ new Set([...this.keys(), ...other.keys()]);
490
+ const keys = /* @__PURE__ */ new Set([
491
+ ...this.keys(),
492
+ ...other.keys()
493
+ ]);
339
494
  for (const k of keys) {
340
495
  const hasInSelf = this.has(k);
341
496
  const hasInOther = other.has(k);
@@ -355,15 +510,41 @@ var Collection = class extends Map {
355
510
  }
356
511
  return coll;
357
512
  }
513
+ /**
514
+ * The sorted method sorts the items of a collection and returns it.
515
+ * The sort is not necessarily stable in Node 10 or older.
516
+ * The default sort order is according to string Unicode code points.
517
+ *
518
+ * @param compareFunction - Specifies a function that defines the sort order.
519
+ * If omitted, the collection is sorted according to each character's Unicode code point value,
520
+ * according to the string conversion of each element.
521
+ * @example
522
+ * ```ts
523
+ * collection.sorted((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
524
+ * ```
525
+ */
358
526
  sorted(compareFunction = Collection.defaultSort) {
359
527
  return new this.constructor[Symbol.species](this).sort((av, bv, ak, bk) => compareFunction(av, bv, ak, bk));
360
528
  }
361
529
  toJSON() {
362
- return [...this.values()];
530
+ return [
531
+ ...this.values()
532
+ ];
363
533
  }
364
534
  static defaultSort(firstValue, secondValue) {
365
535
  return Number(firstValue > secondValue) || Number(firstValue === secondValue) - 1;
366
536
  }
537
+ /**
538
+ * Creates a Collection from a list of entries.
539
+ *
540
+ * @param entries - The list of entries
541
+ * @param combine - Function to combine an existing entry with a new one
542
+ * @example
543
+ * ```ts
544
+ * Collection.combineEntries([["a", 1], ["b", 2], ["a", 2]], (x, y) => x + y);
545
+ * // returns Collection { "a" => 3, "b" => 2 }
546
+ * ```
547
+ */
367
548
  static combineEntries(entries, combine) {
368
549
  const coll = new Collection();
369
550
  for (const [k, v] of entries) {
@@ -379,7 +560,7 @@ var Collection = class extends Map {
379
560
  __name(Collection, "Collection");
380
561
 
381
562
  // src/index.ts
382
- var version = "1.4.0-dev.1677499493-ffdb197.0";
563
+ var version = "[VI]{{inject}}[/VI]";
383
564
  // Annotate the CommonJS export names for ESM import in node:
384
565
  0 && (module.exports = {
385
566
  Collection,