From 1facc0e4fee98df915d0ecd8f20ce56482ba6875 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Tue, 4 Oct 2011 15:56:26 -0400 Subject: [PATCH] merging in Tim Smart's gorgeous deep equality patch for _.isEqual --- underscore.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/underscore.js b/underscore.js index fbd8e423b..fcd3d09c9 100644 --- a/underscore.js +++ b/underscore.js @@ -631,22 +631,21 @@ if (isDateA || isDateB) return isDateA && isDateB && a.getTime() == b.getTime(); // Compare RegExps by their source patterns and flags. var isRegExpA = _.isRegExp(a), isRegExpB = _.isRegExp(b); - if (isRegExpA || isRegExpB) + if (isRegExpA || isRegExpB) { // Ensure commutative equality for RegExps. return isRegExpA && isRegExpB && a.source == b.source && a.global == b.global && a.multiline == b.multiline && a.ignoreCase == b.ignoreCase; + } // Ensure that both values are objects. if (typeA != 'object') return false; // Unwrap any wrapped objects. if (a._chain) a = a._wrapped; if (b._chain) b = b._wrapped; // Invoke a custom `isEqual` method if one is provided. - if (typeof a.isEqual == 'function') return a.isEqual(b); - // If only `b` provides an `isEqual` method, `a` and `b` are not equal. - if (typeof b.isEqual == 'function') return false; + if (_.isFunction(a.isEqual)) return a.isEqual(b); // Assume equality for cyclic structures. The algorithm for detecting cyclic structures is // adapted from ES 5.1 section 15.12.3, abstract operation `JO`. var length = stack.length; @@ -748,7 +747,7 @@ // Is a given value a boolean? _.isBoolean = function(obj) { - return obj === true || obj === false || typeof obj == 'object' && toString.call(obj) == '[object Boolean]'; + return obj === true || obj === false || toString.call(obj) == '[object Boolean]'; }; // Is a given value a date?