@discordjs/collection
Advanced tools
Comparing version 0.6.0-dev.1648339700-520f471 to 0.6.0-dev.1648515283-ac4bc3a
@@ -1,2 +0,354 @@ | ||
var h=Object.defineProperty;var y=Object.getOwnPropertyDescriptor;var d=Object.getOwnPropertyNames;var b=Object.prototype.hasOwnProperty;var v=(o,e,i)=>e in o?h(o,e,{enumerable:!0,configurable:!0,writable:!0,value:i}):o[e]=i;var k=o=>h(o,"__esModule",{value:!0}),p=(o,e)=>h(o,"name",{value:e,configurable:!0});var T=(o,e)=>{for(var i in e)h(o,i,{get:e[i],enumerable:!0})},C=(o,e,i,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let l of d(e))!b.call(o,l)&&(i||l!=="default")&&h(o,l,{get:()=>e[l],enumerable:!(t=y(e,l))||t.enumerable});return o};var m=(o=>(e,i)=>o&&o.get(e)||(i=C(k({}),e,1),o&&o.set(e,i),i))(typeof WeakMap!="undefined"?new WeakMap:0);var V=(o,e,i)=>(v(o,typeof e!="symbol"?e+"":e,i),i);var g={};T(g,{Collection:()=>a,default:()=>w});var u=class extends Map{ensure(e,i){if(this.has(e))return this.get(e);let t=i(e,this);return this.set(e,t),t}hasAll(...e){return e.every(i=>super.has(i))}hasAny(...e){return e.some(i=>super.has(i))}first(e){if(typeof e=="undefined")return this.values().next().value;if(e<0)return this.last(e*-1);e=Math.min(this.size,e);let i=this.values();return Array.from({length:e},()=>i.next().value)}firstKey(e){if(typeof e=="undefined")return this.keys().next().value;if(e<0)return this.lastKey(e*-1);e=Math.min(this.size,e);let i=this.keys();return Array.from({length:e},()=>i.next().value)}last(e){let i=[...this.values()];return typeof e=="undefined"?i[i.length-1]:e<0?this.first(e*-1):e?i.slice(-e):[]}lastKey(e){let i=[...this.keys()];return typeof e=="undefined"?i[i.length-1]:e<0?this.firstKey(e*-1):e?i.slice(-e):[]}at(e){return e=Math.floor(e),[...this.values()].at(e)}keyAt(e){return e=Math.floor(e),[...this.keys()].at(e)}random(e){let i=[...this.values()];return typeof e=="undefined"?i[Math.floor(Math.random()*i.length)]:!i.length||!e?[]:Array.from({length:Math.min(e,i.length)},()=>i.splice(Math.floor(Math.random()*i.length),1)[0])}randomKey(e){let i=[...this.keys()];return typeof e=="undefined"?i[Math.floor(Math.random()*i.length)]:!i.length||!e?[]:Array.from({length:Math.min(e,i.length)},()=>i.splice(Math.floor(Math.random()*i.length),1)[0])}reverse(){let e=[...this.entries()].reverse();this.clear();for(let[i,t]of e)this.set(i,t);return this}find(e,i){typeof i!="undefined"&&(e=e.bind(i));for(let[t,l]of this)if(e(l,t,this))return l}findKey(e,i){typeof i!="undefined"&&(e=e.bind(i));for(let[t,l]of this)if(e(l,t,this))return t}sweep(e,i){typeof i!="undefined"&&(e=e.bind(i));let t=this.size;for(let[l,n]of this)e(n,l,this)&&this.delete(l);return t-this.size}filter(e,i){typeof i!="undefined"&&(e=e.bind(i));let t=new this.constructor[Symbol.species];for(let[l,n]of this)e(n,l,this)&&t.set(l,n);return t}partition(e,i){typeof i!="undefined"&&(e=e.bind(i));let t=[new this.constructor[Symbol.species],new this.constructor[Symbol.species]];for(let[l,n]of this)e(n,l,this)?t[0].set(l,n):t[1].set(l,n);return t}flatMap(e,i){let t=this.map(e,i);return new this.constructor[Symbol.species]().concat(...t)}map(e,i){typeof i!="undefined"&&(e=e.bind(i));let t=this.entries();return Array.from({length:this.size},()=>{let[l,n]=t.next().value;return e(n,l,this)})}mapValues(e,i){typeof i!="undefined"&&(e=e.bind(i));let t=new this.constructor[Symbol.species];for(let[l,n]of this)t.set(l,e(n,l,this));return t}some(e,i){typeof i!="undefined"&&(e=e.bind(i));for(let[t,l]of this)if(e(l,t,this))return!0;return!1}every(e,i){typeof i!="undefined"&&(e=e.bind(i));for(let[t,l]of this)if(!e(l,t,this))return!1;return!0}reduce(e,i){let t;if(typeof i!="undefined"){t=i;for(let[n,r]of this)t=e(t,r,n,this);return t}let l=!0;for(let[n,r]of this){if(l){t=r,l=!1;continue}t=e(t,r,n,this)}if(l)throw new TypeError("Reduce of empty collection with no initial value");return t}each(e,i){return this.forEach(e,i),this}tap(e,i){return typeof i!="undefined"&&(e=e.bind(i)),e(this),this}clone(){return new this.constructor[Symbol.species](this)}concat(...e){let i=this.clone();for(let t of e)for(let[l,n]of t)i.set(l,n);return i}equals(e){if(!e)return!1;if(this===e)return!0;if(this.size!==e.size)return!1;for(let[i,t]of this)if(!e.has(i)||t!==e.get(i))return!1;return!0}sort(e=u.defaultSort){let i=[...this.entries()];i.sort((t,l)=>e(t[1],l[1],t[0],l[0])),super.clear();for(let[t,l]of i)super.set(t,l);return this}intersect(e){let i=new this.constructor[Symbol.species];for(let[t,l]of e)this.has(t)&&Object.is(l,this.get(t))&&i.set(t,l);return i}difference(e){let i=new this.constructor[Symbol.species];for(let[t,l]of e)this.has(t)||i.set(t,l);for(let[t,l]of this)e.has(t)||i.set(t,l);return i}merge(e,i,t,l){let n=new this.constructor[Symbol.species],r=new Set([...this.keys(),...e.keys()]);for(let s of r){let f=this.has(s),K=e.has(s);if(f&&K){let c=l(this.get(s),e.get(s),s);c.keep&&n.set(s,c.value)}else if(f){let c=i(this.get(s),s);c.keep&&n.set(s,c.value)}else if(K){let c=t(e.get(s),s);c.keep&&n.set(s,c.value)}}return n}sorted(e=u.defaultSort){return new this.constructor[Symbol.species](this).sort((i,t,l,n)=>e(i,t,l,n))}toJSON(){return[...this.values()]}static defaultSort(e,i){return Number(e>i)||Number(e===i)-1}static combineEntries(e,i){let t=new u;for(let[l,n]of e)t.has(l)?t.set(l,i(t.get(l),n,l)):t.set(l,n);return t}},a=u;p(a,"Collection"),V(a,"default",u);var w=a;module.exports=m(g);0&&(module.exports={Collection}); | ||
"use strict"; | ||
var __defProp = Object.defineProperty; | ||
var __getOwnPropDesc = Object.getOwnPropertyDescriptor; | ||
var __getOwnPropNames = Object.getOwnPropertyNames; | ||
var __hasOwnProp = Object.prototype.hasOwnProperty; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true }); | ||
var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); | ||
var __export = (target, all) => { | ||
for (var name in all) | ||
__defProp(target, name, { get: all[name], enumerable: true }); | ||
}; | ||
var __reExport = (target, module2, copyDefault, desc) => { | ||
if (module2 && typeof module2 === "object" || typeof module2 === "function") { | ||
for (let key of __getOwnPropNames(module2)) | ||
if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default")) | ||
__defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable }); | ||
} | ||
return target; | ||
}; | ||
var __toCommonJS = /* @__PURE__ */ ((cache) => { | ||
return (module2, temp) => { | ||
return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp); | ||
}; | ||
})(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0); | ||
var __publicField = (obj, key, value) => { | ||
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); | ||
return value; | ||
}; | ||
// src/index.ts | ||
var src_exports = {}; | ||
__export(src_exports, { | ||
Collection: () => Collection, | ||
default: () => src_default | ||
}); | ||
var _Collection = class extends Map { | ||
ensure(key, defaultValueGenerator) { | ||
if (this.has(key)) | ||
return this.get(key); | ||
const defaultValue = defaultValueGenerator(key, this); | ||
this.set(key, defaultValue); | ||
return defaultValue; | ||
} | ||
hasAll(...keys) { | ||
return keys.every((k) => super.has(k)); | ||
} | ||
hasAny(...keys) { | ||
return keys.some((k) => super.has(k)); | ||
} | ||
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); | ||
} | ||
firstKey(amount) { | ||
if (typeof amount === "undefined") | ||
return this.keys().next().value; | ||
if (amount < 0) | ||
return this.lastKey(amount * -1); | ||
amount = Math.min(this.size, amount); | ||
const iter = this.keys(); | ||
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); | ||
} | ||
lastKey(amount) { | ||
const arr = [...this.keys()]; | ||
if (typeof amount === "undefined") | ||
return arr[arr.length - 1]; | ||
if (amount < 0) | ||
return this.firstKey(amount * -1); | ||
if (!amount) | ||
return []; | ||
return arr.slice(-amount); | ||
} | ||
at(index) { | ||
index = Math.floor(index); | ||
const arr = [...this.values()]; | ||
return arr.at(index); | ||
} | ||
keyAt(index) { | ||
index = Math.floor(index); | ||
const arr = [...this.keys()]; | ||
return arr.at(index); | ||
} | ||
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]); | ||
} | ||
randomKey(amount) { | ||
const arr = [...this.keys()]; | ||
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]); | ||
} | ||
reverse() { | ||
const entries = [...this.entries()].reverse(); | ||
this.clear(); | ||
for (const [key, value] of entries) | ||
this.set(key, value); | ||
return this; | ||
} | ||
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 void 0; | ||
} | ||
findKey(fn, thisArg) { | ||
if (typeof thisArg !== "undefined") | ||
fn = fn.bind(thisArg); | ||
for (const [key, val] of this) { | ||
if (fn(val, key, this)) | ||
return key; | ||
} | ||
return void 0; | ||
} | ||
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; | ||
} | ||
filter(fn, thisArg) { | ||
if (typeof thisArg !== "undefined") | ||
fn = fn.bind(thisArg); | ||
const results = new this.constructor[Symbol.species](); | ||
for (const [key, val] of this) { | ||
if (fn(val, key, this)) | ||
results.set(key, val); | ||
} | ||
return results; | ||
} | ||
partition(fn, thisArg) { | ||
if (typeof thisArg !== "undefined") | ||
fn = fn.bind(thisArg); | ||
const results = [ | ||
new this.constructor[Symbol.species](), | ||
new this.constructor[Symbol.species]() | ||
]; | ||
for (const [key, val] of this) { | ||
if (fn(val, key, this)) { | ||
results[0].set(key, val); | ||
} else { | ||
results[1].set(key, val); | ||
} | ||
} | ||
return results; | ||
} | ||
flatMap(fn, thisArg) { | ||
const collections = this.map(fn, thisArg); | ||
return new this.constructor[Symbol.species]().concat(...collections); | ||
} | ||
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; | ||
return fn(value, key, this); | ||
}); | ||
} | ||
mapValues(fn, thisArg) { | ||
if (typeof thisArg !== "undefined") | ||
fn = fn.bind(thisArg); | ||
const coll = new this.constructor[Symbol.species](); | ||
for (const [key, val] of this) | ||
coll.set(key, fn(val, key, this)); | ||
return coll; | ||
} | ||
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; | ||
} | ||
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; | ||
} | ||
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); | ||
} | ||
if (first) { | ||
throw new TypeError("Reduce of empty collection with no initial value"); | ||
} | ||
return accumulator; | ||
} | ||
each(fn, thisArg) { | ||
this.forEach(fn, thisArg); | ||
return this; | ||
} | ||
tap(fn, thisArg) { | ||
if (typeof thisArg !== "undefined") | ||
fn = fn.bind(thisArg); | ||
fn(this); | ||
return this; | ||
} | ||
clone() { | ||
return new this.constructor[Symbol.species](this); | ||
} | ||
concat(...collections) { | ||
const newColl = this.clone(); | ||
for (const coll of collections) { | ||
for (const [key, val] of coll) | ||
newColl.set(key, val); | ||
} | ||
return newColl; | ||
} | ||
equals(collection) { | ||
if (!collection) | ||
return false; | ||
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; | ||
} | ||
sort(compareFunction = _Collection.defaultSort) { | ||
const entries = [...this.entries()]; | ||
entries.sort((a, b) => compareFunction(a[1], b[1], a[0], b[0])); | ||
super.clear(); | ||
for (const [k, v] of entries) { | ||
super.set(k, v); | ||
} | ||
return this; | ||
} | ||
intersect(other) { | ||
const coll = new this.constructor[Symbol.species](); | ||
for (const [k, v] of other) { | ||
if (this.has(k) && Object.is(v, this.get(k))) { | ||
coll.set(k, v); | ||
} | ||
} | ||
return coll; | ||
} | ||
difference(other) { | ||
const coll = new this.constructor[Symbol.species](); | ||
for (const [k, v] of other) { | ||
if (!this.has(k)) | ||
coll.set(k, v); | ||
} | ||
for (const [k, v] of this) { | ||
if (!other.has(k)) | ||
coll.set(k, v); | ||
} | ||
return coll; | ||
} | ||
merge(other, whenInSelf, whenInOther, whenInBoth) { | ||
const coll = new this.constructor[Symbol.species](); | ||
const keys = /* @__PURE__ */ new Set([...this.keys(), ...other.keys()]); | ||
for (const k of keys) { | ||
const hasInSelf = this.has(k); | ||
const hasInOther = other.has(k); | ||
if (hasInSelf && hasInOther) { | ||
const r = whenInBoth(this.get(k), other.get(k), k); | ||
if (r.keep) | ||
coll.set(k, r.value); | ||
} else if (hasInSelf) { | ||
const r = whenInSelf(this.get(k), k); | ||
if (r.keep) | ||
coll.set(k, r.value); | ||
} else if (hasInOther) { | ||
const r = whenInOther(other.get(k), k); | ||
if (r.keep) | ||
coll.set(k, r.value); | ||
} | ||
} | ||
return coll; | ||
} | ||
sorted(compareFunction = _Collection.defaultSort) { | ||
return new this.constructor[Symbol.species](this).sort((av, bv, ak, bk) => compareFunction(av, bv, ak, bk)); | ||
} | ||
toJSON() { | ||
return [...this.values()]; | ||
} | ||
static defaultSort(firstValue, secondValue) { | ||
return Number(firstValue > secondValue) || Number(firstValue === secondValue) - 1; | ||
} | ||
static combineEntries(entries, combine) { | ||
const coll = new _Collection(); | ||
for (const [k, v] of entries) { | ||
if (coll.has(k)) { | ||
coll.set(k, combine(coll.get(k), v, k)); | ||
} else { | ||
coll.set(k, v); | ||
} | ||
} | ||
return coll; | ||
} | ||
}; | ||
var Collection = _Collection; | ||
__name(Collection, "Collection"); | ||
__publicField(Collection, "default", _Collection); | ||
var src_default = Collection; | ||
module.exports = __toCommonJS(src_exports); | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
Collection | ||
}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@discordjs/collection", | ||
"version": "0.6.0-dev.1648339700-520f471", | ||
"version": "0.6.0-dev.1648515283-ac4bc3a", | ||
"description": "Utility data structure used in discord.js", | ||
@@ -5,0 +5,0 @@ "scripts": { |
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
131829
1105