Skip to content
Myr

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

2 files+155 added−54 removed

modified:__tests__/patch.js+89 added-18 removed
74 unmodified lines
75 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()8586 // 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})8081describe("simple assignment - 1", () => {82 runPatchTest(
14 unmodified lines
97 [{op: "replace", path: ["x", "y"], value: 5}]98 )99})100101describe("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})110111describe("delete 1", () => {112 runPatchTest(113 {x: {y: 4}},114 d => {
79 unmodified lines
194 {x: [1, 2, 3]},195 d => {196 d.x.unshift(4)197 },198 [{op: "add", path: ["x", 0], value: 4}]199 )200})201202describe("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 lines
218 {x: [1, 2, 3]},219 d => {220 d.x.splice(1, 1)221 },222 [{op: "remove", path: ["x", 1]}]223 )224})225226describe("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})241242describe("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 lines
319 [{op: "replace", path: ["x", "length"], value: 3}]320 )321})322323describe("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})341342describe("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})360361describe("simple replacement", () => {362 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])363})364
58 unmodified lines
modified:src/patches.js+66 added-36 removed