49 lines
1.2 KiB
JavaScript
49 lines
1.2 KiB
JavaScript
/**
|
|
* Copyright 2004-present Facebook. All Rights Reserved.
|
|
*
|
|
* @providesModule deepDiffer
|
|
* @flow
|
|
*/
|
|
'use strict';
|
|
|
|
/*
|
|
* @returns {bool} true if different, false if equal
|
|
*/
|
|
var deepDiffer = function(one: any, two: any): bool {
|
|
if (one === two) {
|
|
// Short circuit on identical object references instead of traversing them.
|
|
return false;
|
|
}
|
|
if ((typeof one === 'function') && (typeof two === 'function')) {
|
|
// We consider all functions equal
|
|
return false;
|
|
}
|
|
if ((typeof one !== 'object') || (one === null)) {
|
|
// Primitives can be directly compared
|
|
return one !== two;
|
|
}
|
|
if ((typeof two !== 'object') || (two === null)) {
|
|
// We know they are different because the previous case would have triggered
|
|
// otherwise.
|
|
return true;
|
|
}
|
|
if (one.constructor !== two.constructor) {
|
|
return true;
|
|
}
|
|
for (var key in one) {
|
|
if (deepDiffer(one[key], two[key])) {
|
|
return true;
|
|
}
|
|
}
|
|
for (var twoKey in two) {
|
|
// The only case we haven't checked yet is keys that are in two but aren't
|
|
// in one, which means they are different.
|
|
if (one[twoKey] === undefined && two[twoKey] !== undefined) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
};
|
|
|
|
module.exports = deepDiffer;
|