Some refactoring in applying META data
This commit is contained in:
parent
9db7fdf0e5
commit
e69d962ada
|
@ -337,8 +337,7 @@ export function sort (eson, path, order = null) {
|
|||
orderedProps.reverse()
|
||||
}
|
||||
|
||||
const orderedObject = cloneWithSymbols(object)
|
||||
orderedObject[META] = setIn(object[META], ['props'], orderedProps)
|
||||
const orderedObject = setIn(object, [META, 'props'], orderedProps)
|
||||
|
||||
// TODO: refactor into a set of move actions, so we keep eson state of the items
|
||||
|
||||
|
|
25
src/eson.js
25
src/eson.js
|
@ -101,25 +101,25 @@ export function transform (eson, callback, path = []) {
|
|||
|
||||
if (updated[META].type === 'Object') {
|
||||
let changed = false
|
||||
let updatedObj = {}
|
||||
let updatedChilds = {}
|
||||
for (let key in updated) {
|
||||
if (updated.hasOwnProperty(key)) {
|
||||
updatedObj[key] = transform(updated[key], callback, path.concat(key))
|
||||
changed = changed || (updatedObj[key] !== updated[key])
|
||||
updatedChilds[key] = transform(updated[key], callback, path.concat(key))
|
||||
changed = changed || (updatedChilds[key] !== updated[key])
|
||||
}
|
||||
}
|
||||
updatedObj[META] = updated[META]
|
||||
return changed ? updatedObj : updated
|
||||
updatedChilds[META] = updated[META]
|
||||
return changed ? updatedChilds : updated
|
||||
}
|
||||
else if (updated[META].type === 'Array') {
|
||||
let changed = false
|
||||
let updatedArr = []
|
||||
let updatedChilds = []
|
||||
for (let i = 0; i < updated.length; i++) {
|
||||
updatedArr[i] = transform(updated[i], callback, path.concat(String(i)))
|
||||
changed = changed || (updatedArr[i] !== updated[i])
|
||||
updatedChilds[i] = transform(updated[i], callback, path.concat(String(i)))
|
||||
changed = changed || (updatedChilds[i] !== updated[i])
|
||||
}
|
||||
updatedArr[META] = updated[META]
|
||||
return changed ? updatedArr : updated
|
||||
updatedChilds[META] = updated[META]
|
||||
return changed ? updatedChilds : updated
|
||||
}
|
||||
else { // eson[META].type === 'value'
|
||||
return updated
|
||||
|
@ -135,10 +135,7 @@ export function transform (eson, callback, path = []) {
|
|||
export function updatePaths(eson, path = []) {
|
||||
return transform(eson, function (value, path) {
|
||||
if (!isEqual(value[META].path, path)) {
|
||||
// TODO: extend setIn to support symbols
|
||||
let updatedValue = cloneWithSymbols(value)
|
||||
updatedValue[META] = setIn(value[META], ['path'], path)
|
||||
return updatedValue
|
||||
return setIn(value, [META, 'path'], path)
|
||||
}
|
||||
else {
|
||||
return value
|
||||
|
|
|
@ -136,8 +136,7 @@ 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)
|
||||
let newValue = setIn(value, [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') {
|
||||
|
@ -190,8 +189,8 @@ export function remove (data, path) {
|
|||
const index = parent[META].props.indexOf(prop)
|
||||
const nextProp = parent[META].props[index + 1] || null
|
||||
|
||||
let updatedParent = deleteIn(parent, [prop])
|
||||
updatedParent[META] = deleteIn(parent[META], ['props', index], parent[META].props)
|
||||
let updatedParent = deleteIn(parent, [prop]) // delete property itself
|
||||
updatedParent = deleteIn(updatedParent, [META, 'props', index]) // delete property from the props list
|
||||
|
||||
return {
|
||||
data: setIn(data, parentPath, updatePaths(updatedParent, parentPath)),
|
||||
|
@ -235,14 +234,9 @@ export function add (data, path, value, options) {
|
|||
const existingIndex = props.indexOf(prop)
|
||||
|
||||
if (existingIndex !== -1) {
|
||||
// replace existing item
|
||||
// update path
|
||||
// FIXME: also update value's id
|
||||
let newValue = updatePaths(cloneWithSymbols(value), path)
|
||||
newValue[META] = setIn(newValue[META], ['id'], oldValue[META].id)
|
||||
// console.log('copied id from existing value' + oldValue[META].id)
|
||||
|
||||
return setIn(parent, [prop], newValue)
|
||||
// replace existing item, keep existing id
|
||||
const newValue = setIn(value, [META, 'id'], oldValue[META].id)
|
||||
return setIn(parent, [prop], updatePaths(newValue, path))
|
||||
}
|
||||
else {
|
||||
// insert new item
|
||||
|
|
Loading…
Reference in New Issue