Skip to content

Commit

Permalink
Fix final remove order in diff util (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
angus-c authored Apr 30, 2022
1 parent 3fd356a commit 7dc5699
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
11 changes: 6 additions & 5 deletions packages/collection-diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,13 @@ function diff(obj1, obj2, pathConverter) {
}
}

return diffs.remove
.reverse()
.concat(diffs.replace)
.concat(diffs.add);
return diffs;
}
return getDiff(obj1, obj2, [], {remove: [], replace: [], add: []});
const finalDiffs = getDiff(obj1, obj2, [], {remove: [], replace: [], add: []});
return finalDiffs.remove
.reverse()
.concat(finalDiffs.replace)
.concat(finalDiffs.add);
}

function pushReplace(path, basePath, key, diffs, pathConverter, obj2) {
Expand Down
27 changes: 24 additions & 3 deletions test/collection-diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,8 @@ test('arrays', function(t) {
);
t.ok(
compare(diff(obj14, obj13), [
{op: 'remove', path: [3]},
{op: 'remove', path: [1, 'b']},
{op: 'remove', path: [3]},
{op: 'replace', path: [0], value: 'a'},
{op: 'add', path: [1, 'a'], value: 3},
])
Expand Down Expand Up @@ -381,7 +381,7 @@ test('object vs array', function(t) {
});

test('round trip', function(t) {
t.plan(4);
t.plan(7);

var obj15 = [1, 2, 3, 4, 5, 17, 18];
var obj16 = [2];
Expand All @@ -390,6 +390,13 @@ test('round trip', function(t) {
diffApply(obj15, thisDiff);
t.ok(compare(obj15, obj16));

var obj15a = {numbers: [1, 2, 3, 4]};
var obj16a = {numbers: [5, 6]};

var thisDiff = diff(obj15a, obj16a);
diffApply(obj15a, thisDiff);
t.ok(compare(obj15a, obj16a));

var obj15a = [1, 2, 3, 4, 5, 17, 18];
var obj16a = [2];

Expand All @@ -410,6 +417,20 @@ test('round trip', function(t) {
thisDiff = diff(obj18a, obj17a);
diffApply(obj18a, thisDiff);
t.ok(compare(obj17a, obj18a));

var obj17b = ['a', {a: 3}, 'd'];
var obj18b = ['b', {b: 3}, 'd', 'e'];

thisDiff = diff(obj17b, obj18b);
diffApply(obj17b, thisDiff);
t.ok(compare(obj17b, obj18b));

var obj17c = ['a', {a: 3}, 'd'];
var obj18c = ['b', {b: 3}, 'd', 'e'];

thisDiff = diff(obj18c, obj17c);
diffApply(obj18c, thisDiff);
t.ok(compare(obj17c, obj18c));
});

test('flat objects using jsPatchStandard', function(t) {
Expand Down Expand Up @@ -607,8 +628,8 @@ test('arrays using jsPatchStandard', function(t) {
);
t.ok(
compare(diff(obj14, obj13, jsonPatchPathConverter), [
{op: 'remove', path: '/3'},
{op: 'remove', path: '/1/b'},
{op: 'remove', path: '/3'},
{op: 'replace', path: '/0', value: 'a'},
{op: 'add', path: '/1/a', value: 3},
])
Expand Down

0 comments on commit 7dc5699

Please sign in to comment.