From ccf89162b7cdd1d2bde51a8e87c6a99938471dc9 Mon Sep 17 00:00:00 2001 From: jos Date: Fri, 29 Dec 2017 21:40:23 +0100 Subject: [PATCH] Implement apply eson state with jsonpatch replace --- src/jsoneditor/patchEson.js | 18 ++++++++-------- src/jsoneditor/patchEson.test.js | 36 +++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/jsoneditor/patchEson.js b/src/jsoneditor/patchEson.js index 5d94b84..a37a2f7 100644 --- a/src/jsoneditor/patchEson.js +++ b/src/jsoneditor/patchEson.js @@ -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) diff --git a/src/jsoneditor/patchEson.test.js b/src/jsoneditor/patchEson.test.js index 2709a76..a2fb51d 100644 --- a/src/jsoneditor/patchEson.test.js +++ b/src/jsoneditor/patchEson.test.js @@ -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],