Skip to content

Commit d4ad868

Browse files
author
Phillip Clark
committed
closes #126 - example of prefiltering
1 parent 8d942ff commit d4ad868

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

Readme.md

+30
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,36 @@ if (changes) {
198198
199199
The `prefilter`'s signature should be `function(path, key)` and it should return a truthy value for any `path`-`key` combination that should be filtered. If filtered, the difference analysis does no further analysis of on the identified object-property path.
200200

201+
```javascript
202+
const diff = require('deep-diff');
203+
const assert = require('assert');
204+
205+
const data = {
206+
issue: 126,
207+
submittedBy: 'abuzarhamza',
208+
title: 'readme.md need some additional example prefilter',
209+
posts: [
210+
{
211+
date: '2018-04-16',
212+
text: `additional example for prefilter for deep-diff would be great.
213+
https://stackoverflow.com/questions/38364639/pre-filter-condition-deep-diff-node-js`
214+
}
215+
]
216+
};
217+
218+
const clone = JSON.parse(JSON.stringify(data));
219+
clone.title = 'README.MD needs additional example illustrating how to prefilter';
220+
clone.disposition = 'completed';
221+
222+
const two = diff(data, clone);
223+
const none = diff(data, clone,
224+
(path, key) => path.length === 0 && ~['title', 'disposition'].indexOf(key)
225+
);
226+
227+
assert.equal(two.length, 2, 'should reflect two differences');
228+
assert.ok(typeof none === 'undefined', 'should reflect no differences');
229+
```
230+
201231
## Contributing
202232
203233
When contributing, keep in mind that it is an objective of `deep-diff` to have no package dependencies. This may change in the future, but for now, no-dependencies.

examples/issue-126.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// This example shows how prefiltering can be used.
2+
3+
const diff = require('../'); // deep-diff
4+
const { log, inspect } = require('util');
5+
const assert = require('assert');
6+
7+
const data = {
8+
issue: 126,
9+
submittedBy: 'abuzarhamza',
10+
title: 'readme.md need some additional example prefilter',
11+
posts: [
12+
{
13+
date: '2018-04-16',
14+
text: `additional example for prefilter for deep-diff would be great.
15+
https://stackoverflow.com/questions/38364639/pre-filter-condition-deep-diff-node-js`
16+
}
17+
]
18+
};
19+
20+
const clone = JSON.parse(JSON.stringify(data));
21+
clone.title = 'README.MD needs additional example illustrating how to prefilter';
22+
clone.disposition = 'completed';
23+
24+
const two = diff(data, clone);
25+
const none = diff(data, clone,
26+
(path, key) => path.length === 0 && ~['title', 'disposition'].indexOf(key)
27+
);
28+
29+
assert.equal(two.length, 2, 'should reflect two differences');
30+
assert.ok(typeof none === 'undefined', 'should reflect no differences');
31+
32+
log(inspect(two, false, 9));
33+
log(inspect(none, false, 9));

0 commit comments

Comments
 (0)