Skip to content

Repository files navigation

deep-diff-patch — Compute a minimal JSON-serializable diff between objects and apply it as a patch

Compute a minimal JSON-serializable diff between objects and apply it as a patch

CILicense: MITnpmNode 20+


Install

npm install deep-diff-patch

Usage

import{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'}

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:

  • op is 'add', 'remove', or 'replace'
  • path uses JSON Pointer format (RFC 6901): /key/nested/0
  • value is present for add and replace operations

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

License

MIT

About

Compute a minimal JSON-serializable diff between objects and apply it as a patch

Resources

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages