Skip to content
Myr

Merge pull request #415 from jsdevtom/feat/setting-length-array-makes-delete

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Michel Weststrate · committed by GitHub · parents 71454c2, d483334 · Browse files · 2 files+30 added−19 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • feat: Prefer using `remove` patches over changing the `length` property when shortening arrays

Diff stats

2 files+30 added−19 removed

  • tests:+28 added−5 removed
  • other:+2 added−14 removed

2 files+30 added−19 removed

modified:__tests__/patch.js+28 added-5 removed
273 unmodified lines
274 d => {275 d.x[0] = 4276 d.x.length = 2277 },278 [{op: "replace", path: ["x", 0], value: 4}, {op: "remove", path: ["x", 2]}],279 [279 {op: "replace", path: ["x", 0], value: 4},280 {op: "replace", path: ["x", "length"], value: 2}280 {op: "replace", path: ["x", 0], value: 1},281 {op: "add", path: ["x", 2], value: 3}282 ]283 )284})285
30 unmodified lines
316 {x: [1, 2, 3]},317 d => {318 d.x.length -= 2319 },319 [{op: "replace", path: ["x", "length"], value: 1}],320 [{op: "remove", path: ["x", 2]}, {op: "remove", path: ["x", 1]}],321 [322 {op: "add", path: ["x", 1], value: 2},323 {op: "add", path: ["x", 2], value: 3}324 ]
6 unmodified lines
331 d => {332 d.x.pop()333 d.x.pop()334 },334 [{op: "replace", path: ["x", "length"], value: 1}]335 [{op: "remove", path: ["x", 2]}, {op: "remove", path: ["x", 1]}]336 )337})338339describe("arrays - push multiple", () => {
5 unmodified lines
345 [346 {op: "add", path: ["x", 3], value: 4},347 {op: "add", path: ["x", 4], value: 5}348 ],348 [{op: "replace", path: ["x", "length"], value: 3}]349 [{op: "remove", path: ["x", 4]}, {op: "remove", path: ["x", 3]}]350 )351})352353describe("arrays - splice (expand)", () => {
33 unmodified lines
387 ]388 )389})390391describe("arrays - splice should should result in remove op.", () => {392 runPatchTest(393 [1, 2],394 d => {395 d.splice(1, 1)396 },397 [{op: "remove", path: [1]}],398 [{op: "add", path: [1], value: 2}]399 )400})401402describe("arrays - NESTED splice should should result in remove op.", () => {403 runPatchTest(404 {a: {b: {c: [1, 2]}}},405 d => {406 d.a.b.c.splice(1, 1)407 },408 [{op: "remove", path: ["a", "b", "c", 1]}],409 [{op: "add", path: ["a", "b", "c", 1], value: 2}]410 )411})412413describe("simple replacement", () => {414 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])415})416
164 unmodified lines
modified:src/patches.js+2 added-14 removed