]> git.r.bdr.sh - rbdr/dotfiles/blob
c4b2b42b100836946f1a0aee9b05825f390bc2ec
[rbdr/dotfiles] /
1 'use strict';
2
3 var eq = require('./eq')
4 , isPlainObject = require('./is-plain-object')
5 , value = require('./valid-value')
6
7 , isArray = Array.isArray, keys = Object.keys
8 , propertyIsEnumerable = Object.prototype.propertyIsEnumerable
9
10 , eqArr, eqVal, eqObj;
11
12 eqArr = function (a, b, recMap) {
13 var i, l = a.length;
14 if (l !== b.length) return false;
15 for (i = 0; i < l; ++i) {
16 if (a.hasOwnProperty(i) !== b.hasOwnProperty(i)) return false;
17 if (!eqVal(a[i], b[i], recMap)) return false;
18 }
19 return true;
20 };
21
22 eqObj = function (a, b, recMap) {
23 var k1 = keys(a), k2 = keys(b);
24 if (k1.length !== k2.length) return false;
25 return k1.every(function (key) {
26 if (!propertyIsEnumerable.call(b, key)) return false;
27 return eqVal(a[key], b[key], recMap);
28 });
29 };
30
31 eqVal = function (a, b, recMap) {
32 var i, eqX, c1, c2;
33 if (eq(a, b)) return true;
34 if (isPlainObject(a)) {
35 if (!isPlainObject(b)) return false;
36 eqX = eqObj;
37 } else if (isArray(a) && isArray(b)) {
38 eqX = eqArr;
39 } else {
40 return false;
41 }
42 c1 = recMap[0];
43 c2 = recMap[1];
44 i = c1.indexOf(a);
45 if (i !== -1) {
46 if (c2[i].indexOf(b) !== -1) return true;
47 } else {
48 i = c1.push(a) - 1;
49 c2[i] = [];
50 }
51 c2[i].push(b);
52 return eqX(a, b, recMap);
53 };
54
55 module.exports = function (a, b) {
56 if (eq(value(a), value(b))) return true;
57 return eqVal(Object(a), Object(b), [[], []]);
58 };