JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1829
  • Score
    100M100P100Q123600F
  • License MIT

Human Readable Difference Between Two Objects

Package Exports

  • human-object-diff

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (human-object-diff) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

human-object-diff

build status code coverage code style styled with prettier made with lass license npm downloads

Configurable Human Readable Difference Between Two Plain Objects

Table of Contents

Install

npm:

npm install human-object-diff

yarn:

yarn add human-object-diff

Usage

const humanDiff = require('human-object-diff');

const lhs = { foo: 'bar' };
const rhs = { foo: 'baz' };
const options = {};

const diff = humanDiff(lhs, rhs, options);

console.log(humanObjectDiff.renderName());
// -> ['Foo", with a value of "bar" (at Obj.foo) was changed to "baz"']

Configuring

Options

human-object-diff supports a variety of options to allow you to take control over the output of your object diff. Future versions will allow users to fully customize sentence structure.

Option type Default Description
objectName String 'Obj' This is the object name when presented in the path. ie... "Obj.foo" ignored if hidePath is true
prefilter Array|Func see prefiltering
dateFormat String 'MM/dd/yyyy hh:mm a' dateFns format string see below
futureTense Bool 'past' If set to true, sentences will output "will be" changed instead of "was changed"
hidePath Bool false If set to true, path..ie "(Obj.foo)".. is suppressed making the output less technical
ignoreArrays Bool false If array differences aren't needed. Set to true and skip processing

Support for Dates

human-object-diff uses date-fns format function under the hood to show human readable date differences. We also supply a dateFormat option where you can supply your own date formatting string. Please note, that date-fns format strings are different from moment.js format strings. Please refer to the documentation here and here

Prefiltering

There may be some paths in your object diffs that you'd like to ignore. You can do that with prefiltering. As a convenience, ou can add this option as an array of strings, which are the keys of the base path of the object.

for instance

const lhs = { foo: 'bar', biz: { foo: 'baz' } };
const rhs = { foo: 'bar', biz: { foo: 'buzz' } };

hrDiff(lhs, rhs, { prefilter: ['foo'] });

You would still see the diffs for biz.foo but you would ignore the diff for foo.

You can also pass a function for this option which will be directly passed to the underlying diff library.

The prefilter function takes a signature of function(path, key). Here path is an array that represents the path leading up to the object property. The key is the key, or what would be the final element of the path. The function returns true for any paths you would want to ignore.

For instance, in the object below:

const obj = { foo: { bar: [1, 2, { baz: 'buzz' }] } };

The path and key for foo would be path [] and key 'foo'.

The path and key for foo.bar would be path ['foo'] key 'bar'

for foo.bar[2].baz it would be path: ['foo', 'bar', 2] and key 'baz'

To ignore changes in foo.bar you could pass a functions like

const prefilter = (path, key) => path[0] === 'foo' && key === 'bar';

Contributors

Name Website
Spencer Snyder http://spencersnyder.io/

License

MIT © Spencer Snyder