feat: improved array patches
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Ilyas Kabirov · committed by Alec Larson · parent 0b6960a · Browse files · 2 files·+155 added−54 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+155 added−54 removed
- tests:+89 added−18 removed
- other:+66 added−36 removed
Provenance
- Commit
cb5707f- Tree
bf9fe9c- Author
Ilyas Kabirov <ilyas.kabirov@gmail.com>- Committer
Alec Larson <alec.stanford.larson@gmail.com>- Parent
0b6960a
2 files changed+155 added−54 removed
modified:__tests__/patch.js+89 added-18 removed
74 unmodified lines75 const patch = {op: "add", path: ["a", "b", "c"], value: 1}76 applyPatches({}, [patch])77 }).toThrowError(/^Cannot apply patch, path doesn't resolve:/)78 })79- it("throws when a patch tries to splice an array", () => {80- // Pop is ok81- expect(() => {82- const patch = {op: "remove", path: [0]}83- applyPatches([1], [patch])84- }).not.toThrowError()85-86- // Splice is unsupported87- expect(() => {88- const patch = {op: "remove", path: [0]}89- applyPatches([1, 2], [patch])90- }).toThrowError(/^Only the last index of an array can be removed/)91- })79 })80 81 describe("simple assignment - 1", () => {82 runPatchTest(14 unmodified lines97 [{op: "replace", path: ["x", "y"], value: 5}]98 )99 })100 101+describe("simple assignment - 3", () => {102+ runPatchTest(103+ {x: [{y: 4}]},104+ d => {105+ d.x[0].y++106+ },107+ [{op: "replace", path: ["x", 0, "y"], value: 5}]108+ )109+})110+111 describe("delete 1", () => {112 runPatchTest(113 {x: {y: 4}},114 d => {79 unmodified lines194 {x: [1, 2, 3]},195 d => {196 d.x.unshift(4)197 },198+ [{op: "add", path: ["x", 0], value: 4}]199+ )200+})201+202+describe("arrays - multiple prepend", () => {203+ runPatchTest(204+ {x: [1, 2, 3]},205+ d => {206+ d.x.unshift(4)207+ d.x.unshift(5)208+ },209 [202- {op: "replace", path: ["x", 0], value: 4},203- {op: "replace", path: ["x", 1], value: 1},204- {op: "replace", path: ["x", 2], value: 2},205- {op: "add", path: ["x", 3], value: 3}210+ {op: "add", path: ["x", 0], value: 5},211+ {op: "add", path: ["x", 1], value: 4}212 ]213 )214 })215 2 unmodified lines218 {x: [1, 2, 3]},219 d => {220 d.x.splice(1, 1)221 },222+ [{op: "remove", path: ["x", 1]}]223+ )224+})225+226+describe("arrays - multiple splice", () => {227+ runPatchTest(228+ [0, 1, 2, 3, 4, 5, 0],229+ d => {230+ d.splice(4, 2, 3)231+ d.splice(1, 2, 3)232+ },233 [217- {op: "replace", path: ["x", 1], value: 3},234+ {op: "replace", path: [1], value: 3},235+ {op: "replace", path: [2], value: 3},236+ {op: "remove", path: [5]},237+ {op: "remove", path: [4]}238+ ]239+ )240+})241+242+describe("arrays - modify and shrink", () => {243+ runPatchTest(244+ {x: [1, 2, 3]},245+ d => {246+ d.x[0] = 4247+ d.x.length = 2248+ },249+ [250+ {op: "replace", path: ["x", 0], value: 4},251 {op: "replace", path: ["x", "length"], value: 2}252 ]253 )254 })64 unmodified lines319 [{op: "replace", path: ["x", "length"], value: 3}]320 )321 })322 323+describe("arrays - splice (expand)", () => {324+ runPatchTest(325+ {x: [1, 2, 3]},326+ d => {327+ d.x.splice(1, 1, 4, 5, 6)328+ },329+ [330+ {op: "replace", path: ["x", 1], value: 4},331+ {op: "add", path: ["x", 2], value: 5},332+ {op: "add", path: ["x", 3], value: 6}333+ ],334+ [335+ {op: "replace", path: ["x", 1], value: 2},336+ {op: "remove", path: ["x", 3]},337+ {op: "remove", path: ["x", 2]}338+ ]339+ )340+})341+342+describe("arrays - splice (shrink)", () => {343+ runPatchTest(344+ {x: [1, 2, 3, 4, 5]},345+ d => {346+ d.x.splice(1, 3, 6)347+ },348+ [349+ {op: "replace", path: ["x", 1], value: 6},350+ {op: "remove", path: ["x", 3]},351+ {op: "remove", path: ["x", 2]}352+ ],353+ [354+ {op: "replace", path: ["x", 1], value: 2},355+ {op: "add", path: ["x", 2], value: 3},356+ {op: "add", path: ["x", 3], value: 4}357+ ]358+ )359+})360+361 describe("simple replacement", () => {362 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])363 })364 58 unmodified linesmodified:src/patches.js+66 added-36 removed
5 unmodified lines6789101110111314151617181912131415161722232418192021222324252627282930313233343536373826272839404142434445464748313233343549505138394041424344455253545556575859606162636465474849666768697071727374757677787980818229 unmodified lines11211311411590911161171181191201211221231241251261279697981281291301311327 unmodified lines5 unmodified lines : generateObjectPatches(state, basePath, patches, inversePatches)}
function generateArrayPatches(state, basePath, patches, inversePatches) { const {base, copy, assigned} = state const minLength = Math.min(base.length, copy.length) let {base, copy, assigned} = state
// Look for replaced indices. for (let i = 0; i < minLength; i++) { if (assigned[i] && base[i] !== copy[i]) { const path = basePath.concat(i) patches.push({op: "replace", path, value: copy[i]}) inversePatches.push({op: "replace", path, value: base[i]}) } // Guarantee `base` is never longer than `copy` if (copy.length < base.length) { ;[base, copy] = [copy, base] ;[patches, inversePatches] = [inversePatches, patches] }
// Did the array expand? if (minLength < copy.length) { for (let i = minLength; i < copy.length; i++) { const delta = copy.length - base.length
// Find the first changed index. let start = 0 while (base[start] === copy[start] && start < base.length) { ++start }
// Find the last changed index. let baseEnd = base.length while (baseEnd > start && base[baseEnd - 1] === copy[baseEnd + delta - 1]) { --baseEnd }
// Look for replaced indices. const replaceCount = baseEnd - start for (let i = 0; i < replaceCount; ++i) { const index = start + i const path = basePath.concat([index]) if (assigned[index] && copy[index] !== base[index]) { patches.push({ op: "add", path: basePath.concat(i), value: copy[i] op: "replace", path, value: copy[index] }) inversePatches.push({ op: "replace", path, value: base[index] }) } inversePatches.push({ op: "replace", path: basePath.concat("length"), value: base.length }) } start += replaceCount
// ...or did it shrink? else if (minLength < base.length) { patches.push({ op: "replace", path: basePath.concat("length"), value: copy.length }) for (let i = minLength; i < base.length; i++) { // For "add" patches that extend the array, we can use a single "replace" // patch (on the `length` property) as the inverse patch, instead of multiple // "remove" patches. const useRemove = start != base.length const replacePatchesCount = patches.length for (let i = delta - 1; i >= 0; --i) { const path = basePath.concat([start + i]) patches[i + replacePatchesCount] = { op: "add", path, value: copy[start + i] } if (useRemove) { inversePatches.push({ op: "add", path: basePath.concat(i), value: base[i] op: "remove", path }) } }
if (!useRemove) { inversePatches.push({ op: "replace", path: basePath.concat(["length"]), value: base.length }) }}
function generateObjectPatches(state, basePath, patches, inversePatches) { const {base, copy} = state29 unmodified lines } const key = path[path.length - 1] switch (patch.op) { case "replace": case "add": // TODO: add support is not extensive, it does not support insertion or `-` atm! base[key] = patch.value break case "add": if (Array.isArray(base)) { // TODO: support "foo/-" paths for appending to an array base.splice(key, 0, patch.value) } else { base[key] = patch.value } break case "remove": if (Array.isArray(base)) { if (key !== base.length - 1) throw new Error(`Only the last index of an array can be removed, index: ${key}, length: ${base.length}`) // prettier-ignore base.length -= 1 base.splice(key, 1) } else { delete base[key] } break7 unmodified lines