Skip to content

Commit c4c6351

Browse files
author
Phillip Clark
committed
closes #11. Added prefilter capability
1 parent 68edb7a commit c4c6351

File tree

3 files changed

+69
-14
lines changed

3 files changed

+69
-14
lines changed

index.js

+17-12
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,13 @@
7474
return arr;
7575
}
7676

77-
function deepDiff(lhs, rhs, changes, path, key, stack) {
77+
function deepDiff(lhs, rhs, changes, prefilter, path, key, stack) {
7878
path = path || [];
7979
var currentPath = path.slice(0);
80-
if (key) { currentPath.push(key); }
80+
if (key) {
81+
if (prefilter && prefilter(currentPath, key)) return;
82+
currentPath.push(key);
83+
}
8184
var ltype = typeof lhs;
8285
var rtype = typeof rhs;
8386
if (ltype === 'undefined') {
@@ -104,7 +107,7 @@
104107
if (i >= rhs.length) {
105108
changes(new DiffArray(currentPath, i, new DiffDeleted(undefined, lhs[i])));
106109
} else {
107-
deepDiff(lhs[i], rhs[i], ea, [], null, stack);
110+
deepDiff(lhs[i], rhs[i], ea, prefilter, [], null, stack);
108111
}
109112
}
110113
while(i < rhs.length) {
@@ -116,14 +119,14 @@
116119
akeys.forEach(function(k) {
117120
var i = pkeys.indexOf(k);
118121
if (i >= 0) {
119-
deepDiff(lhs[k], rhs[k], changes, currentPath, k, stack);
122+
deepDiff(lhs[k], rhs[k], changes, prefilter, currentPath, k, stack);
120123
pkeys = arrayRemove(pkeys, i);
121124
} else {
122-
deepDiff(lhs[k], undefined, changes, currentPath, k, stack);
125+
deepDiff(lhs[k], undefined, changes, prefilter, currentPath, k, stack);
123126
}
124127
});
125128
pkeys.forEach(function(k) {
126-
deepDiff(undefined, rhs[k], changes, currentPath, k, stack);
129+
deepDiff(undefined, rhs[k], changes, prefilter, currentPath, k, stack);
127130
});
128131
}
129132
stack.length = stack.length - 1;
@@ -135,13 +138,15 @@
135138
}
136139
}
137140

138-
function accumulateDiff(lhs, rhs, accum) {
141+
function accumulateDiff(lhs, rhs, prefilter, accum) {
139142
accum = accum || [];
140-
deepDiff(lhs, rhs, function(diff) {
141-
if (diff) {
142-
accum.push(diff);
143-
}
144-
});
143+
deepDiff(lhs, rhs,
144+
function(diff) {
145+
if (diff) {
146+
accum.push(diff);
147+
}
148+
},
149+
prefilter);
145150
return (accum.length) ? accum : undefined;
146151
}
147152

test/tests.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
DeepDiff = { Conflict: "here" };
1818
DeepDiffConflict = DeepDiff;
1919
</script>
20-
<script src="../releases/deep-diff-0.1.5.min.js"></script>
21-
<!--script src="../index.js"></script-->
20+
<!--<script src="../releases/deep-diff-0.1.5.min.js"></script>-->
21+
<script src="../index.js"></script>
2222
<script>mocha.setup('bdd')</script>
2323
<script src="tests.js"></script>
2424

test/tests.js

+50
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,56 @@ describe('deep-diff', function() {
161161

162162
});
163163

164+
165+
166+
describe('When filtering keys', function() {
167+
var lhs = { enhancement: 'Filter/Ignore Keys?', numero: 11, submitted_by: 'ericclemmons', supported_by: ['ericclemmons'], status: 'open' };
168+
var rhs = { enhancement: 'Filter/Ignore Keys?', numero: 11, submitted_by: 'ericclemmons', supported_by: [
169+
'ericclemmons',
170+
'TylerGarlick',
171+
'flitbit',
172+
'ergdev'
173+
], status: 'closed', fixed_by: 'flitbit' };
174+
175+
describe('if the filtered property is an array', function() {
176+
177+
it('changes to the array do not appear as a difference', function() {
178+
var prefilter = function(path, key) {
179+
return key === 'supported_by'
180+
};
181+
var diff = deep(lhs, rhs, prefilter);
182+
expect(diff).to.be.ok();
183+
expect(diff.length).to.be(2);
184+
expect(diff[0]).to.have.property('kind');
185+
expect(diff[0].kind).to.be('E');
186+
expect(diff[1]).to.have.property('kind');
187+
expect(diff[1].kind).to.be('N');
188+
});
189+
190+
});
191+
192+
describe('if the filtered property is not an array', function() {
193+
194+
it('changes do not appear as a difference', function() {
195+
var prefilter = function(path, key) {
196+
return key === 'fixed_by'
197+
};
198+
var diff = deep(lhs, rhs, prefilter);
199+
expect(diff).to.be.ok();
200+
expect(diff.length).to.be(4);
201+
expect(diff[0]).to.have.property('kind');
202+
expect(diff[0].kind).to.be('A');
203+
expect(diff[1]).to.have.property('kind');
204+
expect(diff[1].kind).to.be('A');
205+
expect(diff[2]).to.have.property('kind');
206+
expect(diff[2].kind).to.be('A');
207+
expect(diff[3]).to.have.property('kind');
208+
expect(diff[3].kind).to.be('E');
209+
});
210+
211+
});
212+
});
213+
164214
describe('A target that has nested values', function() {
165215
var nestedOne = { noChange: 'same', levelOne: { levelTwo: 'value' } };
166216
var nestedTwo = { noChange: 'same', levelOne: { levelTwo: 'another value' } };

0 commit comments

Comments
 (0)