Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended diff option to allow for a custom differ function #241

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you could use default paremeters.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thiamsantos Yeah that makes sense -> b1f3281


try {
if (isCollapsed) {
Expand Down