diff --git a/lib/Util/Cache.js b/lib/Util/Cache.js index 184d498e8..9d71c0a7f 100644 --- a/lib/Util/Cache.js +++ b/lib/Util/Cache.js @@ -21,7 +21,21 @@ var Cache = (function (_Array) { } 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]();;) { var _ref; @@ -37,10 +51,11 @@ var Cache = (function (_Array) { var item = _ref; - if (item[key] == value) { + if (valid(item[key])) { return item; } } + return null; }; @@ -50,12 +65,38 @@ var Cache = (function (_Array) { Cache.prototype.getAll = function getAll(key, value) { var found = new Cache(this[discrimS]); - this.forEach(function (val, index, array) { - if (val.hasOwnProperty(key) && val[key] == value) { - found.push(val); - return; + + 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 _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; }; diff --git a/src/Util/Cache.js b/src/Util/Cache.js index 7fc70aec1..843520533 100644 --- a/src/Util/Cache.js +++ b/src/Util/Cache.js @@ -11,14 +11,28 @@ export default class Cache extends Array { } get(key, value) { - if (key === this[discrimS]) + if (key === this[discrimS] && typeof value === "string") { return this[discrimCacheS][value] || null; + } - for(var item of this){ - if(item[key] == value){ + 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 item of this) { + if (valid(item[key])) { return item; } } + return null; } @@ -28,12 +42,25 @@ export default class Cache extends Array { getAll(key, value) { var found = new Cache(this[discrimS]); - this.forEach((val, index, array) => { - if (val.hasOwnProperty(key) && val[key] == value) { - found.push(val); - return; + + 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 item of this) { + if (valid(item[key])) { + found.add(item); + } + } + return found; }