Implement apply eson state with jsonpatch replace

This commit is contained in:
jos 2017-12-29 21:40:23 +01:00
parent 419bdc6307
commit ccf89162b7
2 changed files with 44 additions and 10 deletions

View File

@ -35,12 +35,11 @@ export function patchEson (eson, patch, expand = expandAll) {
switch (action.op) { switch (action.op) {
case 'add': { case 'add': {
let newValue = jsonToEson(action.value, path) const newValue = jsonToEson(action.value, path)
if (options && options.state) { const newValueWithState = (options && options.state)
newValue = applyEsonState(newValue, options.state) ? applyEsonState(newValue, options.state)
} : newValue
// FIXME: apply options.type const result = add(updatedEson, path, newValueWithState, options)
const result = add(updatedEson, path, newValue, options)
updatedEson = result.data updatedEson = result.data
revert = result.revert.concat(revert) revert = result.revert.concat(revert)
@ -57,9 +56,10 @@ export function patchEson (eson, patch, expand = expandAll) {
case 'replace': { case 'replace': {
const newValue = jsonToEson(action.value, path) const newValue = jsonToEson(action.value, path)
// FIXME: apply expanded state const newValueWithState = (options && options.state)
// FIXME: apply options.type ? applyEsonState(newValue, options.state)
const result = replace(updatedEson, path, newValue) : newValue
const result = replace(updatedEson, path, newValueWithState)
updatedEson = result.data updatedEson = result.data
revert = result.revert.concat(revert) revert = result.revert.concat(revert)

View File

@ -77,7 +77,7 @@ test('jsonpatch add: append to matrix', () => {
]) ])
}) })
test('jsonpatch add: pass eson state', () => { test('jsonpatch add: apply eson state', () => {
const json = { const json = {
a: 2 a: 2
} }
@ -202,6 +202,40 @@ test('jsonpatch replace (keep ids intact)', () => {
expect(patchedValueId).toEqual(valueId) expect(patchedValueId).toEqual(valueId)
}) })
test('jsonpatch replace: apply eson state', () => {
const json = {
a: 2,
b: 4
}
const patch = [
{
op: 'replace',
path: '/b',
value: {c: {d: 3}},
meta: {
state: {
'': { expanded: true },
'/c/d': { expanded: true }
}
}
}
]
const data = jsonToEson(json)
const result = patchEson(data, patch)
const patchedData = result.data
let expected = jsonToEson({
a: 2,
b: {c: {d: 3}}
})
expected = expandOne(expected, ['b'], true)
expected = expandOne(expected, ['b', 'c', 'd'], true)
assertDeepEqualEson(patchedData, expected)
})
test('jsonpatch copy', () => { test('jsonpatch copy', () => {
const json = { const json = {
arr: [1,2,3], arr: [1,2,3],