Skip to content

Commit 0a1704b

Browse files
author
Tim van der Horst
committed
feat: Extended diff option to allow for a custom differ function
1 parent 38d48a0 commit 0a1704b

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Note: logger **must be** the last middleware in chain, otherwise it will log thu
7373
logger = console: LoggerObject, // implementation of the `console` API.
7474
logErrors = true: Boolean, // should the logger catch, log, and re-throw errors?
7575

76-
diff = false: Boolean, // (alpha) show diff between states?
76+
diff = false: Boolean | customDiffer, // (alpha) show diff between states?
7777
diffPredicate // (alpha) filter function for showing states diff, similar to `predicate`
7878
}
7979
```
@@ -159,8 +159,9 @@ Format the title used for each action.
159159

160160
*Default: prints something like `action @ ${time} ${action.type} (in ${took.toFixed(2)} ms)`*
161161

162-
#### __diff (Boolean)__
163-
Show states diff.
162+
#### __diff = (prevState: Object, newState: Object) => diff__
163+
Show states diff. Takes a boolean or optionally a custom differ to use instead of the default
164+
[`deep-diff`](https://github.com/flitbit/diff).
164165

165166
*Default: `false`*
166167

spec/diff.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sinon from 'sinon';
22
import { expect } from 'chai';
3+
import differ from 'deep-diff';
34
import { style, render, default as diffLogger } from '../src/diff';
45

56
context('Diff', () => {
@@ -111,5 +112,16 @@ context('Diff', () => {
111112

112113
expect(logger.log.calledWithExactly('%c CHANGED:', 'color: #2196F3; font-weight: bold', 'name', 'kirk', '→', 'picard')).to.be.true;
113114
});
115+
116+
it('should use a custom differ if provided', () => {
117+
const callback = sinon.spy();
118+
const customDiffer = (prevState, newState) => {
119+
callback();
120+
return differ(prevState, newState);
121+
};
122+
diffLogger({name: 'kirk'}, {name: 'picard'}, logger, false, customDiffer);
123+
124+
expect(callback.called).to.be.true;
125+
});
114126
});
115127
});

src/core.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ function printBuffer(buffer, options) {
4646
level,
4747
diff,
4848
} = options;
49+
const customDiffer = typeof diff === 'function' ? diff : undefined;
4950

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

@@ -126,7 +127,7 @@ function printBuffer(buffer, options) {
126127
}
127128

128129
if (diff) {
129-
diffLogger(prevState, nextState, logger, isCollapsed);
130+
diffLogger(prevState, nextState, logger, isCollapsed, customDiffer);
130131
}
131132

132133
try {

src/diff.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ export function render(diff) {
4141
}
4242
}
4343

44-
export default function diffLogger(prevState, newState, logger, isCollapsed) {
45-
const diff = differ(prevState, newState);
44+
export default function diffLogger(prevState, newState, logger, isCollapsed, customDiffer) {
45+
const diff = (customDiffer || differ)(prevState, newState);
4646

4747
try {
4848
if (isCollapsed) {

0 commit comments

Comments
 (0)