Skip to content

Commit dd83e2e

Browse files
authoredMar 23, 2023
fix: patching maps failed when using number keys (#1025)
1 parent 082eecd commit dd83e2e

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed
 

‎__tests__/patch.js

+18
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,24 @@ describe("simple assignment - 7", () => {
214214
)
215215
})
216216

217+
describe("simple assignment - 8", () => {
218+
runPatchTest(
219+
new Map([[0, new Map([[1, 4]])]]),
220+
d => {
221+
d.get(0).set(1, 5)
222+
d.get(0).set(2, 6)
223+
},
224+
[
225+
{op: "replace", path: [0, 1], value: 5},
226+
{op: "add", path: [0, 2], value: 6}
227+
],
228+
[
229+
{op: "replace", path: [0, 1], value: 4},
230+
{op: "remove", path: [0, 2]}
231+
]
232+
)
233+
})
234+
217235
describe("delete 1", () => {
218236
runPatchTest(
219237
{x: {y: 4}},

‎src/plugins/patches.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,11 @@ export function enablePatches() {
207207
let base: any = draft
208208
for (let i = 0; i < path.length - 1; i++) {
209209
const parentType = getArchtype(base)
210-
const p = "" + path[i]
210+
let p = path[i]
211+
if (typeof p !== "string" && typeof p !== "number") {
212+
p = "" + p
213+
}
214+
211215
// See #738, avoid prototype pollution
212216
if (
213217
(parentType === Archtype.Object || parentType === Archtype.Array) &&

0 commit comments

Comments
 (0)
Please sign in to comment.