Ensure _.contains works with a fromIndex for objects.

This commit is contained in:
John-David Dalton
2014-04-10 00:54:27 -07:00
parent a620c2ec1a
commit bd23666b45

View File

@@ -3645,31 +3645,34 @@
*/
function contains(collection, target, fromIndex) {
var length = collection ? collection.length : 0;
fromIndex = (typeof fromIndex == 'number' && fromIndex) || 0;
if (typeof length == 'number' && length > -1 && length <= maxSafeInteger) {
if (typeof collection == 'string' || !isArray(collection) && isString(collection)) {
if (fromIndex >= length) {
return false;
}
return nativeContains
? nativeContains.call(collection, target, fromIndex)
: collection.indexOf(target, fromIndex) > -1;
}
var indexOf = getIndexOf();
fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : fromIndex;
return indexOf(collection, target, fromIndex) > -1;
if (!(typeof length == 'number' && length > -1 && length <= maxSafeInteger)) {
var props = keys(collection);
length = props.length;
}
var index = -1,
result = false;
baseEach(collection, function(value) {
if (++index >= fromIndex) {
return !(result = value === target);
if (typeof fromIndex == 'number') {
fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : (fromIndex || 0);
} else {
fromIndex = 0;
}
if (props) {
while (fromIndex < length) {
var value = collection[props[fromIndex++]];
if (value === target) {
return true;
}
}
});
return result;
return false;
}
if (typeof collection == 'string' || !isArray(collection) && isString(collection)) {
if (fromIndex >= length) {
return false;
}
return nativeContains
? nativeContains.call(collection, target, fromIndex)
: collection.indexOf(target, fromIndex) > -1;
}
var indexOf = getIndexOf();
return indexOf(collection, target, fromIndex) > -1;
}
/**