Skip to content

Commit

Permalink
Attempt to fix coalesce adjacent remove + add into replace
Browse files Browse the repository at this point in the history
Signed-off-by: Manish Bhatia <[email protected]>
  • Loading branch information
Manish Bhatia committed Jan 31, 2020
1 parent 90b3454 commit 8447149
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions jiff.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,19 @@ function lcsToJsonPatch(a1, a2, path, state, lcsMatrix) {
// Coalesce adjacent remove + add into replace
last = patch[patch.length-1];
context = state.makeContext(j, a1);
prevP = path + '/' + (j + (offset - 1));

if(state.invertible) {
patch.push({ op: 'test', path: p, value: a1[j], context: context });
}

if(last !== void 0 && last.op === 'add' && last.path === p) {
if(last !== void 0 && last.op === 'add' && last.path === prevP) {
last.op = 'replace';
last.context = context;
// Insert a test before the replace operation in case invertible is true
if(state.invertible) {
patch.splice(patch.length - 2, 0, { op: 'test', path: prevP, value: a1[j], context: context })
}
} else {
if(state.invertible) {
patch.push({ op: 'test', path: p, value: a1[j], context: context });
}
patch.push({ op: 'remove', path: p, context: context });
}

Expand Down

0 comments on commit 8447149

Please sign in to comment.