From e4450c8417624b71d779cb4f41692538f9165e10 Mon Sep 17 00:00:00 2001 From: sowgro Date: Sat, 2 Sep 2023 19:12:47 -0400 Subject: first commit --- .../discord.js/src/util/LimitedCollection.js | 68 ++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 node_modules/discord.js/src/util/LimitedCollection.js (limited to 'node_modules/discord.js/src/util/LimitedCollection.js') diff --git a/node_modules/discord.js/src/util/LimitedCollection.js b/node_modules/discord.js/src/util/LimitedCollection.js new file mode 100644 index 0000000..12a8360 --- /dev/null +++ b/node_modules/discord.js/src/util/LimitedCollection.js @@ -0,0 +1,68 @@ +'use strict'; + +const { Collection } = require('@discordjs/collection'); +const { DiscordjsTypeError, ErrorCodes } = require('../errors'); + +/** + * Options for defining the behavior of a LimitedCollection + * @typedef {Object} LimitedCollectionOptions + * @property {?number} [maxSize=Infinity] The maximum size of the Collection + * @property {?Function} [keepOverLimit=null] A function, which is passed the value and key of an entry, ran to decide + * to keep an entry past the maximum size + */ + +/** + * A Collection which holds a max amount of entries. + * @extends {Collection} + * @param {LimitedCollectionOptions} [options={}] Options for constructing the Collection. + * @param {Iterable} [iterable=null] Optional entries passed to the Map constructor. + */ +class LimitedCollection extends Collection { + constructor(options = {}, iterable) { + if (typeof options !== 'object' || options === null) { + throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'options', 'object', true); + } + const { maxSize = Infinity, keepOverLimit = null } = options; + + if (typeof maxSize !== 'number') { + throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'maxSize', 'number'); + } + if (keepOverLimit !== null && typeof keepOverLimit !== 'function') { + throw new DiscordjsTypeError(ErrorCodes.InvalidType, 'keepOverLimit', 'function'); + } + + super(iterable); + + /** + * The max size of the Collection. + * @type {number} + */ + this.maxSize = maxSize; + + /** + * A function called to check if an entry should be kept when the Collection is at max size. + * @type {?Function} + */ + this.keepOverLimit = keepOverLimit; + } + + set(key, value) { + if (this.maxSize === 0 && !this.keepOverLimit?.(value, key, this)) return this; + if (this.size >= this.maxSize && !this.has(key)) { + for (const [k, v] of this.entries()) { + const keep = this.keepOverLimit?.(v, k, this) ?? false; + if (!keep) { + this.delete(k); + break; + } + } + } + return super.set(key, value); + } + + static get [Symbol.species]() { + return Collection; + } +} + +module.exports = LimitedCollection; -- cgit v1.2.3