From bd23666b45976f8bdd059b427fdd0a21419a63de Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Thu, 10 Apr 2014 00:54:27 -0700 Subject: [PATCH] Ensure `_.contains` works with a `fromIndex` for objects. --- lodash.js | 49 ++++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/lodash.js b/lodash.js index 3f17973b6..755ab6141 100644 --- a/lodash.js +++ b/lodash.js @@ -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; } /**