Skip to content

Commit

Permalink
feat: Always use remove command in generateArrayPatches()
Browse files Browse the repository at this point in the history
Fixes #208
  • Loading branch information
jsdevtom committed Sep 6, 2019
1 parent a16deda commit d483334
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
33 changes: 28 additions & 5 deletions __tests__/patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,10 @@ describe("arrays - modify and shrink", () => {
d.x[0] = 4
d.x.length = 2
},
[{op: "replace", path: ["x", 0], value: 4}, {op: "remove", path: ["x", 2]}],
[
{op: "replace", path: ["x", 0], value: 4},
{op: "replace", path: ["x", "length"], value: 2}
{op: "replace", path: ["x", 0], value: 1},
{op: "add", path: ["x", 2], value: 3}
]
)
})
Expand Down Expand Up @@ -290,7 +291,7 @@ 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 @@ -305,7 +306,7 @@ 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 @@ -319,7 +320,7 @@ 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 Down Expand Up @@ -361,6 +362,28 @@ describe("arrays - splice (shrink)", () => {
)
})

describe("arrays - splice should should result in remove op.", () => {
runPatchTest(
[1, 2],
d => {
d.splice(1, 1)
},
[{op: "remove", path: [1]}],
[{op: "add", path: [1], value: 2}]
)
})

describe("arrays - NESTED splice should should result in remove op.", () => {
runPatchTest(
{a: {b: {c: [1, 2]}}},
d => {
d.a.b.c.splice(1, 1)
},
[{op: "remove", path: ["a", "b", "c", 1]}],
[{op: "add", path: ["a", "b", "c", 1], value: 2}]
)
})

describe("simple replacement", () => {
runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])
})
Expand Down
16 changes: 2 additions & 14 deletions src/patches.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ function generateArrayPatches(state, basePath, patches, inversePatches) {
}
}

const useRemove = end != base.length
const replaceCount = patches.length

// Process added indices.
Expand All @@ -58,20 +57,9 @@ function generateArrayPatches(state, basePath, patches, inversePatches) {
path,
value: copy[i]
}
if (useRemove) {
inversePatches.push({
op: "remove",
path
})
}
}

// One "replace" patch reverses all non-splicing "add" patches.
if (!useRemove) {
inversePatches.push({
op: "replace",
path: basePath.concat(["length"]),
value: base.length
op: "remove",
path
})
}
}
Expand Down

0 comments on commit d483334

Please sign in to comment.