Skip to content

Commit 5721bb7

Browse files
Rayzhangzhangzhangpeifeng
and
zhangpeifeng
authored
fix: special case NaN in setter (#912)
* fix: special case NaN in setter * fix: naN special case Co-authored-by: zhangpeifeng <[email protected]>
1 parent 8aac6ca commit 5721bb7

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

__tests__/base.js

+17
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,23 @@ function runBaseTest(name, useProxies, autoFreeze, useListener) {
12821282
expect(nextState).not.toEqual(baseState)
12831283
})
12841284

1285+
it("should handle equality correctly - 3", () => {
1286+
const baseState = {
1287+
x: "s1",
1288+
y: 1,
1289+
z: NaN
1290+
}
1291+
const nextState = produce(baseState, draft => {
1292+
draft.x = "s2"
1293+
draft.y = 1
1294+
draft.z = NaN
1295+
expect(draft[DRAFT_STATE].assigned_.x).toBe(true)
1296+
expect(draft[DRAFT_STATE].assigned_.y).toBe(undefined)
1297+
expect(draft[DRAFT_STATE].assigned_.z).toBe(undefined)
1298+
})
1299+
expect(nextState.x).toBe("s2")
1300+
})
1301+
12851302
// AKA: recursive produce calls
12861303
describe("nested producers", () => {
12871304
describe("when base state is not a draft", () => {

src/core/proxy.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,11 @@ export const objectTraps: ProxyHandler<ProxyState> = {
158158
}
159159

160160
if (
161-
state.copy_![prop] === value &&
161+
(state.copy_![prop] === value &&
162+
// special case: handle new props with value 'undefined'
163+
(value !== undefined || prop in state.copy_)) ||
162164
// special case: NaN
163-
typeof value !== "number" &&
164-
// special case: handle new props with value 'undefined'
165-
(value !== undefined || prop in state.copy_)
165+
(Number.isNaN(value) && Number.isNaN(state.copy_![prop]))
166166
)
167167
return true
168168

0 commit comments

Comments
 (0)