Skip to content

Documentation / base / applyPatch

Function: applyPatch() ​

applyPatch(base, patch): DiffValue

Defined in: libs/base/src/diff.ts:331

Apply a DiffPatch to a given base value to produce a new value.

Behavior:

  • set: sets/replaces the value at path (deep-cloned).
  • del: deletes the value at path; deleting the root yields undefined.
  • arr: applies array element sets first, then ins, then del (descending indices), minimizing index-shift side-effects during mutation.

Structural handling:

  • Intermediate containers are created as needed: arrays for numeric next keys, objects otherwise.
  • If an arr operation targets a non-array location, the array result is reconstructed by replaying the sub-ops against an empty array and then placed at the path.

Immutability:

  • The function starts by deep-cloning base to avoid mutating the input.

Parameters ​

base ​

DiffValue

The source value onto which the patch is applied.

patch ​

DiffPatch

The patch to apply (produced by diff).

Returns ​

DiffValue

The result of applying patch to base.

Released under the MIT License.