Remove `simplifyPatch`, it doesn't always work correctly
This commit is contained in:
parent
b71afda45a
commit
b3c41bce03
|
@ -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
|
// when a previous action takes place on the same path, remove the first
|
||||||
return {
|
return {
|
||||||
data: updatedEson,
|
data: updatedEson,
|
||||||
revert: simplifyPatch(revert),
|
revert,
|
||||||
error: null
|
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 {ESON} data
|
||||||
* @param {string} path
|
* @param {string} path
|
||||||
|
|
|
@ -334,6 +334,7 @@ test('jsonpatch move and replace', t => {
|
||||||
|
|
||||||
t.deepEqual(patchedJson2, json)
|
t.deepEqual(patchedJson2, json)
|
||||||
t.deepEqual(revert2, [
|
t.deepEqual(revert2, [
|
||||||
|
{op: 'remove', path: '/b'},
|
||||||
{op: 'move', from: '/a', 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(patchedJson2, json)
|
||||||
t.deepEqual(revert2, [
|
t.deepEqual(revert2, [
|
||||||
|
{op: 'remove', path: '/arr'},
|
||||||
{op: 'move', from: '/obj', path: '/arr'}
|
{op: 'move', from: '/obj', path: '/arr'}
|
||||||
])
|
])
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue