1 | var pSlice = Array.prototype.slice;
|
2 | var Object_keys = typeof Object.keys === 'function'
|
3 | ? Object.keys
|
4 | : function (obj) {
|
5 | var keys = [];
|
6 | for (var key in obj) keys.push(key);
|
7 | return keys;
|
8 | }
|
9 | ;
|
10 |
|
11 | var deepEqual = module.exports = function (actual, expected) {
|
12 |
|
13 | if (actual === 0 && expected === 0) {
|
14 | return areZerosEqual(actual, expected);
|
15 |
|
16 |
|
17 | } else if (actual === expected) {
|
18 | return true;
|
19 |
|
20 | } else if (actual instanceof Date && expected instanceof Date) {
|
21 | return actual.getTime() === expected.getTime();
|
22 |
|
23 | } else if (isNumberNaN(actual)) {
|
24 | return isNumberNaN(expected);
|
25 |
|
26 |
|
27 |
|
28 | } else if (typeof actual != 'object' && typeof expected != 'object') {
|
29 | return actual == expected;
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | } else {
|
38 | return objEquiv(actual, expected);
|
39 | }
|
40 | };
|
41 |
|
42 | function isUndefinedOrNull(value) {
|
43 | return value === null || value === undefined;
|
44 | }
|
45 |
|
46 | function isArguments(object) {
|
47 | return Object.prototype.toString.call(object) == '[object Arguments]';
|
48 | }
|
49 |
|
50 | function isNumberNaN(value) {
|
51 |
|
52 | return typeof value == 'number' && value !== value;
|
53 | }
|
54 |
|
55 | function areZerosEqual(zeroA, zeroB) {
|
56 |
|
57 | return (1 / zeroA) === (1 / zeroB);
|
58 | }
|
59 |
|
60 | function objEquiv(a, b) {
|
61 | if (isUndefinedOrNull(a) || isUndefinedOrNull(b))
|
62 | return false;
|
63 |
|
64 |
|
65 | if (a.prototype !== b.prototype) return false;
|
66 |
|
67 |
|
68 | if (isArguments(a)) {
|
69 | if (!isArguments(b)) {
|
70 | return false;
|
71 | }
|
72 | a = pSlice.call(a);
|
73 | b = pSlice.call(b);
|
74 | return deepEqual(a, b);
|
75 | }
|
76 | try {
|
77 | var ka = Object_keys(a),
|
78 | kb = Object_keys(b),
|
79 | key, i;
|
80 | } catch (e) {
|
81 | return false;
|
82 | }
|
83 |
|
84 |
|
85 | if (ka.length != kb.length)
|
86 | return false;
|
87 |
|
88 | ka.sort();
|
89 | kb.sort();
|
90 |
|
91 | for (i = ka.length - 1; i >= 0; i--) {
|
92 | if (ka[i] != kb[i])
|
93 | return false;
|
94 | }
|
95 |
|
96 |
|
97 | for (i = ka.length - 1; i >= 0; i--) {
|
98 | key = ka[i];
|
99 | if (!deepEqual(a[key], b[key])) return false;
|
100 | }
|
101 | return true;
|
102 | }
|