'use strict';
type Options = {+unsafelyIgnoreFunctions?: boolean};
function deepDiffer(
one: any,
two: any,
maxDepthOrOptions: Options | number = -1,
maybeOptions?: Options,
): boolean {
const options =
typeof maxDepthOrOptions === 'number' ? maybeOptions : maxDepthOrOptions;
const maxDepth =
typeof maxDepthOrOptions === 'number' ? maxDepthOrOptions : -1;
if (maxDepth === 0) {
return true;
}
if (one === two) {
return false;
}
if (typeof one === 'function' && typeof two === 'function') {
let unsafelyIgnoreFunctions =
options == null ? null : options.unsafelyIgnoreFunctions;
if (unsafelyIgnoreFunctions == null) {
unsafelyIgnoreFunctions = true;
}
return !unsafelyIgnoreFunctions;
}
if (typeof one !== 'object' || one === null) {
return one !== two;
}
if (typeof two !== 'object' || two === null) {
return true;
}
if (one.constructor !== two.constructor) {
return true;
}
if (Array.isArray(one)) {
const len = one.length;
if (two.length !== len) {
return true;
}
for (let ii = 0; ii < len; ii++) {
if (deepDiffer(one[ii], two[ii], maxDepth - 1, options)) {
return true;
}
}
} else {
for (const key in one) {
if (deepDiffer(one[key], two[key], maxDepth - 1, options)) {
return true;
}
}
for (const twoKey in two) {
if (one[twoKey] === undefined && two[twoKey] !== undefined) {
return true;
}
}
}
return false;
}
module.exports = deepDiffer;