Skip to content

Commit

Permalink
feat: Extended diff option to allow for a custom differ function
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim van der Horst committed Jun 9, 2017
1 parent 38d48a0 commit 0a1704b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
}
```
Expand Down Expand Up @@ -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`*

Expand Down
12 changes: 12 additions & 0 deletions spec/diff.spec.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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;
});
});
});
3 changes: 2 additions & 1 deletion src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ function printBuffer(buffer, options) {
level,
diff,
} = options;
const customDiffer = typeof diff === 'function' ? diff : undefined;

const isUsingDefaultFormatter = typeof options.titleFormatter === 'undefined';

Expand Down Expand Up @@ -126,7 +127,7 @@ function printBuffer(buffer, options) {
}

if (diff) {
diffLogger(prevState, nextState, logger, isCollapsed);
diffLogger(prevState, nextState, logger, isCollapsed, customDiffer);
}

try {
Expand Down
4 changes: 2 additions & 2 deletions src/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 0a1704b

Please sign in to comment.