mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-15 11:03:30 +01:00
More rewrites
This commit is contained in:
51
src/Util/Cache.js
Normal file
51
src/Util/Cache.js
Normal file
@@ -0,0 +1,51 @@
|
||||
"use strict";
|
||||
|
||||
class Cache extends Array{
|
||||
constructor(discrim, limit){
|
||||
super();
|
||||
this.discrim = discrim || "id";
|
||||
}
|
||||
|
||||
get(key, value){
|
||||
var found = null;
|
||||
this.forEach( (val, index, array) => {
|
||||
if(val.hasOwnProperty(key) && val[key] == value){
|
||||
found = val;
|
||||
return;
|
||||
}
|
||||
} );
|
||||
return found;
|
||||
}
|
||||
|
||||
getAll(key, value){
|
||||
var found = [];
|
||||
this.forEach( (val, index, array) => {
|
||||
if(val.hasOwnProperty(key) && val[key] == value){
|
||||
found.push(val);
|
||||
return;
|
||||
}
|
||||
} );
|
||||
return found;
|
||||
}
|
||||
|
||||
add(data){
|
||||
var exit = false;
|
||||
for(var item of this){
|
||||
if(item[this.discrim] === data[this.discrim]){
|
||||
exit = item;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(exit){
|
||||
return exit;
|
||||
}else{
|
||||
if(this.limit && this.length >= this.limit){
|
||||
this.splice(0, 1);
|
||||
}
|
||||
this.push(data);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Cache;
|
||||
29
src/Util/Equality.js
Normal file
29
src/Util/Equality.js
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
The Equality Class is just used to show
|
||||
that a Class has an ID that can be used to
|
||||
check for equality.
|
||||
|
||||
Never use == or === when comparing
|
||||
objects in discord.js, they will be different
|
||||
instances sometimes.
|
||||
|
||||
Instead, use objectThatExtendsEquality.equals()
|
||||
*/
|
||||
class Equality{
|
||||
constructor(){
|
||||
|
||||
}
|
||||
|
||||
get eqDiscriminator(){
|
||||
return "id";
|
||||
}
|
||||
|
||||
equals(object){
|
||||
if(object && object[this.eqDiscriminator] == this[this.eqDiscriminator]){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Equality;
|
||||
Reference in New Issue
Block a user