/tiny-diff

Deep compare two javascript objects and returns additions, removals and edits.

Primary LanguageJavaScriptMIT LicenseMIT

Tiny Diff

npm version Build Status Coverage Status Dependency Status devDependency Status

Deep compare two javascript objects and returns additions, removals and edits.

Installation

npm i tiny-diff

Files

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

Usage

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)

API

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 or update
  • path (string): path to changed value
  • left: value that was changed
  • right: value that was changed

Examples

Refer to the tests directory for more examples.

diff({foo: {bar: 'baz'}}, {foo: {bar: 'qoz'}})
// [{ kind: 'update', path: 'foo.bar', left: 'baz', right: 'qoz'}]