Allow get/getAll to take a function only

This commit is contained in:
abalabahaha
2016-01-31 22:37:31 -08:00
parent 580f260933
commit 6f7bd987b8
2 changed files with 30 additions and 30 deletions

View File

@@ -21,18 +21,17 @@ var Cache = (function (_Array) {
} }
Cache.prototype.get = function get(key, value) { Cache.prototype.get = function get(key, value) {
if (key === this[discrimS] && typeof value === "string") { if (typeof key === 'function') {
var valid = key;
key = null;
} else if (key === this[discrimS] && typeof value === "string") {
return this[discrimCacheS][value] || null; return this[discrimCacheS][value] || null;
} } else if (value.constructor.name === 'RegExp') {
var valid = function valid(item) {
var valid = value;
if (value.constructor.name === 'RegExp') {
valid = function valid(item) {
return value.test(item); return value.test(item);
}; };
} else if (typeof value !== 'function') { } else if (typeof value !== 'function') {
valid = function valid(item) { var valid = function valid(item) {
return item == value; return item == value;
}; };
} }
@@ -51,7 +50,7 @@ var Cache = (function (_Array) {
var item = _ref; var item = _ref;
if (valid(item[key])) { if (valid(key == null ? item : item[key])) {
return item; return item;
} }
} }
@@ -66,14 +65,15 @@ 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]);
var valid = value; if (typeof key === 'function') {
var valid = key;
if (value.constructor.name === 'RegExp') { key = null;
valid = function valid(item) { } else if (value.constructor.name === 'RegExp') {
var valid = function valid(item) {
return value.test(item); return value.test(item);
}; };
} else if (typeof value !== 'function') { } else if (typeof value !== 'function') {
valid = function valid(item) { var valid = function valid(item) {
return item == value; return item == value;
}; };
} }
@@ -92,7 +92,7 @@ var Cache = (function (_Array) {
var item = _ref2; var item = _ref2;
if (valid(item[key])) { if (valid(key == null ? item : item[key])) {
found.add(item); found.add(item);
} }
} }

View File

@@ -11,24 +11,23 @@ export default class Cache extends Array {
} }
get(key, value) { get(key, value) {
if (key === this[discrimS] && typeof value === "string") { if (typeof key === 'function') {
var valid = key;
key = null;
} else if (key === this[discrimS] && typeof value === "string") {
return this[discrimCacheS][value] || null; return this[discrimCacheS][value] || null;
} } else if (value.constructor.name === 'RegExp') {
var valid = function valid(item) {
var valid = value;
if (value.constructor.name === 'RegExp') {
valid = function valid(item) {
return value.test(item); return value.test(item);
} }
} else if (typeof value !== 'function') { } else if (typeof value !== 'function') {
valid = function valid(item) { var valid = function valid(item) {
return item == value; return item == value;
} }
} }
for (var item of this) { for (var item of this) {
if (valid(item[key])) { if (valid(key == null ? item : item[key])) {
return item; return item;
} }
} }
@@ -43,20 +42,21 @@ export default class Cache extends Array {
getAll(key, value) { getAll(key, value) {
var found = new Cache(this[discrimS]); var found = new Cache(this[discrimS]);
var valid = value; if (typeof key === 'function') {
var valid = key;
if (value.constructor.name === 'RegExp') { key = null;
valid = function valid(item) { } else if (value.constructor.name === 'RegExp') {
var valid = function valid(item) {
return value.test(item); return value.test(item);
} }
} else if (typeof value !== 'function') { } else if (typeof value !== 'function') {
valid = function valid(item) { var valid = function valid(item) {
return item == value; return item == value;
} }
} }
for (var item of this) { for (var item of this) {
if (valid(item[key])) { if (valid(key == null ? item : item[key])) {
found.add(item); found.add(item);
} }
} }