Fixed keeping id's intact when replacing a value
This commit is contained in:
parent
dd22b97eb0
commit
9db7fdf0e5
|
@ -135,8 +135,17 @@ export function patchEson (eson, patch, expand = expandAll) {
|
||||||
export function replace (data, path, value) {
|
export function replace (data, path, value) {
|
||||||
const oldValue = getIn(data, path)
|
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 {
|
return {
|
||||||
data: setIn(data, path, value),
|
data: setIn(data, path, newValue),
|
||||||
revert: [{
|
revert: [{
|
||||||
op: 'replace',
|
op: 'replace',
|
||||||
path: compileJSONPointer(path),
|
path: compileJSONPointer(path),
|
||||||
|
|
|
@ -153,21 +153,20 @@ test('jsonpatch replace', t => {
|
||||||
])
|
])
|
||||||
})
|
})
|
||||||
|
|
||||||
// // FIXME: keep ids intact
|
test('jsonpatch replace (keep ids intact)', t => {
|
||||||
// test('jsonpatch replace (keep ids intact)', t => {
|
const json = { value: 42 }
|
||||||
// const json = { value: 42 }
|
const patch = [
|
||||||
// const patch = [
|
{op: 'replace', path: '/value', value: 100}
|
||||||
// {op: 'replace', path: '/value', value: 100}
|
]
|
||||||
// ]
|
|
||||||
//
|
const data = jsonToEson(json)
|
||||||
// const data = jsonToEson(json)
|
const valueId = data.value[META].id
|
||||||
// const valueId = data.value[META].id
|
|
||||||
//
|
const patchedData = patchEson(data, patch).data
|
||||||
// const patchedData = patchEson(data, patch).data
|
const patchedValueId = patchedData.value[META].id
|
||||||
// const patchedValueId = patchedData.value[META].id
|
|
||||||
//
|
t.is(patchedValueId, valueId)
|
||||||
// t.is(patchedValueId, valueId)
|
})
|
||||||
// })
|
|
||||||
|
|
||||||
test('jsonpatch copy', t => {
|
test('jsonpatch copy', t => {
|
||||||
const json = {
|
const json = {
|
||||||
|
|
Loading…
Reference in New Issue