Skip to content

Commit

Permalink
Do not replace array.length
Browse files Browse the repository at this point in the history
Patches generated by the current implementation do not follow the RFC method for removing an element from an array (https://www.rfc-editor.org/rfc/rfc6902#page-13) and cannot be used from other languages (e.g., https://pypi.org/project/jsonpatch/).
  • Loading branch information
kshramt committed Jul 31, 2022
1 parent f0de60a commit 503b4be
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
32 changes: 22 additions & 10 deletions __tests__/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ describe("arrays - splice middle", () => {
},
[
{op: "replace", path: ["x", 1], value: 3},
{op: "replace", path: ["x", "length"], value: 2}
{op: "remove", path: ["x", 2]}
]
)
})
Expand All @@ -574,7 +574,8 @@ describe("arrays - multiple splice", () => {
{op: "replace", path: [1], value: 3},
{op: "replace", path: [2], value: 3},
{op: "replace", path: [4], value: 0},
{op: "replace", path: ["length"], value: 5}
{op: "remove", path: [6]},
{op: "remove", path: [5]}
]
)
})
Expand All @@ -589,7 +590,7 @@ describe("arrays - modify and shrink", () => {
},
[
{op: "replace", path: ["x", 0], value: 4},
{op: "replace", path: ["x", "length"], value: 2}
{op: "remove", path: ["x", 2]}
],
[
{op: "replace", path: ["x", 0], value: 1},
Expand Down Expand Up @@ -634,7 +635,10 @@ describe("arrays - truncate", () => {
d => {
d.x.length -= 2
},
[{op: "replace", path: ["x", "length"], value: 1}],
[
{op: "remove", path: ["x", 2]},
{op: "remove", path: ["x", 1]}
],
[
{op: "add", path: ["x", 1], value: 2},
{op: "add", path: ["x", 2], value: 3}
Expand All @@ -649,7 +653,10 @@ describe("arrays - pop twice", () => {
d.x.pop()
d.x.pop()
},
[{op: "replace", path: ["x", "length"], value: 1}]
[
{op: "remove", path: ["x", 2]},
{op: "remove", path: ["x", 1]}
]
)
})

Expand All @@ -664,7 +671,10 @@ describe("arrays - push multiple", () => {
{op: "add", path: ["x", 3], value: 4},
{op: "add", path: ["x", 4], value: 5}
],
[{op: "replace", path: ["x", "length"], value: 3}]
[
{op: "remove", path: ["x", 4]},
{op: "remove", path: ["x", 3]}
]
)
})

Expand All @@ -684,7 +694,8 @@ describe("arrays - splice (expand)", () => {
[
{op: "replace", path: ["x", 1], value: 2},
{op: "replace", path: ["x", 2], value: 3},
{op: "replace", path: ["x", "length"], value: 3}
{op: "remove", path: ["x", 4]},
{op: "remove", path: ["x", 3]}
]
)
})
Expand All @@ -699,7 +710,8 @@ describe("arrays - splice (shrink)", () => {
[
{op: "replace", path: ["x", 1], value: 6},
{op: "replace", path: ["x", 2], value: 5},
{op: "replace", path: ["x", "length"], value: 3}
{op: "remove", path: ["x", 4]},
{op: "remove", path: ["x", 3]}
],
[
{op: "replace", path: ["x", 1], value: 2},
Expand Down Expand Up @@ -817,7 +829,7 @@ describe("arrays - splice should should result in remove op.", () => {
d => {
d.splice(1, 1)
},
[{op: "replace", path: ["length"], value: 1}],
[{op: "remove", path: [1]}],
[{op: "add", path: [1], value: 2}]
)
})
Expand All @@ -829,7 +841,7 @@ describe("arrays - NESTED splice should should result in remove op.", () => {
d => {
d.a.b.c.splice(1, 1)
},
[{op: "replace", path: ["a", "b", "c", "length"], value: 1}],
[{op: "remove", path: ["a", "b", "c", 1]}],
[{op: "add", path: ["a", "b", "c", 1], value: 2}]
)
})
Expand Down
8 changes: 4 additions & 4 deletions src/plugins/patches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ export function enablePatches() {
value: clonePatchValueIfNeeded(copy_[i])
})
}
if (base_.length < copy_.length) {
for (let i = copy_.length - 1; base_.length <= i; --i) {
const path = basePath.concat([i])
inversePatches.push({
op: REPLACE,
path: basePath.concat(["length"]),
value: base_.length
op: REMOVE,
path
})
}
}
Expand Down

0 comments on commit 503b4be

Please sign in to comment.