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) { function contains(collection, target, fromIndex) {
var length = collection ? collection.length : 0; var length = collection ? collection.length : 0;
fromIndex = (typeof fromIndex == 'number' && fromIndex) || 0; if (!(typeof length == 'number' && length > -1 && length <= maxSafeInteger)) {
var props = keys(collection);
if (typeof length == 'number' && length > -1 && length <= maxSafeInteger) { length = props.length;
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;
} }
var index = -1, if (typeof fromIndex == 'number') {
result = false; fromIndex = fromIndex < 0 ? nativeMax(length + fromIndex, 0) : (fromIndex || 0);
} else {
baseEach(collection, function(value) { fromIndex = 0;
if (++index >= fromIndex) { }
return !(result = value === target); if (props) {
while (fromIndex < length) {
var value = collection[props[fromIndex++]];
if (value === target) {
return true;
}
} }
}); return false;
}
return result; 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;
} }
/** /**