From 9db7fdf0e5797f38605bc37a2f916a06c699c10a Mon Sep 17 00:00:00 2001 From: jos Date: Sun, 17 Dec 2017 21:10:07 +0100 Subject: [PATCH] Fixed keeping id's intact when replacing a value --- src/patchEson.js | 11 ++++++++++- test/patchEson.test.js | 29 ++++++++++++++--------------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/patchEson.js b/src/patchEson.js index 2ab27cf..f004f35 100644 --- a/src/patchEson.js +++ b/src/patchEson.js @@ -135,8 +135,17 @@ export function patchEson (eson, patch, expand = expandAll) { export function replace (data, path, value) { const oldValue = getIn(data, path) + // keep the original id + let newValue = value + newValue = setIn(newValue, [META, 'id'], oldValue[META].id) + + // FIXME: get the original expanded state of the copied value from JSON-Patch + if (newValue[META].type === 'Object' || newValue[META].type === 'Array') { + newValue = setIn(newValue, [META, 'expanded'], true) + } + return { - data: setIn(data, path, value), + data: setIn(data, path, newValue), revert: [{ op: 'replace', path: compileJSONPointer(path), diff --git a/test/patchEson.test.js b/test/patchEson.test.js index 09a7a4d..b27dff1 100644 --- a/test/patchEson.test.js +++ b/test/patchEson.test.js @@ -153,21 +153,20 @@ test('jsonpatch replace', t => { ]) }) -// // FIXME: keep ids intact -// test('jsonpatch replace (keep ids intact)', t => { -// const json = { value: 42 } -// const patch = [ -// {op: 'replace', path: '/value', value: 100} -// ] -// -// const data = jsonToEson(json) -// const valueId = data.value[META].id -// -// const patchedData = patchEson(data, patch).data -// const patchedValueId = patchedData.value[META].id -// -// t.is(patchedValueId, valueId) -// }) +test('jsonpatch replace (keep ids intact)', t => { + const json = { value: 42 } + const patch = [ + {op: 'replace', path: '/value', value: 100} + ] + + const data = jsonToEson(json) + const valueId = data.value[META].id + + const patchedData = patchEson(data, patch).data + const patchedValueId = patchedData.value[META].id + + t.is(patchedValueId, valueId) +}) test('jsonpatch copy', t => { const json = {