Skip to content

Commit cddf4fb

Browse files
authored
Account for NaN === NaN evaluating to false
1 parent bda92c6 commit cddf4fb

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
//
22

3+
function defaultCompare(a, b) {
4+
// `NaN === NaN` returns `false`
5+
// see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN#Polyfill
6+
return a === b || (typeof a === 'number' && a !== a && typeof b === 'number' && b !== b);
7+
}
8+
39
module.exports = function shallowEqual(objA, objB, compare, compareContext) {
410
var ret = compare ? compare.call(compareContext, objA, objB) : void 0;
511

612
if (ret !== void 0) {
713
return !!ret;
814
}
915

10-
if (objA === objB) {
16+
if (defaultCompare(objA, objB)) {
1117
return true;
1218
}
1319

@@ -37,7 +43,7 @@ module.exports = function shallowEqual(objA, objB, compare, compareContext) {
3743

3844
ret = compare ? compare.call(compareContext, valueA, valueB, key) : void 0;
3945

40-
if (ret === false || (ret === void 0 && valueA !== valueB)) {
46+
if (ret === false || (ret === void 0 && !defaultCompare(valueA, valueB))) {
4147
return false;
4248
}
4349
}

0 commit comments

Comments
 (0)