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