From d6617a9ec4b526362fa61e3c66f913b10eb0f029 Mon Sep 17 00:00:00 2001 From: Manish Bhatia Date: Fri, 31 Jan 2020 23:40:57 +0530 Subject: [PATCH] Attempt to fix coalesce adjacent remove + add into replace Signed-off-by: Manish Bhatia --- jiff.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/jiff.js b/jiff.js index 65669e7..dfbff36 100644 --- a/jiff.js +++ b/jiff.js @@ -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 - 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 }); }