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) {
|
||||
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),
|
||||
|
|
|
@ -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 = {
|
||||
|
|
Loading…
Reference in New Issue