Skip to content

Commit

Permalink
Added polyfill for Object.is, fixes #77
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Jan 19, 2018
1 parent 737e556 commit cf0b1e1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,12 @@ export function verifyReturnValue(value) {
`Immer callback expects no return value. However ${typeof value} was returned`
)
}

export function is(x, y) {
// From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js
if (x === y) {
return x !== 0 || 1 / x === 1 / y
} else {
return x !== x && y !== y
}
}
7 changes: 4 additions & 3 deletions src/es5.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// @ts-check

import {
is,
isProxyable,
isProxy,
freeze,
Expand Down Expand Up @@ -46,7 +47,7 @@ function get(state, prop) {
function set(state, prop, value) {
assertUnfinished(state)
if (!state.modified) {
if (Object.is(source(state)[prop], value)) return
if (is(source(state)[prop], value)) return
markChanged(state)
prepareCopy(state)
}
Expand Down Expand Up @@ -189,7 +190,7 @@ export function produceEs5(baseState, producer) {

function shallowEqual(objA, objB) {
//From: https://github.com/facebook/fbjs/blob/c69904a511b900266935168223063dd8772dfc40/packages/fbjs/src/core/shallowEqual.js
if (Object.is(objA, objB)) return true
if (is(objA, objB)) return true
if (
typeof objA !== "object" ||
objA === null ||
Expand All @@ -204,7 +205,7 @@ function shallowEqual(objA, objB) {
for (let i = 0; i < keysA.length; i++) {
if (
!hasOwnProperty.call(objB, keysA[i]) ||
!Object.is(objA[keysA[i]], objB[keysA[i]])
!is(objA[keysA[i]], objB[keysA[i]])
) {
return false
}
Expand Down
3 changes: 2 additions & 1 deletion src/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// @ts-check

import {
is,
isProxyable,
isProxy,
freeze,
Expand Down Expand Up @@ -73,7 +74,7 @@ function get(state, prop) {
function set(state, prop, value) {
if (!state.modified) {
if (
(prop in state.base && Object.is(state.base[prop], value)) ||
(prop in state.base && is(state.base[prop], value)) ||
(prop in state.proxies && state.proxies[prop] === value)
)
return true
Expand Down

0 comments on commit cf0b1e1

Please sign in to comment.