UNPKG

2.7 kBJavaScriptView Raw
1var traverse = require('../../');
2
3module.exports = function (a, b) {
4 if (arguments.length !== 2) {
5 throw new Error(
6 'deepEqual requires exactly two objects to compare against'
7 );
8 }
9
10 var equal = true;
11 var node = b;
12
13 traverse(a).forEach(function (y) {
14 var notEqual = (function () {
15 equal = false;
16 //this.stop();
17 return undefined;
18 }).bind(this);
19
20 //if (node === undefined || node === null) return notEqual();
21
22 if (!this.isRoot) {
23 /*
24 if (!Object.hasOwnProperty.call(node, this.key)) {
25 return notEqual();
26 }
27 */
28 if (typeof node !== 'object') return notEqual();
29 node = node[this.key];
30 }
31
32 var x = node;
33
34 this.post(function () {
35 node = x;
36 });
37
38 var toS = function (o) {
39 return Object.prototype.toString.call(o);
40 };
41
42 if (this.circular) {
43 if (traverse(b).get(this.circular.path) !== x) notEqual();
44 }
45 else if (typeof x !== typeof y) {
46 notEqual();
47 }
48 else if (x === null || y === null || x === undefined || y === undefined) {
49 if (x !== y) notEqual();
50 }
51 else if (x.__proto__ !== y.__proto__) {
52 notEqual();
53 }
54 else if (x === y) {
55 // nop
56 }
57 else if (typeof x === 'function') {
58 if (x instanceof RegExp) {
59 // both regexps on account of the __proto__ check
60 if (x.toString() != y.toString()) notEqual();
61 }
62 else if (x !== y) notEqual();
63 }
64 else if (typeof x === 'object') {
65 if (toS(y) === '[object Arguments]'
66 || toS(x) === '[object Arguments]') {
67 if (toS(x) !== toS(y)) {
68 notEqual();
69 }
70 }
71 else if (x instanceof Date || y instanceof Date) {
72 if (!(x instanceof Date) || !(y instanceof Date)
73 || x.getTime() !== y.getTime()) {
74 notEqual();
75 }
76 }
77 else {
78 var kx = Object.keys(x);
79 var ky = Object.keys(y);
80 if (kx.length !== ky.length) return notEqual();
81 for (var i = 0; i < kx.length; i++) {
82 var k = kx[i];
83 if (!Object.hasOwnProperty.call(y, k)) {
84 notEqual();
85 }
86 }
87 }
88 }
89 });
90
91 return equal;
92};