mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 17:13:31 +01:00
Cleanup Part 2: Electric Boogaloo (Reloaded) (#594)
* Cleanup Part 2: Electric Boogaloo (Reloaded) * Moar cleanup * Tweak NOT_A_PERMISSION error
This commit is contained in:
committed by
Amish Shah
parent
5a9c42061f
commit
0b908f5bce
@@ -3,10 +3,9 @@
|
||||
* @extends {Map}
|
||||
*/
|
||||
class Collection extends Map {
|
||||
|
||||
/**
|
||||
* Returns an ordered array of the values of this collection.
|
||||
* @returns {Array}
|
||||
* @returns {*[]}
|
||||
* @example
|
||||
* // identical to:
|
||||
* Array.from(collection.values());
|
||||
@@ -17,18 +16,19 @@ class Collection extends Map {
|
||||
|
||||
/**
|
||||
* Returns the first item in this collection.
|
||||
* @returns {Object}
|
||||
* @returns {*}
|
||||
* @example
|
||||
* // identical to:
|
||||
* Array.from(collection.values())[0];
|
||||
*/
|
||||
first() {
|
||||
return this.array()[0];
|
||||
return this.values().next().value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last item in this collection.
|
||||
* @returns {Object}
|
||||
* Returns the last item in this collection. This is a relatively slow operation,
|
||||
* since an array copy of the values must be made to find the last element.
|
||||
* @returns {*}
|
||||
*/
|
||||
last() {
|
||||
const arr = this.array();
|
||||
@@ -36,8 +36,9 @@ class Collection extends Map {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random item from this collection.
|
||||
* @returns {Object}
|
||||
* Returns a random item from this collection. This is a relatively slow operation,
|
||||
* since an array copy of the values must be made to find a random element.
|
||||
* @returns {*}
|
||||
*/
|
||||
random() {
|
||||
const arr = this.array();
|
||||
@@ -47,32 +48,21 @@ class Collection extends Map {
|
||||
/**
|
||||
* If the items in this collection have a delete method (e.g. messages), invoke
|
||||
* the delete method. Returns an array of promises
|
||||
* @returns {Array<Promise>}
|
||||
* @returns {Promise[]}
|
||||
*/
|
||||
deleteAll() {
|
||||
const returns = [];
|
||||
for (const item of this.values()) {
|
||||
if (item.delete) {
|
||||
returns.push(item.delete());
|
||||
}
|
||||
if (item.delete) returns.push(item.delete());
|
||||
}
|
||||
return returns;
|
||||
}
|
||||
|
||||
/**
|
||||
* The length (size) of this collection.
|
||||
* @readonly
|
||||
* @type {number}
|
||||
*/
|
||||
get length() {
|
||||
return this.size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of items where `item[key] === value` of the collection
|
||||
* @param {string} key the key to filter by
|
||||
* @param {*} value the expected value
|
||||
* @returns {Array<Object>}
|
||||
* @param {string} key The key to filter by
|
||||
* @param {*} value The expected value
|
||||
* @returns {*[]}
|
||||
* @example
|
||||
* collection.getAll('username', 'Bob');
|
||||
*/
|
||||
@@ -81,18 +71,16 @@ class Collection extends Map {
|
||||
if (typeof value === 'undefined') throw new Error('value must be specified');
|
||||
const results = [];
|
||||
for (const item of this.values()) {
|
||||
if (item[key] === value) {
|
||||
results.push(item);
|
||||
}
|
||||
if (item[key] === value) results.push(item);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a single item where `item[key] === value`
|
||||
* @param {string} key the key to filter by
|
||||
* @param {*} value the expected value
|
||||
* @returns {Object}
|
||||
* @param {string} key The key to filter by
|
||||
* @param {*} value The expected value
|
||||
* @returns {*}
|
||||
* @example
|
||||
* collection.get('id', '123123...');
|
||||
*/
|
||||
@@ -100,17 +88,15 @@ class Collection extends Map {
|
||||
if (typeof key !== 'string') throw new TypeError('key must be a string');
|
||||
if (typeof value === 'undefined') throw new Error('value must be specified');
|
||||
for (const item of this.values()) {
|
||||
if (item[key] === value) {
|
||||
return item;
|
||||
}
|
||||
if (item[key] === value) return item;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the collection has an item where `item[key] === value`
|
||||
* @param {string} key the key to filter by
|
||||
* @param {*} value the expected value
|
||||
* @param {string} key The key to filter by
|
||||
* @param {*} value The expected value
|
||||
* @returns {boolean}
|
||||
* @example
|
||||
* if (collection.exists('id', '123123...')) {
|
||||
@@ -121,32 +107,26 @@ class Collection extends Map {
|
||||
return Boolean(this.find(key, value));
|
||||
}
|
||||
|
||||
_arrayMethod(method, args) {
|
||||
return Array.prototype[method].apply(this.array(), args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Identical to
|
||||
* [Array.filter()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter),
|
||||
* but returns a Collection instead of an Array.
|
||||
* @param {function} callback the callback used to filter
|
||||
* @param {Object} [thisArg] value to set as this when filtering
|
||||
* @param {function} callback Function used to filter (should return a boolean)
|
||||
* @param {Object} [thisArg] Value to set as this when filtering
|
||||
* @returns {Collection}
|
||||
*/
|
||||
filter(...args) {
|
||||
const newArray = this.array().filter(...args);
|
||||
const collection = new Collection();
|
||||
for (const item of newArray) {
|
||||
collection.set(item.id, item);
|
||||
}
|
||||
for (const item of newArray) collection.set(item.id, item);
|
||||
return collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Functionally identical shortcut to `collection.array().map(...)`.
|
||||
* @param {function} callback Function that produces an element of the new Array, taking three arguments.
|
||||
* @param {function} callback Function that produces an element of the new Array, taking three arguments
|
||||
* @param {*} [thisArg] Optional. Value to use as this when executing callback.
|
||||
* @returns {array}
|
||||
* @returns {*[]}
|
||||
*/
|
||||
map(...args) {
|
||||
return this.array().map(...args);
|
||||
|
||||
Reference in New Issue
Block a user