source: node_modules/traverse/test/lib/deep_equal.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

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