Skip to content

Commit

Permalink
fix: special case NaN in setter (#912)
Browse files Browse the repository at this point in the history
* fix: special case NaN in setter

* fix: naN special case

Co-authored-by: zhangpeifeng <zhangpeifeng.zpf@alibaba-inc.com>
  • Loading branch information
Rayzhangzhang and zhangpeifeng authored Oct 22, 2022
1 parent 8aac6ca commit 5721bb7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
17 changes: 17 additions & 0 deletions __tests__/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,23 @@ function runBaseTest(name, useProxies, autoFreeze, useListener) {
expect(nextState).not.toEqual(baseState)
})

it("should handle equality correctly - 3", () => {
const baseState = {
x: "s1",
y: 1,
z: NaN
}
const nextState = produce(baseState, draft => {
draft.x = "s2"
draft.y = 1
draft.z = NaN
expect(draft[DRAFT_STATE].assigned_.x).toBe(true)
expect(draft[DRAFT_STATE].assigned_.y).toBe(undefined)
expect(draft[DRAFT_STATE].assigned_.z).toBe(undefined)
})
expect(nextState.x).toBe("s2")
})

// AKA: recursive produce calls
describe("nested producers", () => {
describe("when base state is not a draft", () => {
Expand Down
8 changes: 4 additions & 4 deletions src/core/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,11 @@ export const objectTraps: ProxyHandler<ProxyState> = {
}

if (
state.copy_![prop] === value &&
(state.copy_![prop] === value &&
// special case: handle new props with value 'undefined'
(value !== undefined || prop in state.copy_)) ||
// special case: NaN
typeof value !== "number" &&
// special case: handle new props with value 'undefined'
(value !== undefined || prop in state.copy_)
(Number.isNaN(value) && Number.isNaN(state.copy_![prop]))
)
return true

Expand Down

0 comments on commit 5721bb7

Please sign in to comment.