Fix callback use with _.isEqual.

This commit is contained in:
John-David Dalton
2014-05-21 00:18:33 -07:00
parent 96b525658e
commit 4345f353a0

View File

@@ -1696,7 +1696,7 @@
* @returns {boolean} Returns `true` if the values are equivalent, else `false`. * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
*/ */
function baseIsEqual(value, other, callback, isWhere, stackA, stackB) { function baseIsEqual(value, other, callback, isWhere, stackA, stackB) {
var result = callback ? callback(value, other) : undefined; var result = callback && !stackA ? callback(value, other) : undefined;
if (typeof result != 'undefined') { if (typeof result != 'undefined') {
return !!result; return !!result;
} }
@@ -1796,7 +1796,6 @@
return stackB[length] == other; return stackB[length] == other;
} }
} }
var size = 0;
result = true; result = true;
// add `value` and `other` to the stack of traversed objects // add `value` and `other` to the stack of traversed objects
@@ -1806,27 +1805,30 @@
// recursively compare objects and arrays (susceptible to call stack limits) // recursively compare objects and arrays (susceptible to call stack limits)
if (isArr) { if (isArr) {
// compare lengths to determine if a deep comparison is necessary // compare lengths to determine if a deep comparison is necessary
var othLength = other.length;
length = value.length; length = value.length;
size = other.length; result = othLength == length;
result = size == length;
if (result || isWhere) { if (result || isWhere) {
var othIndex = -1;
// deep compare the contents, ignoring non-numeric properties // deep compare the contents, ignoring non-numeric properties
while (size--) { while (++othIndex < othLength) {
var index = length, var othValue = other[othIndex];
othValue = other[size];
if (isWhere) { if (isWhere) {
while (index--) { var index = -1;
while (++index < length) {
result = baseIsEqual(value[index], othValue, callback, isWhere, stackA, stackB); result = baseIsEqual(value[index], othValue, callback, isWhere, stackA, stackB);
if (result) { if (result) {
break; break;
} }
} }
} else { } else {
result = callback ? callback(value, other, key) : undefined; var valValue = value[othIndex];
result = callback ? callback(valValue, othValue, othIndex) : undefined;
result = typeof result == 'undefined' result = typeof result == 'undefined'
? baseIsEqual(value[size], othValue, callback, isWhere, stackA, stackB) ? baseIsEqual(valValue, othValue, callback, isWhere, stackA, stackB)
: !!result; : !!result;
} }
if (!result) { if (!result) {
@@ -1836,6 +1838,8 @@
} }
} }
else { else {
var size = 0;
// deep compare objects using `forIn`, instead of `forOwn`, to avoid `Object.keys` // deep compare objects using `forIn`, instead of `forOwn`, to avoid `Object.keys`
// which, in this case, is more costly // which, in this case, is more costly
baseForIn(other, function(othValue, key, other) { baseForIn(other, function(othValue, key, other) {
@@ -1845,9 +1849,10 @@
size++; size++;
// deep compare each property value // deep compare each property value
if (hasOwnProperty.call(value, key)) { if (hasOwnProperty.call(value, key)) {
result = callback ? callback(value, other, key) : undefined; var valValue = value[key];
result = callback ? callback(valValue, othValue, key) : undefined;
result = typeof result == 'undefined' result = typeof result == 'undefined'
? baseIsEqual(value[key], othValue, callback, isWhere, stackA, stackB) ? baseIsEqual(valValue, othValue, callback, isWhere, stackA, stackB)
: !!result; : !!result;
} }
return result; return result;