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

Fix for issue #35 -- apply diffs for arrays #43

Merged
merged 1 commit into from
Oct 18, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@
if (target && source && change && change.kind) {
var it = target,
i = -1,
last = change.path.length - 1;
last = change.path ? change.path.length - 1 : 0;
while (++i < last) {
if (typeof it[change.path[i]] === 'undefined') {
it[change.path[i]] = (typeof change.path[i] === 'number') ? [] : {};
Expand All @@ -258,7 +258,7 @@
}
switch (change.kind) {
case 'A':
applyArrayChange(it[change.path[i]], change.index, change.item);
applyArrayChange(change.path ? it[change.path[i]] : it, change.index, change.item);
break;
case 'D':
delete it[change.path[i]];
Expand Down
18 changes: 18 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,4 +482,22 @@ describe('deep-diff', function() {

});

describe('regression test for bug #35', function() {
var lhs = ["a", "a", "a"];
var rhs = ["a"];

it('can apply diffs between two top level arrays', function() {
var differences = deep.diff(lhs, rhs);

/* We must apply the differences in reverse order, since the array indices
in the diff become stale/invalid if you delete elements from the array
whose indices are in ascending order */
for (var i = differences.length - 1; i >= 0; i--) {
deep.applyChange(lhs, true, differences[i]);
}

expect(lhs).to.eql(["a"]);
});
});

});