From b3c41bce03bfb700730c37e2867a7f7e7792d52b Mon Sep 17 00:00:00 2001 From: jos Date: Wed, 22 Nov 2017 13:05:53 +0100 Subject: [PATCH] Remove `simplifyPatch`, it doesn't always work correctly --- src/patchEson.js | 34 +--------------------------------- test/patchEson.test.js | 2 ++ 2 files changed, 3 insertions(+), 33 deletions(-) diff --git a/src/patchEson.js b/src/patchEson.js index 1c396c0..a8f7dd2 100644 --- a/src/patchEson.js +++ b/src/patchEson.js @@ -115,7 +115,7 @@ export function patchEson (eson: ESON, patch: ESONPatch, expand = expandAll) { // when a previous action takes place on the same path, remove the first return { data: updatedEson, - revert: simplifyPatch(revert), + revert, error: null } } @@ -190,38 +190,6 @@ export function remove (data: ESON, path: string) { } } -/** - * Remove redundant actions from a ESONPatch array. - * Actions are redundant when they are followed by an action - * acting on the same path. - * @param {ESONPatch} patch - * @return {Array} - */ -export function simplifyPatch(patch: ESONPatch) { - const simplifiedPatch = [] - const paths = {} - - // loop over the patch from last to first - for (let i = patch.length - 1; i >= 0; i--) { - const action = patch[i] - if (action.op === 'test') { - // ignore test actions - simplifiedPatch.unshift(action) - } - else { - // test whether this path was already used - // if not, add this action to the simplified patch - if (paths[action.path] === undefined) { - paths[action.path] = true - simplifiedPatch.unshift(action) - } - } - } - - return simplifiedPatch -} - - /** * @param {ESON} data * @param {string} path diff --git a/test/patchEson.test.js b/test/patchEson.test.js index e19b6a3..93ec21d 100644 --- a/test/patchEson.test.js +++ b/test/patchEson.test.js @@ -334,6 +334,7 @@ test('jsonpatch move and replace', t => { t.deepEqual(patchedJson2, json) t.deepEqual(revert2, [ + {op: 'remove', path: '/b'}, {op: 'move', from: '/a', path: '/b'} ]) }) @@ -371,6 +372,7 @@ test('jsonpatch move and replace (nested)', t => { t.deepEqual(patchedJson2, json) t.deepEqual(revert2, [ + {op: 'remove', path: '/arr'}, {op: 'move', from: '/obj', path: '/arr'} ]) })