Comparing version 2.0.0 to 2.0.1
90
index.js
@@ -62,3 +62,4 @@ 'use strict'; | ||
function memoizeCompare(leftHandOperand, rightHandOperand, memoizeMap) { | ||
if (!memoizeMap) { | ||
// Technically, WeakMap keys can *only* be objects, not primitives. | ||
if (!memoizeMap || isPrimitive(leftHandOperand) || isPrimitive(rightHandOperand)) { | ||
return null; | ||
@@ -85,3 +86,4 @@ } | ||
function memoizeSet(leftHandOperand, rightHandOperand, memoizeMap, result) { | ||
if (!memoizeMap || typeof leftHandOperand !== 'object' || typeof rightHandOperand !== 'object') { | ||
// Technically, WeakMap keys can *only* be objects, not primitives. | ||
if (!memoizeMap || isPrimitive(leftHandOperand) || isPrimitive(rightHandOperand)) { | ||
return; | ||
@@ -119,2 +121,23 @@ } | ||
function deepEqual(leftHandOperand, rightHandOperand, options) { | ||
// If we have a comparator, we can't assume anything; so bail to its check first. | ||
if (options && options.comparator) { | ||
return extensiveDeepEqual(leftHandOperand, rightHandOperand, options); | ||
} | ||
var simpleResult = simpleEqual(leftHandOperand, rightHandOperand); | ||
if (simpleResult !== null) { | ||
return simpleResult; | ||
} | ||
// Deeper comparisons are pushed through to a larger function | ||
return extensiveDeepEqual(leftHandOperand, rightHandOperand, options); | ||
} | ||
/** | ||
* Many comparisons can be canceled out early via simple equality or primitive checks. | ||
* @param {Mixed} leftHandOperand | ||
* @param {Mixed} rightHandOperand | ||
* @return {Boolean|null} equal match | ||
*/ | ||
function simpleEqual(leftHandOperand, rightHandOperand) { | ||
// Equal references (except for Numbers) can be returned early | ||
@@ -134,13 +157,9 @@ if (leftHandOperand === rightHandOperand) { | ||
// Primitives/null/undefined can be checked for referential equality; returning early | ||
if ( | ||
typeof leftHandOperand !== 'object' || | ||
leftHandOperand === null || | ||
leftHandOperand === undefined // eslint-disable-line no-undefined | ||
) { | ||
return leftHandOperand === rightHandOperand; | ||
// Anything that is not an 'object', i.e. symbols, functions, booleans, numbers, | ||
// strings, and undefined, can be compared by reference. | ||
if (isPrimitive(leftHandOperand) || isPrimitive(rightHandOperand)) { | ||
// Easy out b/c it would have passed the first equality check | ||
return false; | ||
} | ||
// Deeper comparisons are pushed through to a larger function | ||
return extensiveDeepEqual(leftHandOperand, rightHandOperand, options); | ||
return null; | ||
} | ||
@@ -165,12 +184,27 @@ | ||
var memoizeResult = memoizeCompare(leftHandOperand, rightHandOperand, options.memoize); | ||
if (memoizeResult !== null) { | ||
return memoizeResult; | ||
// Check if a memoized result exists. | ||
var memoizeResultLeft = memoizeCompare(leftHandOperand, rightHandOperand, options.memoize); | ||
if (memoizeResultLeft !== null) { | ||
return memoizeResultLeft; | ||
} | ||
var memoizeResultRight = memoizeCompare(rightHandOperand, leftHandOperand, options.memoize); | ||
if (memoizeResultRight !== null) { | ||
return memoizeResultRight; | ||
} | ||
var comparatorResult = comparator && comparator(leftHandOperand, rightHandOperand); | ||
if (comparatorResult === false || comparatorResult === true) { | ||
memoizeSet(leftHandOperand, rightHandOperand, options.memoize, comparatorResult); | ||
memoizeSet(rightHandOperand, leftHandOperand, options.memoize, comparatorResult); | ||
return comparatorResult; | ||
// If a comparator is present, use it. | ||
if (comparator) { | ||
var comparatorResult = comparator(leftHandOperand, rightHandOperand); | ||
// Comparators may return null, in which case we want to go back to default behavior. | ||
if (comparatorResult === false || comparatorResult === true) { | ||
memoizeSet(leftHandOperand, rightHandOperand, options.memoize, comparatorResult); | ||
return comparatorResult; | ||
} | ||
// To allow comparators to override *any* behavior, we ran them first. Since it didn't decide | ||
// what to do, we need to make sure to return the basic tests first before we move on. | ||
var simpleResult = simpleEqual(leftHandOperand, rightHandOperand); | ||
if (simpleResult !== null) { | ||
// Don't memoize this, it takes longer to set/retrieve than to just compare. | ||
return simpleResult; | ||
} | ||
} | ||
@@ -181,3 +215,2 @@ | ||
memoizeSet(leftHandOperand, rightHandOperand, options.memoize, false); | ||
memoizeSet(rightHandOperand, leftHandOperand, options.memoize, false); | ||
return false; | ||
@@ -188,7 +221,5 @@ } | ||
memoizeSet(leftHandOperand, rightHandOperand, options.memoize, true); | ||
memoizeSet(rightHandOperand, leftHandOperand, options.memoize, true); | ||
var result = extensiveDeepEqualByType(leftHandOperand, rightHandOperand, leftHandType, options); | ||
memoizeSet(leftHandOperand, rightHandOperand, options.memoize, result); | ||
memoizeSet(rightHandOperand, leftHandOperand, options.memoize, result); | ||
return result; | ||
@@ -443,1 +474,14 @@ } | ||
} | ||
/*! | ||
* Returns true if the argument is a primitive. | ||
* | ||
* This intentionally returns true for all objects that can be compared by reference, | ||
* including functions and symbols. | ||
* | ||
* @param {Mixed} value | ||
* @return {Boolean} result | ||
*/ | ||
function isPrimitive(value) { | ||
return value === null || typeof value !== 'object'; | ||
} |
@@ -88,3 +88,3 @@ { | ||
}, | ||
"version": "2.0.0" | ||
"version": "2.0.1" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23596
440