Cleanup baseIsEqual and baseMerge.

This commit is contained in:
John-David Dalton
2014-05-13 08:38:43 -07:00
parent 347663716c
commit 13e00379ae

103
lodash.js
View File

@@ -1783,17 +1783,14 @@
break;
}
}
if (!result) {
break;
}
} else {
result = callback ? callback(value, other, key) : undefined;
if (!(result = typeof result == 'undefined'
? baseIsEqual(value[size], othValue, callback, isWhere, stackA, stackB)
: !!result
)) {
break;
}
result = typeof result == 'undefined'
? baseIsEqual(value[size], othValue, callback, isWhere, stackA, stackB)
: !!result;
}
if (!result) {
break;
}
}
}
@@ -1803,16 +1800,17 @@
// which, in this case, is more costly
baseForIn(other, function(othValue, key, other) {
if (hasOwnProperty.call(other, key)) {
result = false;
// count the number of properties
size++;
// deep compare each property value
if (!hasOwnProperty.call(value, key)) {
return (result = false);
if (hasOwnProperty.call(value, key)) {
result = callback ? callback(value, other, key) : undefined;
result = typeof result == 'undefined'
? baseIsEqual(value[key], othValue, callback, isWhere, stackA, stackB)
: !!result;
}
result = callback ? callback(value, other, key) : undefined;
return (result = typeof result == 'undefined'
? baseIsEqual(value[key], othValue, callback, isWhere, stackA, stackB)
: !!result);
return result;
}
});
@@ -1845,53 +1843,46 @@
*/
function baseMerge(object, source, callback, stackA, stackB) {
(isArray(source) ? arrayEach : baseForOwn)(source, function(srcValue, key, source) {
var found,
isArr,
result = srcValue,
var isArr = srcValue && isArray(srcValue),
isObj = srcValue && isPlainObject(srcValue),
value = object[key];
if (srcValue && ((isArr = isArray(srcValue)) || isPlainObject(srcValue))) {
// avoid merging previously merged cyclic sources
var stackLength = stackA.length;
while (stackLength--) {
if ((found = stackA[stackLength] == srcValue)) {
value = stackB[stackLength];
break;
}
}
if (!found) {
var isShallow;
if (callback) {
result = callback(value, srcValue, key, object, source);
if ((isShallow = typeof result != 'undefined')) {
value = result;
}
}
if (!isShallow) {
value = isArr
? (isArray(value) ? value : [])
: (isPlainObject(value) ? value : {});
}
// add `source` and associated `value` to the stack of traversed objects
stackA.push(srcValue);
stackB.push(value);
// recursively merge objects and arrays (susceptible to call stack limits)
if (!isShallow) {
baseMerge(value, srcValue, callback, stackA, stackB);
}
}
}
else {
if (callback) {
result = callback(value, srcValue, key, object, source);
if (typeof result == 'undefined') {
result = srcValue;
}
if (!(isArr || isObj)) {
result = callback ? callback(value, srcValue, key, object, source) : undefined;
if (typeof result == 'undefined') {
result = srcValue;
}
if (typeof result != 'undefined') {
value = result;
}
object[key] = value;
return;
}
// avoid merging previously merged cyclic sources
var length = stackA.length;
while (length--) {
if (stackA[length] == srcValue) {
object[key] = stackB[length];
return;
}
}
var result = callback ? callback(value, srcValue, key, object, source) : undefined,
isShallow = typeof result != 'undefined';
if (isShallow) {
value = result;
} else {
value = isArr
? (isArray(value) ? value : [])
: (isPlainObject(value) ? value : {});
}
// add `source` and associated `value` to the stack of traversed objects
stackA.push(srcValue);
stackB.push(value);
// recursively merge objects and arrays (susceptible to call stack limits)
if (!isShallow) {
baseMerge(value, srcValue, callback, stackA, stackB);
}
object[key] = value;
});