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

@@ -1617,6 +1617,7 @@
// non `Object` object instances with different constructors are not equal
if (ctorA != ctorB &&
!(hasOwnProperty.call(a, 'constructor') && hasOwnProperty.call(b, 'constructor')) &&
!(isFunction(ctorA) && ctorA instanceof ctorA && isFunction(ctorB) && ctorB instanceof ctorB) &&
('constructor' in a && 'constructor' in b)
) {
@@ -5692,7 +5693,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,
@@ -5704,11 +5705,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;