@zhelvis/structure-ts
    Preparing search index...

    Function revertDiff

    • Reverts a series of diff actions by applying their inverse operations.

      This function takes a target object and a series of actions, then applies the inverse of each action to undo the changes. This is particularly useful for implementing undo functionality. The original target is not modified - a deep clone is created first.

      Parameters

      • target: unknown

        The object to revert changes from (will not be modified)

      • diff: Diff

        Array of actions to invert and apply

      Returns unknown

      A new object with all the changes reverted

      When navigation fails or invalid operations are attempted

      When unknown action types are encountered

      When prototype pollution is detected

      const modified = { a: 1, b: { c: 3 }, d: 4 };
      const changes = [
      { type: ActionType.CHANGE, path: ['b', 'c'], from: 2, to: 3 },
      { type: ActionType.ADD, path: ['d'], value: 4 }
      ];
      const reverted = revertDiff(modified, changes);
      // reverted: { a: 1, b: { c: 2 } } (d is removed, c is changed back)

      // Perfect for undo systems:
      const original = { x: 1 };
      const diff = computeDiff(original, { x: 2, y: 3 });
      const modified = applyDiff(original, diff);
      const undone = revertDiff(modified, diff);
      // undone equals original