Make _.isEqual work with plain objects containing constructor properties with like object values and make _.invert assign arrays when using the multiValue flag. [closes #420]

This commit is contained in:
John-David Dalton
2014-01-09 19:41:55 -08:00
parent 60b7f504e6
commit 2ec5e948b4
9 changed files with 252 additions and 228 deletions

View File

@@ -676,15 +676,23 @@
if (className != objectClass) {
return false;
}
var ctorA = a.constructor,
ctorB = b.constructor;
var ownCtorA = hasOwnProperty.call(a, 'constructor'),
ownCtorB = hasOwnProperty.call(b, 'constructor');
if (ctorA != ctorB &&
!(isFunction(ctorA) && ctorA instanceof ctorA && isFunction(ctorB) && ctorB instanceof ctorB) &&
('constructor' in a && 'constructor' in b)
) {
if (ownCtorA !== ownCtorB) {
return false;
}
if (!ownCtorA) {
var ctorA = a.constructor,
ctorB = b.constructor;
if (ctorA != ctorB &&
!(isFunction(ctorA) && ctorA instanceof ctorA && isFunction(ctorB) && ctorB instanceof ctorB) &&
('constructor' in a && 'constructor' in b)
) {
return false;
}
}
}
stackA || (stackA = []);
stackB || (stackB = []);
@@ -3799,7 +3807,7 @@
*
* // with `multiValue`
* _.invert({ 'first': 'fred', 'second': 'barney', 'third': 'fred' }, true);
* // => { 'fred': ['first', 'third'], 'barney': 'second' }
* // => { 'fred': ['first', 'third'], 'barney': ['second'] }
*/
function invert(object, multiValue) {
var index = -1,
@@ -3811,11 +3819,12 @@
var key = props[index],
value = object[key];
if (multiValue && hasOwnProperty.call(result, value)) {
if (typeof result[value] == 'string') {
result[value] = [result[value]];
if (multiValue) {
if (hasOwnProperty.call(result, value)) {
result[value].push(key);
} else {
result[value] = [key];
}
result[value].push(key);
}
else {
result[value] = key;