Support for regex/function in get/getAll

This commit is contained in:
abalabahaha
2016-01-31 22:30:02 -08:00
parent 6772222955
commit 580f260933
2 changed files with 83 additions and 15 deletions

View File

@@ -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;
};

View File

@@ -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;
}