Deep compare two javascript objects and returns additions, removals and edits.
npm i tiny-diff
The code has been transpiled to es5 syntax so it should work in most environments.
./dist/tiny-diff.js
- cjs bundle, transpiled to node4. referenced in package.json's main entry./dist/tiny-diff.mjs
- es6 exports. transpiled to node4. referenced in package.json's module entry./dist/tiny-diff.umd.js
- browser bundle, transpiled to ie8../dist/tiny-diff.umd.min.js
- browser bundle minified
This module needs the following polyfills:
- Object.entries
- Object.getOwnPropertyDescriptors
- Number.isNaN
const {diff} = require('@tswaters/tiny-diff')
// or: import {diff} from '@tswaters/tiny-diff'
// or: var diff = window.tinyDiff.diff
// or: define(...) // whatever; it's a umd export, figure it out
diff(right, left)
diff(right: *, left: *, path: string?)
- right/left: can be any js value
- path: used internally to track the path.
returns: Diff[]
Each Diff
has the following properties:
- kind (string): One of
add
,remove
orupdate
- path (string): path to changed value
- left: value that was changed
- right: value that was changed
Refer to the tests directory for more examples.
diff({foo: {bar: 'baz'}}, {foo: {bar: 'qoz'}})
// [{ kind: 'update', path: 'foo.bar', left: 'baz', right: 'qoz'}]