_.isEqual: Move the internal eq method into the main closure. Remove strict type checking.

This commit is contained in:
Kit Goncharov
2011-07-12 20:22:05 -06:00
parent e21b346cbf
commit 9d0b43221a

View File

@@ -593,8 +593,7 @@
return obj;
};
// Perform a deep comparison to check if two objects are equal.
_.isEqual = (function() {
// Internal recursive comparison function.
function eq(a, b, stack) {
// Identical objects are equal.
if (a === b) return true;
@@ -603,13 +602,13 @@
// Compare object types.
var typeA = typeof a;
if (typeA != typeof b) return false;
// Compare functions by reference.
if (_.isFunction(a)) return _.isFunction(b) && a == b;
// Compare strings, numbers, dates, and booleans by value.
if (_.isString(a)) return _.isString(b) && String(a) == String(b);
if (_.isNumber(a)) return _.isNumber(b) && +a == +b;
// The type comparison above prevents unwanted type coercion.
if (a == b) return true;
// Ensure that both values are truthy or falsy.
if ((!a && b) || (a && !b)) return false;
// `NaN` values are toxic.
if (_.isNaN(a) || _.isNaN(b)) return false;
if (_.isDate(a)) return _.isDate(b) && a.getTime() == b.getTime();
if (_.isBoolean(a)) return _.isBoolean(b) && +a == +b;
// Compare RegExps by their source patterns and flags.
if (_.isRegExp(a)) return _.isRegExp(b) && a.source == b.source &&
a.global == b.global &&
@@ -646,11 +645,11 @@
stack.pop();
return result;
}
// Expose the recursive `isEqual` method.
return function(a, b) {
// Perform a deep comparison to check if two objects are equal.
_.isEqual = function(a, b) {
return eq(a, b, []);
};
})();
// Is a given array or object empty?
_.isEmpty = function(obj) {