diff --git a/README.md b/README.md index 2737cc0..842ea76 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ Note: logger **must be** the last middleware in chain, otherwise it will log thu logger = console: LoggerObject, // implementation of the `console` API. logErrors = true: Boolean, // should the logger catch, log, and re-throw errors? - diff = false: Boolean, // (alpha) show diff between states? + diff = false: Boolean | customDiffer, // (alpha) show diff between states? diffPredicate // (alpha) filter function for showing states diff, similar to `predicate` } ``` @@ -159,8 +159,9 @@ Format the title used for each action. *Default: prints something like `action @ ${time} ${action.type} (in ${took.toFixed(2)} ms)`* -#### __diff (Boolean)__ -Show states diff. +#### __diff = (prevState: Object, newState: Object) => diff__ +Show states diff. Takes a boolean or optionally a custom differ to use instead of the default +[`deep-diff`](https://github.com/flitbit/diff). *Default: `false`* diff --git a/spec/diff.spec.js b/spec/diff.spec.js index a89b4bf..599e7cd 100644 --- a/spec/diff.spec.js +++ b/spec/diff.spec.js @@ -1,5 +1,6 @@ import sinon from 'sinon'; import { expect } from 'chai'; +import deepDiff from 'deep-diff'; import { style, render, default as diffLogger } from '../src/diff'; context('Diff', () => { @@ -111,5 +112,16 @@ context('Diff', () => { expect(logger.log.calledWithExactly('%c CHANGED:', 'color: #2196F3; font-weight: bold', 'name', 'kirk', '→', 'picard')).to.be.true; }); + + it('should use a custom differ if provided', () => { + const callback = sinon.spy(); + const customDiffer = (prevState, newState) => { + callback(); + return deepDiff(prevState, newState); + }; + diffLogger({name: 'kirk'}, {name: 'picard'}, logger, false, customDiffer); + + expect(callback.called).to.be.true; + }); }); }); diff --git a/src/core.js b/src/core.js index fba99bc..de7602c 100644 --- a/src/core.js +++ b/src/core.js @@ -46,6 +46,7 @@ function printBuffer(buffer, options) { level, diff, } = options; + const customDiffer = typeof diff === 'function' ? diff : undefined; const isUsingDefaultFormatter = typeof options.titleFormatter === 'undefined'; @@ -126,7 +127,7 @@ function printBuffer(buffer, options) { } if (diff) { - diffLogger(prevState, nextState, logger, isCollapsed); + diffLogger(prevState, nextState, logger, isCollapsed, customDiffer); } try { diff --git a/src/diff.js b/src/diff.js index 31d20d6..b51ded2 100644 --- a/src/diff.js +++ b/src/diff.js @@ -1,4 +1,4 @@ -import differ from 'deep-diff'; +import deepDiff from 'deep-diff'; // https://github.com/flitbit/diff#differences const dictionary = { @@ -41,7 +41,7 @@ export function render(diff) { } } -export default function diffLogger(prevState, newState, logger, isCollapsed) { +export default function diffLogger(prevState, newState, logger, isCollapsed, differ = deepDiff) { const diff = differ(prevState, newState); try {