From 0a1704bb9acc6e3a12a4e59cc118a29179a9117a Mon Sep 17 00:00:00 2001 From: Tim van der Horst Date: Fri, 9 Jun 2017 12:30:55 +1000 Subject: [PATCH 1/2] feat: Extended diff option to allow for a custom differ function --- README.md | 7 ++++--- spec/diff.spec.js | 12 ++++++++++++ src/core.js | 3 ++- src/diff.js | 4 ++-- 4 files changed, 20 insertions(+), 6 deletions(-) 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..e71e42f 100644 --- a/spec/diff.spec.js +++ b/spec/diff.spec.js @@ -1,5 +1,6 @@ import sinon from 'sinon'; import { expect } from 'chai'; +import differ 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 differ(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..431f71d 100644 --- a/src/diff.js +++ b/src/diff.js @@ -41,8 +41,8 @@ export function render(diff) { } } -export default function diffLogger(prevState, newState, logger, isCollapsed) { - const diff = differ(prevState, newState); +export default function diffLogger(prevState, newState, logger, isCollapsed, customDiffer) { + const diff = (customDiffer || differ)(prevState, newState); try { if (isCollapsed) { From b1f3281a3392ea9381baf39b6cb2a83abfd161b3 Mon Sep 17 00:00:00 2001 From: Tim van der Horst Date: Mon, 12 Jun 2017 14:07:20 +1000 Subject: [PATCH 2/2] Changed custom differ to be a parameter defaulting to `deep-diff` --- spec/diff.spec.js | 4 ++-- src/diff.js | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/spec/diff.spec.js b/spec/diff.spec.js index e71e42f..599e7cd 100644 --- a/spec/diff.spec.js +++ b/spec/diff.spec.js @@ -1,6 +1,6 @@ import sinon from 'sinon'; import { expect } from 'chai'; -import differ from 'deep-diff'; +import deepDiff from 'deep-diff'; import { style, render, default as diffLogger } from '../src/diff'; context('Diff', () => { @@ -117,7 +117,7 @@ context('Diff', () => { const callback = sinon.spy(); const customDiffer = (prevState, newState) => { callback(); - return differ(prevState, newState); + return deepDiff(prevState, newState); }; diffLogger({name: 'kirk'}, {name: 'picard'}, logger, false, customDiffer); diff --git a/src/diff.js b/src/diff.js index 431f71d..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,8 +41,8 @@ export function render(diff) { } } -export default function diffLogger(prevState, newState, logger, isCollapsed, customDiffer) { - const diff = (customDiffer || differ)(prevState, newState); +export default function diffLogger(prevState, newState, logger, isCollapsed, differ = deepDiff) { + const diff = differ(prevState, newState); try { if (isCollapsed) {