slash-create
Version:
Create and sync Discord slash commands!
168 lines (167 loc) • 5.92 kB
JavaScript
"use strict";
/* eslint-disable no-dupe-class-members */
Object.defineProperty(exports, "__esModule", { value: true });
exports.Collection = void 0;
/** A map with a subset of extra features from [@discordjs/collection](https://npm.im/@discordjs/collection). */
class Collection extends Map {
first(amount) {
if (typeof amount === 'undefined')
return this.values().next().value;
if (amount < 0)
return this.last(amount * -1);
amount = Math.min(this.size, amount);
const iter = this.values();
return Array.from({ length: amount }, () => iter.next().value);
}
last(amount) {
const arr = [...this.values()];
if (typeof amount === 'undefined')
return arr[arr.length - 1];
if (amount < 0)
return this.first(amount * -1);
if (!amount)
return [];
return arr.slice(-amount);
}
random(amount) {
const arr = [...this.values()];
if (typeof amount === 'undefined')
return arr[Math.floor(Math.random() * arr.length)];
if (!arr.length || !amount)
return [];
return Array.from({ length: Math.min(amount, arr.length) }, () => arr.splice(Math.floor(Math.random() * arr.length), 1)[0]);
}
filter(fn, thisArg) {
if (typeof thisArg !== 'undefined')
fn = fn.bind(thisArg);
// @ts-ignore
const results = new this.constructor[Symbol.species]();
for (const [key, val] of this) {
if (fn(val, key, this))
results.set(key, val);
}
return results;
}
find(fn, thisArg) {
if (typeof thisArg !== 'undefined')
fn = fn.bind(thisArg);
for (const [key, val] of this) {
if (fn(val, key, this))
return val;
}
return undefined;
}
map(fn, thisArg) {
if (typeof thisArg !== 'undefined')
fn = fn.bind(thisArg);
const iter = this.entries();
return Array.from({ length: this.size }, () => {
const [key, value] = iter.next().value;
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
return fn(value, key, this);
});
}
some(fn, thisArg) {
if (typeof thisArg !== 'undefined')
fn = fn.bind(thisArg);
for (const [key, val] of this) {
if (fn(val, key, this))
return true;
}
return false;
}
sweep(fn, thisArg) {
if (typeof thisArg !== 'undefined')
fn = fn.bind(thisArg);
const previousSize = this.size;
for (const [key, val] of this) {
if (fn(val, key, this))
this.delete(key);
}
return previousSize - this.size;
}
every(fn, thisArg) {
if (typeof thisArg !== 'undefined')
fn = fn.bind(thisArg);
for (const [key, val] of this) {
if (!fn(val, key, this))
return false;
}
return true;
}
/**
* Applies a function to produce a single value. Identical in behavior to
* [Array.reduce()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce).
* @param fn Function used to reduce, taking four arguments; `accumulator`, `currentValue`, `currentKey`,
* and `collection`
* @param nitialValue Starting value for the accumulator
* @example collection.reduce((acc, guild) => acc + guild.memberCount, 0);
*/
reduce(fn, initialValue) {
let accumulator;
if (typeof initialValue !== 'undefined') {
accumulator = initialValue;
for (const [key, val] of this)
accumulator = fn(accumulator, val, key, this);
return accumulator;
}
let first = true;
for (const [key, val] of this) {
if (first) {
accumulator = val;
first = false;
continue;
}
accumulator = fn(accumulator, val, key, this);
}
// No items iterated.
if (first) {
throw new TypeError('Reduce of empty collection with no initial value');
}
return accumulator;
}
/**
* Creates an identical shallow copy of this collection.
* @example const newColl = someColl.clone();
*/
clone() {
// @ts-ignore
return new this.constructor[Symbol.species](this);
}
/**
* Combines this collection with others into a new collection. None of the source collections are modified.
* @param collections Collections to merge
* @example const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl);
*/
concat(...collections) {
const newColl = this.clone();
for (const coll of collections) {
for (const [key, val] of coll)
newColl.set(key, val);
}
return newColl;
}
/**
* Checks if this collection shares identical items with another.
* This is different to checking for equality using equal-signs, because
* the collections may be different objects, but contain the same data.
* @param collection Collection to compare with
* @returns Whether the collections have identical contents
*/
equals(collection) {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!collection)
return false; // runtime check
if (this === collection)
return true;
if (this.size !== collection.size)
return false;
for (const [key, value] of this) {
if (!collection.has(key) || value !== collection.get(key)) {
return false;
}
}
return true;
}
}
exports.Collection = Collection;