Skip to content
Myr

fix: Append to array when key is "-" (#872)

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

* Appends to array when key is "-" * Avoids unnecessary copy of patches in applyPatches

authored by Louis DeScioli · committed by GitHub · parent 2794576 · Browse files · 3 files+27 added−4 removed
This commit

State

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

Diff stats

3 files+27 added−4 removed

  • tests:+16 added−0 removed
  • other:+11 added−4 removed

3 files+27 added−4 removed

modified:__tests__/patch.js+16 added
718 unmodified lines
719 [{op: "remove", path: ["x", 1, "c"]}]720 )721})722723describe("arrays - append", () => {724 test("appends to array when last part of path is '-'", () => {725 const state = {726 list: [1, 2, 3]727 }728 const patch = {729 op: "add",730 value: 4,731 path: ["list", "-"]732 }733 expect(applyPatches(state, [patch])).toEqual({734 list: [1, 2, 3, 4]735 })736 })737})738739describe("sets - add - 1", () => {740 runPatchTest(741 new Set([1]),742 d => {
579 unmodified lines
modified:src/core/immerClass.ts+8 added-3 removed
194 unmodified lines
195 base = patch.value196 break197 }198 }199 // If there was a patch that replaced the entire state, start from the200 // patch after that.201 if (i > -1) {202 patches = patches.slice(i + 1)203 }204205 const applyPatchesImpl = getPlugin("Patches").applyPatches_206 if (isDraft(base)) {207 // N.B: never hits if some patch a replacement, patches are never drafts203 return applyPatchesImpl(base, patches) as any208 return applyPatchesImpl(base, patches)209 }210 // Otherwise, produce a copy of the base state.211 return this.produce(base, (draft: Drafted) =>207 applyPatchesImpl(draft, patches.slice(i + 1))208 ) as any212 applyPatchesImpl(draft, patches)213 )214 }215}216217export function createProxy<T extends Objectish>(
17 unmodified lines
modified:src/plugins/patches.ts+3 added-1 removed
238 unmodified lines
239 }240 case ADD:241 switch (type) {242 case Archtype.Array:243 return key === "-"244 ? base.push(value)243 return base.splice(key as any, 0, value)245 : base.splice(key as any, 0, value)246 case Archtype.Map:247 return base.set(key, value)248 case Archtype.Set:249 return base.add(value)
52 unmodified lines