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) {
case 'add': {
let newValue = jsonToEson(action.value, path)
if (options && options.state) {
newValue = applyEsonState(newValue, options.state)
}
// FIXME: apply options.type
const result = add(updatedEson, path, newValue, options)
const newValue = jsonToEson(action.value, path)
const newValueWithState = (options && options.state)
? applyEsonState(newValue, options.state)
: newValue
const result = add(updatedEson, path, newValueWithState, options)
updatedEson = result.data
revert = result.revert.concat(revert)
@ -57,9 +56,10 @@ export function patchEson (eson, patch, expand = expandAll) {
case 'replace': {
const newValue = jsonToEson(action.value, path)
// FIXME: apply expanded state
// FIXME: apply options.type
const result = replace(updatedEson, path, newValue)
const newValueWithState = (options && options.state)
? applyEsonState(newValue, options.state)
: newValue
const result = replace(updatedEson, path, newValueWithState)
updatedEson = result.data
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 = {
a: 2
}
@ -202,6 +202,40 @@ test('jsonpatch replace (keep ids intact)', () => {
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', () => {
const json = {
arr: [1,2,3],