Skip to content

Commit

Permalink
fix: patching maps failed when using number keys (#1025)
Browse files Browse the repository at this point in the history
  • Loading branch information
schummar committed Mar 23, 2023
1 parent 082eecd commit dd83e2e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
18 changes: 18 additions & 0 deletions __tests__/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,24 @@ describe("simple assignment - 7", () => {
)
})

describe("simple assignment - 8", () => {
runPatchTest(
new Map([[0, new Map([[1, 4]])]]),
d => {
d.get(0).set(1, 5)
d.get(0).set(2, 6)
},
[
{op: "replace", path: [0, 1], value: 5},
{op: "add", path: [0, 2], value: 6}
],
[
{op: "replace", path: [0, 1], value: 4},
{op: "remove", path: [0, 2]}
]
)
})

describe("delete 1", () => {
runPatchTest(
{x: {y: 4}},
Expand Down
6 changes: 5 additions & 1 deletion src/plugins/patches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,11 @@ export function enablePatches() {
let base: any = draft
for (let i = 0; i < path.length - 1; i++) {
const parentType = getArchtype(base)
const p = "" + path[i]
let p = path[i]
if (typeof p !== "string" && typeof p !== "number") {
p = "" + p
}

// See #738, avoid prototype pollution
if (
(parentType === Archtype.Object || parentType === Archtype.Array) &&
Expand Down

0 comments on commit dd83e2e

Please sign in to comment.