Skip to content

Commit 3abd2c5

Browse files
author
Sergii.Kliuchnyk
committedFeb 17, 2017
ignore: option
1 parent e31626a commit 3abd2c5

File tree

6 files changed

+98
-14
lines changed

6 files changed

+98
-14
lines changed
 

‎README.md

+4
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ var changes = diff(
168168
);
169169
```
170170

171+
## `ignore: function (oldValue, newValue, options)`
172+
173+
Function which will be called before each object comparison and if returning value will be `true` then objects will be ignored. In `options` will be `oldPath` and `newPath`. Useful to prevent from traversing through circular references and when you don't want to compare some kind of objects at all.
174+
171175
## `callback: function (event)`
172176

173177
Function which will be called for each event. If callback is passed then lib will not create array of all changes.

‎bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "simple-diff",
33
"main": "simple-diff.js",
4-
"version": "1.5.0",
4+
"version": "1.6.0",
55
"homepage": "https://github.com/redexp/simple-diff",
66
"authors": [
77
"Sergii Kliuchnyk <sergiikliuchnyk@gmail.com>"

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "simple-diff",
3-
"version": "1.5.0",
3+
"version": "1.6.0",
44
"description": "simple diff for object and arrays with options",
55
"main": "simple-diff.js",
66
"scripts": {

‎simple-diff.js

+14-11
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,23 @@
2929
changes.push(item);
3030
},
3131
comparators = ops.comparators || [],
32+
ignore = ops.ignore,
3233
i, len, prop, id;
3334

34-
if (!isObject(oldObj) || !isObject(newObj)) {
35-
if (oldObj !== newObj) {
36-
callback({
37-
oldPath: oldPath,
38-
newPath: newPath,
39-
type: CHANGE_EVENT,
40-
oldValue: oldObj,
41-
newValue: newObj
42-
});
35+
if ((!isObject(oldObj) || !isObject(newObj)) && oldObj !== newObj) {
36+
callback({
37+
oldPath: oldPath,
38+
newPath: newPath,
39+
type: CHANGE_EVENT,
40+
oldValue: oldObj,
41+
newValue: newObj
42+
});
4343

44-
return changes;
45-
}
44+
return changes;
45+
}
46+
47+
if (ignore && ignore(oldObj, newObj, {oldPath: oldPath, newPath: newPath})) {
48+
return changes;
4649
}
4750

4851
if (isArray(oldObj)) {

‎simple-diff.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/test.js

+77
Original file line numberDiff line numberDiff line change
@@ -634,4 +634,81 @@ describe('diff', function () {
634634
}
635635
]);
636636
});
637+
638+
it('should ignore', function () {
639+
var changes = diff(
640+
{
641+
prop: {
642+
test1: {
643+
test11: 1
644+
},
645+
test2: 1
646+
}
647+
},
648+
{
649+
prop: {
650+
test1: {
651+
test11: 2
652+
},
653+
test2: 2
654+
}
655+
},
656+
{
657+
ignore: function (a, b, ops) {
658+
return ops.oldPath.join('.') === 'prop.test1';
659+
}
660+
}
661+
);
662+
663+
expect(changes).to.deep.equal([
664+
{
665+
oldPath: ['prop', 'test2'],
666+
newPath: ['prop', 'test2'],
667+
type: 'change',
668+
oldValue: 1,
669+
newValue: 2
670+
}
671+
]);
672+
673+
var root = {
674+
prop: {
675+
test2: 2
676+
}
677+
};
678+
679+
root.prop.test1 = root;
680+
681+
changes = diff(
682+
root,
683+
{
684+
prop: {
685+
test1: {},
686+
test2: 3
687+
}
688+
},
689+
{
690+
ignore: function (oldObj, newObj, options) {
691+
var parent = root;
692+
693+
if (options.oldPath.length === 0) return false;
694+
if (parent === oldObj) return true;
695+
696+
return options.oldPath.slice(0, -1).find(function (prop) {
697+
parent = parent[prop];
698+
return parent === oldObj;
699+
});
700+
}
701+
}
702+
);
703+
704+
expect(changes).to.deep.equal([
705+
{
706+
oldPath: ['prop', 'test2'],
707+
newPath: ['prop', 'test2'],
708+
type: 'change',
709+
oldValue: 2,
710+
newValue: 3
711+
}
712+
]);
713+
});
637714
});

0 commit comments

Comments
 (0)