Skip to content

Documentation / base / diff

Function: diff()

diff(base, target): DiffPatch

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

Compute a patch that transforms base into target.

This function emits a sequence of operations needed to convert the input value base into target. The resulting DiffPatch can be applied with applyPatch.

Semantics:

  • Primitives: emits a single set if values differ.
  • Objects: recurses into keys; emits set for additions/updates and del for removals.
  • Arrays: emits an arr operation containing element-wise set, ins, and del.

Notes:

  • Comparison for primitives uses strict equality (===) via shallowEqual.
  • Complex nested changes within arrays are represented either as:
    • element-wise set when types differ or primitives differ, or
    • nested operations pushed to the top-level with extended paths when elements are arrays/objects.
  • This is not a minimum-edit-distance diff; it's a straightforward positional diff.

Parameters

base

DiffValue

The source value.

target

DiffValue

The desired target value.

Returns

DiffPatch

A DiffPatch that converts base into target.

Released under the MIT License.