react / react-0.13.3 / examples / basic-commonjs / node_modules / browserify / node_modules / deep-equal / index.js
80708 viewsvar pSlice = Array.prototype.slice;1var objectKeys = require('./lib/keys.js');2var isArguments = require('./lib/is_arguments.js');34var deepEqual = module.exports = function (actual, expected, opts) {5if (!opts) opts = {};6// 7.1. All identical values are equivalent, as determined by ===.7if (actual === expected) {8return true;910} else if (actual instanceof Date && expected instanceof Date) {11return actual.getTime() === expected.getTime();1213// 7.3. Other pairs that do not both pass typeof value == 'object',14// equivalence is determined by ==.15} else if (typeof actual != 'object' && typeof expected != 'object') {16return opts.strict ? actual === expected : actual == expected;1718// 7.4. For all other Object pairs, including Array objects, equivalence is19// determined by having the same number of owned properties (as verified20// with Object.prototype.hasOwnProperty.call), the same set of keys21// (although not necessarily the same order), equivalent values for every22// corresponding key, and an identical 'prototype' property. Note: this23// accounts for both named and indexed properties on Arrays.24} else {25return objEquiv(actual, expected, opts);26}27}2829function isUndefinedOrNull(value) {30return value === null || value === undefined;31}3233function isBuffer (x) {34if (!x || typeof x !== 'object' || typeof x.length !== 'number') return false;35if (typeof x.copy !== 'function' || typeof x.slice !== 'function') {36return false;37}38if (x.length > 0 && typeof x[0] !== 'number') return false;39return true;40}4142function objEquiv(a, b, opts) {43var i, key;44if (isUndefinedOrNull(a) || isUndefinedOrNull(b))45return false;46// an identical 'prototype' property.47if (a.prototype !== b.prototype) return false;48//~~~I've managed to break Object.keys through screwy arguments passing.49// Converting to array solves the problem.50if (isArguments(a)) {51if (!isArguments(b)) {52return false;53}54a = pSlice.call(a);55b = pSlice.call(b);56return deepEqual(a, b, opts);57}58if (isBuffer(a)) {59if (!isBuffer(b)) {60return false;61}62if (a.length !== b.length) return false;63for (i = 0; i < a.length; i++) {64if (a[i] !== b[i]) return false;65}66return true;67}68try {69var ka = objectKeys(a),70kb = objectKeys(b);71} catch (e) {//happens when one is a string literal and the other isn't72return false;73}74// having the same number of owned properties (keys incorporates75// hasOwnProperty)76if (ka.length != kb.length)77return false;78//the same set of keys (although not necessarily the same order),79ka.sort();80kb.sort();81//~~~cheap key test82for (i = ka.length - 1; i >= 0; i--) {83if (ka[i] != kb[i])84return false;85}86//equivalent values for every corresponding key, and87//~~~possibly expensive deep test88for (i = ka.length - 1; i >= 0; i--) {89key = ka[i];90if (!deepEqual(a[key], b[key], opts)) return false;91}92return typeof a === typeof b;93}949596