Skip to content

Commit

Permalink
Separate Object.is polyfill (facebook#14334)
Browse files Browse the repository at this point in the history
* Separate_Object_Is_Polyfill
  • Loading branch information
mmarkelov authored and n8schloss committed Jan 31, 2019
1 parent 15ffc5f commit bb3fb19
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 34 deletions.
10 changes: 2 additions & 8 deletions packages/react-reconciler/src/ReactFiberNewContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {ContextProvider, ClassComponent} from 'shared/ReactWorkTags';

import invariant from 'shared/invariant';
import warning from 'shared/warning';
import is from 'shared/objectIs';
import {
createUpdate,
enqueueUpdate,
Expand Down Expand Up @@ -104,14 +105,7 @@ export function calculateChangedBits<T>(
newValue: T,
oldValue: T,
) {
// Use Object.is to compare the new context value to the old value. Inlined
// Object.is polyfill.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
if (
(oldValue === newValue &&
(oldValue !== 0 || 1 / oldValue === 1 / (newValue: any))) ||
(oldValue !== oldValue && newValue !== newValue) // eslint-disable-line no-self-compare
) {
if (is(oldValue, newValue)) {
// No change
return 0;
} else {
Expand Down
10 changes: 2 additions & 8 deletions packages/shared/areHookInputsEqual.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import warning from 'shared/warning';
import is from './objectIs';

export default function areHookInputsEqual(arr1: any[], arr2: any[]) {
// Don't bother comparing lengths in prod because these arrays should be
Expand All @@ -24,14 +25,7 @@ export default function areHookInputsEqual(arr1: any[], arr2: any[]) {
);
}
for (let i = 0; i < arr1.length; i++) {
// Inlined Object.is polyfill.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
const val1 = arr1[i];
const val2 = arr2[i];
if (
(val1 === val2 && (val1 !== 0 || 1 / val1 === 1 / (val2: any))) ||
(val1 !== val1 && val2 !== val2) // eslint-disable-line no-self-compare
) {
if (is(arr1[i], arr2[i])) {
continue;
}
return false;
Expand Down
20 changes: 20 additions & 0 deletions packages/shared/objectIs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/

/**
* inlined Object.is polyfill to avoid requiring consumers ship their own
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
*/
function is(x: any, y: any) {
return (
(x === y && (x !== 0 || 1 / x === 1 / y)) || (x !== x && y !== y) // eslint-disable-line no-self-compare
);
}

export default is;
19 changes: 1 addition & 18 deletions packages/shared/shallowEqual.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,10 @@
* @flow
*/

/*eslint-disable no-self-compare */
import is from './objectIs';

const hasOwnProperty = Object.prototype.hasOwnProperty;

/**
* inlined Object.is polyfill to avoid requiring consumers ship their own
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
*/
function is(x, y) {
// SameValue algorithm
if (x === y) {
// Steps 1-5, 7-10
// Steps 6.b-6.e: +0 != -0
// Added the nonzero y check to make Flow happy, but it is redundant
return x !== 0 || y !== 0 || 1 / x === 1 / y;
} else {
// Step 6.a: NaN == NaN
return x !== x && y !== y;
}
}

/**
* Performs equality by iterating through keys on an object and returning false
* when any key has values which are not strictly equal between the arguments.
Expand Down

0 comments on commit bb3fb19

Please sign in to comment.