Compute a minimal JSON-serializable diff between objects and apply it as a patch
npm install deep-diff-patchimport{diff,patch}from'deep-diff-patch';constoldObject={name: 'Alice',age: 30,city: 'NYC'};constnewObject={name: 'Alice',age: 31,email: 'alice@test.com'};constoperations=diff(oldObject,newObject);//=> [// {op: 'replace', path: '/age', value: 31},// {op: 'add', path: '/email', value: 'alice@test.com'},// {op: 'remove', path: '/city'}// ]constresult=patch(oldObject,operations);//=> {name: 'Alice', age: 31, email: 'alice@test.com'}Returns an array of operations describing the changes from oldObject to newObject.
Each operation has the shape {op, path, value?} where:
opis'add','remove', or'replace'pathuses JSON Pointer format (RFC 6901):/key/nested/0valueis present foraddandreplaceoperations
Recursively compares nested objects and arrays (by index).
Applies an array of operations to a deep clone of object and returns the new object. The input is never mutated.
Alias for patch.
- error-serialize - Serialize/deserialize errors to plain objects
MIT