Add unofficial _.where like support for methods like _.find. [closes #159]

Former-commit-id: c6106035af3f3d676cbd3f0a5c785b2c00ad1e9d
This commit is contained in:
John-David Dalton
2013-01-12 19:02:06 -08:00
parent 016391e442
commit 2d202e90b7
2 changed files with 37 additions and 32 deletions

View File

@@ -633,9 +633,23 @@
if (!func) {
return identity;
}
if (typeof func != 'function') {
var type = typeof func;
if (type != 'function') {
if (type != 'object') {
return function(object) {
return object[func];
};
}
var props = keys(func);
return function(object) {
return object[func];
var length = props.length;
while (length--) {
var result = object[props[length]] === func[props[length]];
if (!result) {
break;
}
}
return !!result;
};
}
if (typeof thisArg != 'undefined') {
@@ -2748,17 +2762,7 @@
* // => [{ 'name': 'moe', 'age': 40 }]
*/
function where(collection, properties) {
var props = keys(properties);
return filter(collection, function(object) {
var length = props.length;
while (length--) {
var result = object[props[length]] === properties[props[length]];
if (!result) {
break;
}
}
return !!result;
});
return filter(collection, properties);
}
/*--------------------------------------------------------------------------*/