@discordjs/collection
Advanced tools
Comparing version 2.1.2-dev.1730851912-c34a57b79 to 2.1.2-dev.1730981092-c97310681
/** | ||
* @internal | ||
*/ | ||
interface CollectionConstructor { | ||
new (): Collection<unknown, unknown>; | ||
new <Key, Value>(entries?: readonly (readonly [Key, Value])[] | null): Collection<Key, Value>; | ||
new <Key, Value>(iterable: Iterable<readonly [Key, Value]>): Collection<Key, Value>; | ||
readonly prototype: Collection<unknown, unknown>; | ||
readonly [Symbol.species]: CollectionConstructor; | ||
} | ||
/** | ||
* Represents an immutable version of a collection | ||
*/ | ||
type ReadonlyCollection<Key, Value> = Omit<Collection<Key, Value>, 'clear' | 'delete' | 'ensure' | 'forEach' | 'get' | 'reverse' | 'set' | 'sort' | 'sweep'> & ReadonlyMap<Key, Value>; | ||
/** | ||
* Separate interface for the constructor so that emitted js does not have a constructor that overwrites itself | ||
* | ||
* @internal | ||
*/ | ||
interface Collection<Key, Value> extends Map<Key, Value> { | ||
constructor: CollectionConstructor; | ||
interface Collection<Key, Value> { | ||
/** | ||
* Ambient declaration to allow `this.constructor[@@species]` in class methods. | ||
* | ||
* @internal | ||
*/ | ||
constructor: typeof Collection & { | ||
readonly [Symbol.species]: typeof Collection; | ||
}; | ||
} | ||
@@ -537,2 +529,2 @@ /** | ||
export { Collection, type CollectionConstructor, type Comparator, type Keep, type ReadonlyCollection, version }; | ||
export { Collection, type Comparator, type Keep, type ReadonlyCollection, version }; |
@@ -72,5 +72,9 @@ "use strict"; | ||
if (amount < 0) return this.last(amount * -1); | ||
amount = Math.min(this.size, amount); | ||
if (amount >= this.size) return [...this.values()]; | ||
const iter = this.values(); | ||
return Array.from({ length: amount }, () => iter.next().value); | ||
const results = new Array(amount); | ||
for (let index = 0; index < amount; index++) { | ||
results[index] = iter.next().value; | ||
} | ||
return results; | ||
} | ||
@@ -80,19 +84,23 @@ firstKey(amount) { | ||
if (amount < 0) return this.lastKey(amount * -1); | ||
amount = Math.min(this.size, amount); | ||
if (amount >= this.size) return [...this.keys()]; | ||
const iter = this.keys(); | ||
return Array.from({ length: amount }, () => iter.next().value); | ||
const results = new Array(amount); | ||
for (let index = 0; index < amount; index++) { | ||
results[index] = iter.next().value; | ||
} | ||
return results; | ||
} | ||
last(amount) { | ||
if (amount === void 0) return this.at(-1); | ||
if (!amount) return []; | ||
if (amount < 0) return this.first(amount * -1); | ||
const arr = [...this.values()]; | ||
if (amount === void 0) return arr[arr.length - 1]; | ||
if (amount < 0) return this.first(amount * -1); | ||
if (!amount) return []; | ||
return arr.slice(-amount); | ||
return arr.slice(amount * -1); | ||
} | ||
lastKey(amount) { | ||
if (amount === void 0) return this.keyAt(-1); | ||
if (!amount) return []; | ||
if (amount < 0) return this.firstKey(amount * -1); | ||
const arr = [...this.keys()]; | ||
if (amount === void 0) return arr[arr.length - 1]; | ||
if (amount < 0) return this.firstKey(amount * -1); | ||
if (!amount) return []; | ||
return arr.slice(-amount); | ||
return arr.slice(amount * -1); | ||
} | ||
@@ -107,5 +115,14 @@ /** | ||
at(index) { | ||
index = Math.floor(index); | ||
const arr = [...this.values()]; | ||
return arr.at(index); | ||
index = Math.trunc(index); | ||
if (index >= 0) { | ||
if (index >= this.size) return void 0; | ||
} else { | ||
index += this.size; | ||
if (index < 0) return void 0; | ||
} | ||
const iter = this.values(); | ||
for (let skip = 0; skip < index; skip++) { | ||
iter.next(); | ||
} | ||
return iter.next().value; | ||
} | ||
@@ -120,23 +137,36 @@ /** | ||
keyAt(index) { | ||
index = Math.floor(index); | ||
const arr = [...this.keys()]; | ||
return arr.at(index); | ||
index = Math.trunc(index); | ||
if (index >= 0) { | ||
if (index >= this.size) return void 0; | ||
} else { | ||
index += this.size; | ||
if (index < 0) return void 0; | ||
} | ||
const iter = this.keys(); | ||
for (let skip = 0; skip < index; skip++) { | ||
iter.next(); | ||
} | ||
return iter.next().value; | ||
} | ||
random(amount) { | ||
const arr = [...this.values()]; | ||
if (amount === void 0) 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] | ||
); | ||
if (amount === void 0) return this.at(Math.floor(Math.random() * this.size)); | ||
amount = Math.min(this.size, amount); | ||
if (!amount) return []; | ||
const values = [...this.values()]; | ||
for (let sourceIndex = 0; sourceIndex < amount; sourceIndex++) { | ||
const targetIndex = sourceIndex + Math.floor(Math.random() * (values.length - sourceIndex)); | ||
[values[sourceIndex], values[targetIndex]] = [values[targetIndex], values[sourceIndex]]; | ||
} | ||
return values.slice(0, amount); | ||
} | ||
randomKey(amount) { | ||
const arr = [...this.keys()]; | ||
if (amount === void 0) 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] | ||
); | ||
if (amount === void 0) return this.keyAt(Math.floor(Math.random() * this.size)); | ||
amount = Math.min(this.size, amount); | ||
if (!amount) return []; | ||
const keys = [...this.keys()]; | ||
for (let sourceIndex = 0; sourceIndex < amount; sourceIndex++) { | ||
const targetIndex = sourceIndex + Math.floor(Math.random() * (keys.length - sourceIndex)); | ||
[keys[sourceIndex], keys[targetIndex]] = [keys[targetIndex], keys[sourceIndex]]; | ||
} | ||
return keys.slice(0, amount); | ||
} | ||
@@ -233,6 +263,8 @@ /** | ||
const iter = this.entries(); | ||
return Array.from({ length: this.size }, () => { | ||
const results = new Array(this.size); | ||
for (let index = 0; index < this.size; index++) { | ||
const [key, value] = iter.next().value; | ||
return fn(value, key, this); | ||
}); | ||
results[index] = fn(value, key, this); | ||
} | ||
return results; | ||
} | ||
@@ -499,8 +531,10 @@ mapValues(fn, thisArg) { | ||
const hasInOther = other.has(key); | ||
if (hasInSelf && hasInOther) { | ||
const result = whenInBoth(this.get(key), other.get(key), key); | ||
if (result.keep) coll.set(key, result.value); | ||
} else if (hasInSelf) { | ||
const result = whenInSelf(this.get(key), key); | ||
if (result.keep) coll.set(key, result.value); | ||
if (hasInSelf) { | ||
if (hasInOther) { | ||
const result = whenInBoth(this.get(key), other.get(key), key); | ||
if (result.keep) coll.set(key, result.value); | ||
} else { | ||
const result = whenInSelf(this.get(key), key); | ||
if (result.keep) coll.set(key, result.value); | ||
} | ||
} else if (hasInOther) { | ||
@@ -534,3 +568,3 @@ const result = whenInOther(other.get(key), key); | ||
toSorted(compareFunction = _Collection.defaultSort) { | ||
return new this.constructor[Symbol.species](this).sort((av, bv, ak, bk) => compareFunction(av, bv, ak, bk)); | ||
return new this.constructor[Symbol.species](this).sort(compareFunction); | ||
} | ||
@@ -568,3 +602,3 @@ toJSON() { | ||
// src/index.ts | ||
var version = "2.1.2-dev.1730851912-c34a57b79"; | ||
var version = "2.1.2-dev.1730981092-c97310681"; | ||
// Annotate the CommonJS export names for ESM import in node: | ||
@@ -571,0 +605,0 @@ 0 && (module.exports = { |
{ | ||
"$schema": "https://json.schemastore.org/package.json", | ||
"name": "@discordjs/collection", | ||
"version": "2.1.2-dev.1730851912-c34a57b79", | ||
"version": "2.1.2-dev.1730981092-c97310681", | ||
"description": "Utility data structure used in discord.js", | ||
@@ -6,0 +6,0 @@ "exports": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
219650
1697