@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/dist/index.mjs CHANGED
@@ -3,6 +3,16 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:
3
3
 
4
4
  // src/collection.ts
5
5
  var Collection = class extends Map {
6
+ /**
7
+ * Obtains the value of the given key if it exists, otherwise sets and returns the value provided by the default value generator.
8
+ *
9
+ * @param key - The key to get if it exists, or set otherwise
10
+ * @param defaultValueGenerator - A function that generates the default value
11
+ * @example
12
+ * ```ts
13
+ * collection.ensure(guildId, () => defaultGuildConfig);
14
+ * ```
15
+ */
6
16
  ensure(key, defaultValueGenerator) {
7
17
  if (this.has(key))
8
18
  return this.get(key);
@@ -12,33 +22,51 @@ var Collection = class extends Map {
12
22
  this.set(key, defaultValue);
13
23
  return defaultValue;
14
24
  }
25
+ /**
26
+ * Checks if all of the elements exist in the collection.
27
+ *
28
+ * @param keys - The keys of the elements to check for
29
+ * @returns `true` if all of the elements exist, `false` if at least one does not exist.
30
+ */
15
31
  hasAll(...keys) {
16
32
  return keys.every((k) => super.has(k));
17
33
  }
34
+ /**
35
+ * Checks if any of the elements exist in the collection.
36
+ *
37
+ * @param keys - The keys of the elements to check for
38
+ * @returns `true` if any of the elements exist, `false` if none exist.
39
+ */
18
40
  hasAny(...keys) {
19
41
  return keys.some((k) => super.has(k));
20
42
  }
21
43
  first(amount) {
22
- if (typeof amount === "undefined")
44
+ if (amount === void 0)
23
45
  return this.values().next().value;
24
46
  if (amount < 0)
25
47
  return this.last(amount * -1);
26
48
  amount = Math.min(this.size, amount);
27
49
  const iter = this.values();
28
- return Array.from({ length: amount }, () => iter.next().value);
50
+ return Array.from({
51
+ length: amount
52
+ }, () => iter.next().value);
29
53
  }
30
54
  firstKey(amount) {
31
- if (typeof amount === "undefined")
55
+ if (amount === void 0)
32
56
  return this.keys().next().value;
33
57
  if (amount < 0)
34
58
  return this.lastKey(amount * -1);
35
59
  amount = Math.min(this.size, amount);
36
60
  const iter = this.keys();
37
- return Array.from({ length: amount }, () => iter.next().value);
61
+ return Array.from({
62
+ length: amount
63
+ }, () => iter.next().value);
38
64
  }
39
65
  last(amount) {
40
- const arr = [...this.values()];
41
- if (typeof amount === "undefined")
66
+ const arr = [
67
+ ...this.values()
68
+ ];
69
+ if (amount === void 0)
42
70
  return arr[arr.length - 1];
43
71
  if (amount < 0)
44
72
  return this.first(amount * -1);
@@ -47,8 +75,10 @@ var Collection = class extends Map {
47
75
  return arr.slice(-amount);
48
76
  }
49
77
  lastKey(amount) {
50
- const arr = [...this.keys()];
51
- if (typeof amount === "undefined")
78
+ const arr = [
79
+ ...this.keys()
80
+ ];
81
+ if (amount === void 0)
52
82
  return arr[arr.length - 1];
53
83
  if (amount < 0)
54
84
  return this.firstKey(amount * -1);
@@ -56,40 +86,66 @@ var Collection = class extends Map {
56
86
  return [];
57
87
  return arr.slice(-amount);
58
88
  }
89
+ /**
90
+ * Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
91
+ * Returns the item at a given index, allowing for positive and negative integers.
92
+ * Negative integers count back from the last item in the collection.
93
+ *
94
+ * @param index - The index of the element to obtain
95
+ */
59
96
  at(index) {
60
97
  index = Math.floor(index);
61
- const arr = [...this.values()];
98
+ const arr = [
99
+ ...this.values()
100
+ ];
62
101
  return arr.at(index);
63
102
  }
103
+ /**
104
+ * Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
105
+ * Returns the key at a given index, allowing for positive and negative integers.
106
+ * Negative integers count back from the last item in the collection.
107
+ *
108
+ * @param index - The index of the key to obtain
109
+ */
64
110
  keyAt(index) {
65
111
  index = Math.floor(index);
66
- const arr = [...this.keys()];
112
+ const arr = [
113
+ ...this.keys()
114
+ ];
67
115
  return arr.at(index);
68
116
  }
69
117
  random(amount) {
70
- const arr = [...this.values()];
71
- if (typeof amount === "undefined")
118
+ const arr = [
119
+ ...this.values()
120
+ ];
121
+ if (amount === void 0)
72
122
  return arr[Math.floor(Math.random() * arr.length)];
73
123
  if (!arr.length || !amount)
74
124
  return [];
75
- return Array.from(
76
- { length: Math.min(amount, arr.length) },
77
- () => arr.splice(Math.floor(Math.random() * arr.length), 1)[0]
78
- );
125
+ return Array.from({
126
+ length: Math.min(amount, arr.length)
127
+ }, () => arr.splice(Math.floor(Math.random() * arr.length), 1)[0]);
79
128
  }
80
129
  randomKey(amount) {
81
- const arr = [...this.keys()];
82
- if (typeof amount === "undefined")
130
+ const arr = [
131
+ ...this.keys()
132
+ ];
133
+ if (amount === void 0)
83
134
  return arr[Math.floor(Math.random() * arr.length)];
84
135
  if (!arr.length || !amount)
85
136
  return [];
86
- return Array.from(
87
- { length: Math.min(amount, arr.length) },
88
- () => arr.splice(Math.floor(Math.random() * arr.length), 1)[0]
89
- );
90
- }
137
+ return Array.from({
138
+ length: Math.min(amount, arr.length)
139
+ }, () => arr.splice(Math.floor(Math.random() * arr.length), 1)[0]);
140
+ }
141
+ /**
142
+ * Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse | Array.reverse()}
143
+ * but returns a Collection instead of an Array.
144
+ */
91
145
  reverse() {
92
- const entries = [...this.entries()].reverse();
146
+ const entries = [
147
+ ...this.entries()
148
+ ].reverse();
93
149
  this.clear();
94
150
  for (const [key, value] of entries)
95
151
  this.set(key, value);
@@ -98,7 +154,7 @@ var Collection = class extends Map {
98
154
  find(fn, thisArg) {
99
155
  if (typeof fn !== "function")
100
156
  throw new TypeError(`${fn} is not a function`);
101
- if (typeof thisArg !== "undefined")
157
+ if (thisArg !== void 0)
102
158
  fn = fn.bind(thisArg);
103
159
  for (const [key, val] of this) {
104
160
  if (fn(val, key, this))
@@ -109,7 +165,7 @@ var Collection = class extends Map {
109
165
  findKey(fn, thisArg) {
110
166
  if (typeof fn !== "function")
111
167
  throw new TypeError(`${fn} is not a function`);
112
- if (typeof thisArg !== "undefined")
168
+ if (thisArg !== void 0)
113
169
  fn = fn.bind(thisArg);
114
170
  for (const [key, val] of this) {
115
171
  if (fn(val, key, this))
@@ -120,7 +176,7 @@ var Collection = class extends Map {
120
176
  sweep(fn, thisArg) {
121
177
  if (typeof fn !== "function")
122
178
  throw new TypeError(`${fn} is not a function`);
123
- if (typeof thisArg !== "undefined")
179
+ if (thisArg !== void 0)
124
180
  fn = fn.bind(thisArg);
125
181
  const previousSize = this.size;
126
182
  for (const [key, val] of this) {
@@ -132,7 +188,7 @@ var Collection = class extends Map {
132
188
  filter(fn, thisArg) {
133
189
  if (typeof fn !== "function")
134
190
  throw new TypeError(`${fn} is not a function`);
135
- if (typeof thisArg !== "undefined")
191
+ if (thisArg !== void 0)
136
192
  fn = fn.bind(thisArg);
137
193
  const results = new this.constructor[Symbol.species]();
138
194
  for (const [key, val] of this) {
@@ -144,7 +200,7 @@ var Collection = class extends Map {
144
200
  partition(fn, thisArg) {
145
201
  if (typeof fn !== "function")
146
202
  throw new TypeError(`${fn} is not a function`);
147
- if (typeof thisArg !== "undefined")
203
+ if (thisArg !== void 0)
148
204
  fn = fn.bind(thisArg);
149
205
  const results = [
150
206
  new this.constructor[Symbol.species](),
@@ -166,10 +222,12 @@ var Collection = class extends Map {
166
222
  map(fn, thisArg) {
167
223
  if (typeof fn !== "function")
168
224
  throw new TypeError(`${fn} is not a function`);
169
- if (typeof thisArg !== "undefined")
225
+ if (thisArg !== void 0)
170
226
  fn = fn.bind(thisArg);
171
227
  const iter = this.entries();
172
- return Array.from({ length: this.size }, () => {
228
+ return Array.from({
229
+ length: this.size
230
+ }, () => {
173
231
  const [key, value] = iter.next().value;
174
232
  return fn(value, key, this);
175
233
  });
@@ -177,7 +235,7 @@ var Collection = class extends Map {
177
235
  mapValues(fn, thisArg) {
178
236
  if (typeof fn !== "function")
179
237
  throw new TypeError(`${fn} is not a function`);
180
- if (typeof thisArg !== "undefined")
238
+ if (thisArg !== void 0)
181
239
  fn = fn.bind(thisArg);
182
240
  const coll = new this.constructor[Symbol.species]();
183
241
  for (const [key, val] of this)
@@ -187,7 +245,7 @@ var Collection = class extends Map {
187
245
  some(fn, thisArg) {
188
246
  if (typeof fn !== "function")
189
247
  throw new TypeError(`${fn} is not a function`);
190
- if (typeof thisArg !== "undefined")
248
+ if (thisArg !== void 0)
191
249
  fn = fn.bind(thisArg);
192
250
  for (const [key, val] of this) {
193
251
  if (fn(val, key, this))
@@ -198,7 +256,7 @@ var Collection = class extends Map {
198
256
  every(fn, thisArg) {
199
257
  if (typeof fn !== "function")
200
258
  throw new TypeError(`${fn} is not a function`);
201
- if (typeof thisArg !== "undefined")
259
+ if (thisArg !== void 0)
202
260
  fn = fn.bind(thisArg);
203
261
  for (const [key, val] of this) {
204
262
  if (!fn(val, key, this))
@@ -206,11 +264,23 @@ var Collection = class extends Map {
206
264
  }
207
265
  return true;
208
266
  }
267
+ /**
268
+ * Applies a function to produce a single value. Identical in behavior to
269
+ * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce | Array.reduce()}.
270
+ *
271
+ * @param fn - Function used to reduce, taking four arguments; `accumulator`, `currentValue`, `currentKey`,
272
+ * and `collection`
273
+ * @param initialValue - Starting value for the accumulator
274
+ * @example
275
+ * ```ts
276
+ * collection.reduce((acc, guild) => acc + guild.memberCount, 0);
277
+ * ```
278
+ */
209
279
  reduce(fn, initialValue) {
210
280
  if (typeof fn !== "function")
211
281
  throw new TypeError(`${fn} is not a function`);
212
282
  let accumulator;
213
- if (typeof initialValue !== "undefined") {
283
+ if (initialValue !== void 0) {
214
284
  accumulator = initialValue;
215
285
  for (const [key, val] of this)
216
286
  accumulator = fn(accumulator, val, key, this);
@@ -239,14 +309,31 @@ var Collection = class extends Map {
239
309
  tap(fn, thisArg) {
240
310
  if (typeof fn !== "function")
241
311
  throw new TypeError(`${fn} is not a function`);
242
- if (typeof thisArg !== "undefined")
312
+ if (thisArg !== void 0)
243
313
  fn = fn.bind(thisArg);
244
314
  fn(this);
245
315
  return this;
246
316
  }
317
+ /**
318
+ * Creates an identical shallow copy of this collection.
319
+ *
320
+ * @example
321
+ * ```ts
322
+ * const newColl = someColl.clone();
323
+ * ```
324
+ */
247
325
  clone() {
248
326
  return new this.constructor[Symbol.species](this);
249
327
  }
328
+ /**
329
+ * Combines this collection with others into a new collection. None of the source collections are modified.
330
+ *
331
+ * @param collections - Collections to merge
332
+ * @example
333
+ * ```ts
334
+ * const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl);
335
+ * ```
336
+ */
250
337
  concat(...collections) {
251
338
  const newColl = this.clone();
252
339
  for (const coll of collections) {
@@ -255,6 +342,14 @@ var Collection = class extends Map {
255
342
  }
256
343
  return newColl;
257
344
  }
345
+ /**
346
+ * Checks if this collection shares identical items with another.
347
+ * This is different to checking for equality using equal-signs, because
348
+ * the collections may be different objects, but contain the same data.
349
+ *
350
+ * @param collection - Collection to compare with
351
+ * @returns Whether the collections have identical contents
352
+ */
258
353
  equals(collection) {
259
354
  if (!collection)
260
355
  return false;
@@ -269,8 +364,22 @@ var Collection = class extends Map {
269
364
  }
270
365
  return true;
271
366
  }
367
+ /**
368
+ * The sort method sorts the items of a collection in place and returns it.
369
+ * The sort is not necessarily stable in Node 10 or older.
370
+ * The default sort order is according to string Unicode code points.
371
+ *
372
+ * @param compareFunction - Specifies a function that defines the sort order.
373
+ * If omitted, the collection is sorted according to each character's Unicode code point value, according to the string conversion of each element.
374
+ * @example
375
+ * ```ts
376
+ * collection.sort((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
377
+ * ```
378
+ */
272
379
  sort(compareFunction = Collection.defaultSort) {
273
- const entries = [...this.entries()];
380
+ const entries = [
381
+ ...this.entries()
382
+ ];
274
383
  entries.sort((a, b) => compareFunction(a[1], b[1], a[0], b[0]));
275
384
  super.clear();
276
385
  for (const [k, v] of entries) {
@@ -278,6 +387,11 @@ var Collection = class extends Map {
278
387
  }
279
388
  return this;
280
389
  }
390
+ /**
391
+ * The intersect method returns a new structure containing items where the keys and values are present in both original structures.
392
+ *
393
+ * @param other - The other Collection to filter against
394
+ */
281
395
  intersect(other) {
282
396
  const coll = new this.constructor[Symbol.species]();
283
397
  for (const [k, v] of other) {
@@ -287,6 +401,11 @@ var Collection = class extends Map {
287
401
  }
288
402
  return coll;
289
403
  }
404
+ /**
405
+ * The subtract method returns a new structure containing items where the keys and values of the original structure are not present in the other.
406
+ *
407
+ * @param other - The other Collection to filter against
408
+ */
290
409
  subtract(other) {
291
410
  const coll = new this.constructor[Symbol.species]();
292
411
  for (const [k, v] of this) {
@@ -296,6 +415,11 @@ var Collection = class extends Map {
296
415
  }
297
416
  return coll;
298
417
  }
418
+ /**
419
+ * The difference method returns a new structure containing items where the key is present in one of the original structures but not the other.
420
+ *
421
+ * @param other - The other Collection to filter against
422
+ */
299
423
  difference(other) {
300
424
  const coll = new this.constructor[Symbol.species]();
301
425
  for (const [k, v] of other) {
@@ -308,9 +432,40 @@ var Collection = class extends Map {
308
432
  }
309
433
  return coll;
310
434
  }
435
+ /**
436
+ * Merges two Collections together into a new Collection.
437
+ *
438
+ * @param other - The other Collection to merge with
439
+ * @param whenInSelf - Function getting the result if the entry only exists in this Collection
440
+ * @param whenInOther - Function getting the result if the entry only exists in the other Collection
441
+ * @param whenInBoth - Function getting the result if the entry exists in both Collections
442
+ * @example
443
+ * ```ts
444
+ * // Sums up the entries in two collections.
445
+ * coll.merge(
446
+ * other,
447
+ * x => ({ keep: true, value: x }),
448
+ * y => ({ keep: true, value: y }),
449
+ * (x, y) => ({ keep: true, value: x + y }),
450
+ * );
451
+ * ```
452
+ * @example
453
+ * ```ts
454
+ * // Intersects two collections in a left-biased manner.
455
+ * coll.merge(
456
+ * other,
457
+ * x => ({ keep: false }),
458
+ * y => ({ keep: false }),
459
+ * (x, _) => ({ keep: true, value: x }),
460
+ * );
461
+ * ```
462
+ */
311
463
  merge(other, whenInSelf, whenInOther, whenInBoth) {
312
464
  const coll = new this.constructor[Symbol.species]();
313
- const keys = /* @__PURE__ */ new Set([...this.keys(), ...other.keys()]);
465
+ const keys = /* @__PURE__ */ new Set([
466
+ ...this.keys(),
467
+ ...other.keys()
468
+ ]);
314
469
  for (const k of keys) {
315
470
  const hasInSelf = this.has(k);
316
471
  const hasInOther = other.has(k);
@@ -330,15 +485,41 @@ var Collection = class extends Map {
330
485
  }
331
486
  return coll;
332
487
  }
488
+ /**
489
+ * The sorted method sorts the items of a collection and returns it.
490
+ * The sort is not necessarily stable in Node 10 or older.
491
+ * The default sort order is according to string Unicode code points.
492
+ *
493
+ * @param compareFunction - Specifies a function that defines the sort order.
494
+ * If omitted, the collection is sorted according to each character's Unicode code point value,
495
+ * according to the string conversion of each element.
496
+ * @example
497
+ * ```ts
498
+ * collection.sorted((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
499
+ * ```
500
+ */
333
501
  sorted(compareFunction = Collection.defaultSort) {
334
502
  return new this.constructor[Symbol.species](this).sort((av, bv, ak, bk) => compareFunction(av, bv, ak, bk));
335
503
  }
336
504
  toJSON() {
337
- return [...this.values()];
505
+ return [
506
+ ...this.values()
507
+ ];
338
508
  }
339
509
  static defaultSort(firstValue, secondValue) {
340
510
  return Number(firstValue > secondValue) || Number(firstValue === secondValue) - 1;
341
511
  }
512
+ /**
513
+ * Creates a Collection from a list of entries.
514
+ *
515
+ * @param entries - The list of entries
516
+ * @param combine - Function to combine an existing entry with a new one
517
+ * @example
518
+ * ```ts
519
+ * Collection.combineEntries([["a", 1], ["b", 2], ["a", 2]], (x, y) => x + y);
520
+ * // returns Collection { "a" => 3, "b" => 2 }
521
+ * ```
522
+ */
342
523
  static combineEntries(entries, combine) {
343
524
  const coll = new Collection();
344
525
  for (const [k, v] of entries) {
@@ -354,7 +535,7 @@ var Collection = class extends Map {
354
535
  __name(Collection, "Collection");
355
536
 
356
537
  // src/index.ts
357
- var version = "1.4.0-dev.1677499493-ffdb197.0";
538
+ var version = "[VI]{{inject}}[/VI]";
358
539
  export {
359
540
  Collection,
360
541
  version