deep-diff-patch
v1.0.0
Published
Compute a minimal JSON-serializable diff between objects and apply it as a patch
Maintainers
Readme
deep-diff-patch
Compute a minimal JSON-serializable diff between objects and apply it as a patch
Install
npm install deep-diff-patchUsage
import {diff, patch} from 'deep-diff-patch';
const oldObject = {name: 'Alice', age: 30, city: 'NYC'};
const newObject = {name: 'Alice', age: 31, email: '[email protected]'};
const operations = diff(oldObject, newObject);
//=> [
// {op: 'replace', path: '/age', value: 31},
// {op: 'add', path: '/email', value: '[email protected]'},
// {op: 'remove', path: '/city'}
// ]
const result = patch(oldObject, operations);
//=> {name: 'Alice', age: 31, email: '[email protected]'}API
diff(oldObject, newObject, basePath?)
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).
patch(object, operations)
Applies an array of operations to a deep clone of object and returns the new object. The input is never mutated.
applyPatch(object, operations)
Alias for patch.
Related
- error-serialize - Serialize/deserialize errors to plain objects
License
MIT
