human-object-diff
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -46,2 +46,8 @@ const deepdiff = require('deep-diff'); | ||
let prefilter; | ||
if (Array.isArray(config.prefilter)) | ||
prefilter = (path, key) => | ||
path.length === 0 && config.prefilter.includes(key); | ||
else if (typeof config.prefilter === 'function') prefilter = config.prefilter; | ||
function humanize(prop) { | ||
@@ -145,3 +151,3 @@ return dontHumanizePropertyNames ? prop : titleize(humanizeStr(prop)); | ||
function humanReadable(lhs, rhs) { | ||
const differences = deepdiff(lhs, rhs); | ||
const differences = deepdiff(lhs, rhs, prefilter); | ||
if (!differences) return []; | ||
@@ -148,0 +154,0 @@ const changes = differences.reduce(reducer, []); |
{ | ||
"name": "human-object-diff", | ||
"description": "Human Readable Difference Between Two Objects", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"author": "Spencer Snyder <sasnyde2@gmail.com> (http://spencersnyder.io/)", | ||
@@ -37,4 +37,2 @@ "bugs": { | ||
"nyc": "latest", | ||
"remark-cli": "latest", | ||
"remark-preset-github": "latest", | ||
"xo": "latest" | ||
@@ -64,6 +62,2 @@ }, | ||
], | ||
"*.md": [ | ||
"remark . -qfo", | ||
"git add" | ||
], | ||
"package.json": [ | ||
@@ -92,3 +86,3 @@ "fixpack", | ||
"coverage": "nyc report --reporter=text-lcov > coverage.lcov && codecov", | ||
"lint": "xo && remark . -qfo", | ||
"lint": "xo", | ||
"nyc": "cross-env NODE_ENV=test nyc ava", | ||
@@ -95,0 +89,0 @@ "test": "yarn run lint && yarn run ava", |
@@ -18,3 +18,6 @@ # human-object-diff | ||
* [Usage](#usage) | ||
* [Configuring](#configuring) | ||
* [Options](#options) | ||
* [Support for Dates](#support-for-dates) | ||
* [Prefiltering](#prefiltering) | ||
* [Contributors](#contributors) | ||
@@ -28,3 +31,3 @@ * [License](#license) | ||
```sh | ||
```bash | ||
npm install human-object-diff | ||
@@ -35,3 +38,3 @@ ``` | ||
```sh | ||
```bash | ||
yarn add human-object-diff | ||
@@ -56,12 +59,60 @@ ``` | ||
## 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](#prefiltering) | | ||
| dateFormat | String | 'MM/dd/yyyy hh:mm a' | dateFns format string see [below](#support-for-dates) | | ||
| 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](https://date-fns.org/v2.8.1/docs/format) and [here](https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md) | ||
`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](https://date-fns.org/v2.8.1/docs/format) and [here](https://github.com/date-fns/date-fns/blob/master/docs/unicodeTokens.md) | ||
### 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 | ||
```js | ||
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](https://www.npmjs.com/package/deep-diff). | ||
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: | ||
```js | ||
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 | ||
```js | ||
const prefilter = (path, key) => path[0] === 'foo' && key === 'bar'; | ||
``` | ||
## Contributors | ||
@@ -68,0 +119,0 @@ |
24626
12
14
446
131