2019-01-17 18:22:05 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var isArray = Array.isArray;
|
|
|
|
var keyList = Object.keys;
|
|
|
|
var hasProp = Object.prototype.hasOwnProperty;
|
|
|
|
|
|
|
|
module.exports = function equal(a, b) {
|
|
|
|
if (a === b) return true;
|
|
|
|
|
2019-03-08 18:20:59 +00:00
|
|
|
if (a && b && typeof a == 'object' && typeof b == 'object') {
|
|
|
|
var arrA = isArray(a)
|
|
|
|
, arrB = isArray(b)
|
|
|
|
, i
|
|
|
|
, length
|
|
|
|
, key;
|
|
|
|
|
|
|
|
if (arrA && arrB) {
|
|
|
|
length = a.length;
|
|
|
|
if (length != b.length) return false;
|
|
|
|
for (i = length; i-- !== 0;)
|
|
|
|
if (!equal(a[i], b[i])) return false;
|
|
|
|
return true;
|
|
|
|
}
|
2019-01-17 18:22:05 +00:00
|
|
|
|
2019-03-08 18:20:59 +00:00
|
|
|
if (arrA != arrB) return false;
|
2019-01-17 18:22:05 +00:00
|
|
|
|
2019-03-08 18:20:59 +00:00
|
|
|
var dateA = a instanceof Date
|
|
|
|
, dateB = b instanceof Date;
|
|
|
|
if (dateA != dateB) return false;
|
|
|
|
if (dateA && dateB) return a.getTime() == b.getTime();
|
2019-01-17 18:22:05 +00:00
|
|
|
|
2019-03-08 18:20:59 +00:00
|
|
|
var regexpA = a instanceof RegExp
|
|
|
|
, regexpB = b instanceof RegExp;
|
|
|
|
if (regexpA != regexpB) return false;
|
|
|
|
if (regexpA && regexpB) return a.toString() == b.toString();
|
2019-01-17 18:22:05 +00:00
|
|
|
|
|
|
|
var keys = keyList(a);
|
|
|
|
length = keys.length;
|
|
|
|
|
|
|
|
if (length !== keyList(b).length)
|
|
|
|
return false;
|
|
|
|
|
2019-03-08 18:20:59 +00:00
|
|
|
for (i = length; i-- !== 0;)
|
2019-01-17 18:22:05 +00:00
|
|
|
if (!hasProp.call(b, keys[i])) return false;
|
|
|
|
|
2019-03-08 18:20:59 +00:00
|
|
|
for (i = length; i-- !== 0;) {
|
2019-01-17 18:22:05 +00:00
|
|
|
key = keys[i];
|
|
|
|
if (!equal(a[key], b[key])) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-08 18:20:59 +00:00
|
|
|
return a!==a && b!==b;
|
2019-01-17 18:22:05 +00:00
|
|
|
};
|