mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 20:43:30 +01:00
Support for regex/function in get/getAll
This commit is contained in:
@@ -21,7 +21,21 @@ var Cache = (function (_Array) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Cache.prototype.get = function get(key, value) {
|
Cache.prototype.get = function get(key, value) {
|
||||||
if (key === this[discrimS]) return this[discrimCacheS][value] || null;
|
if (key === this[discrimS] && typeof value === "string") {
|
||||||
|
return this[discrimCacheS][value] || null;
|
||||||
|
}
|
||||||
|
|
||||||
|
var valid = value;
|
||||||
|
|
||||||
|
if (value.constructor.name === 'RegExp') {
|
||||||
|
valid = function valid(item) {
|
||||||
|
return value.test(item);
|
||||||
|
};
|
||||||
|
} else if (typeof value !== 'function') {
|
||||||
|
valid = function valid(item) {
|
||||||
|
return item == value;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
for (var _iterator = this, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
|
for (var _iterator = this, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
|
||||||
var _ref;
|
var _ref;
|
||||||
@@ -37,10 +51,11 @@ var Cache = (function (_Array) {
|
|||||||
|
|
||||||
var item = _ref;
|
var item = _ref;
|
||||||
|
|
||||||
if (item[key] == value) {
|
if (valid(item[key])) {
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -50,12 +65,38 @@ var Cache = (function (_Array) {
|
|||||||
|
|
||||||
Cache.prototype.getAll = function getAll(key, value) {
|
Cache.prototype.getAll = function getAll(key, value) {
|
||||||
var found = new Cache(this[discrimS]);
|
var found = new Cache(this[discrimS]);
|
||||||
this.forEach(function (val, index, array) {
|
|
||||||
if (val.hasOwnProperty(key) && val[key] == value) {
|
var valid = value;
|
||||||
found.push(val);
|
|
||||||
return;
|
if (value.constructor.name === 'RegExp') {
|
||||||
|
valid = function valid(item) {
|
||||||
|
return value.test(item);
|
||||||
|
};
|
||||||
|
} else if (typeof value !== 'function') {
|
||||||
|
valid = function valid(item) {
|
||||||
|
return item == value;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var _iterator2 = this, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
|
||||||
|
var _ref2;
|
||||||
|
|
||||||
|
if (_isArray2) {
|
||||||
|
if (_i2 >= _iterator2.length) break;
|
||||||
|
_ref2 = _iterator2[_i2++];
|
||||||
|
} else {
|
||||||
|
_i2 = _iterator2.next();
|
||||||
|
if (_i2.done) break;
|
||||||
|
_ref2 = _i2.value;
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
var item = _ref2;
|
||||||
|
|
||||||
|
if (valid(item[key])) {
|
||||||
|
found.add(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return found;
|
return found;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -11,14 +11,28 @@ export default class Cache extends Array {
|
|||||||
}
|
}
|
||||||
|
|
||||||
get(key, value) {
|
get(key, value) {
|
||||||
if (key === this[discrimS])
|
if (key === this[discrimS] && typeof value === "string") {
|
||||||
return this[discrimCacheS][value] || null;
|
return this[discrimCacheS][value] || null;
|
||||||
|
}
|
||||||
|
|
||||||
for(var item of this){
|
var valid = value;
|
||||||
if(item[key] == value){
|
|
||||||
|
if (value.constructor.name === 'RegExp') {
|
||||||
|
valid = function valid(item) {
|
||||||
|
return value.test(item);
|
||||||
|
}
|
||||||
|
} else if (typeof value !== 'function') {
|
||||||
|
valid = function valid(item) {
|
||||||
|
return item == value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var item of this) {
|
||||||
|
if (valid(item[key])) {
|
||||||
return item;
|
return item;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,12 +42,25 @@ export default class Cache extends Array {
|
|||||||
|
|
||||||
getAll(key, value) {
|
getAll(key, value) {
|
||||||
var found = new Cache(this[discrimS]);
|
var found = new Cache(this[discrimS]);
|
||||||
this.forEach((val, index, array) => {
|
|
||||||
if (val.hasOwnProperty(key) && val[key] == value) {
|
var valid = value;
|
||||||
found.push(val);
|
|
||||||
return;
|
if (value.constructor.name === 'RegExp') {
|
||||||
|
valid = function valid(item) {
|
||||||
|
return value.test(item);
|
||||||
}
|
}
|
||||||
});
|
} else if (typeof value !== 'function') {
|
||||||
|
valid = function valid(item) {
|
||||||
|
return item == value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var item of this) {
|
||||||
|
if (valid(item[key])) {
|
||||||
|
found.add(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return found;
|
return found;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user