The object to revert changes from (will not be modified)
Array of actions to invert and apply
A new object with all the changes reverted
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
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.