Some refactoring in applying META data

This commit is contained in:
jos 2017-12-17 21:33:45 +01:00
parent 9db7fdf0e5
commit e69d962ada
3 changed files with 18 additions and 28 deletions

View File

@ -337,8 +337,7 @@ export function sort (eson, path, order = null) {
orderedProps.reverse() orderedProps.reverse()
} }
const orderedObject = cloneWithSymbols(object) const orderedObject = setIn(object, [META, 'props'], orderedProps)
orderedObject[META] = setIn(object[META], ['props'], orderedProps)
// TODO: refactor into a set of move actions, so we keep eson state of the items // TODO: refactor into a set of move actions, so we keep eson state of the items

View File

@ -101,25 +101,25 @@ export function transform (eson, callback, path = []) {
if (updated[META].type === 'Object') { if (updated[META].type === 'Object') {
let changed = false let changed = false
let updatedObj = {} let updatedChilds = {}
for (let key in updated) { for (let key in updated) {
if (updated.hasOwnProperty(key)) { if (updated.hasOwnProperty(key)) {
updatedObj[key] = transform(updated[key], callback, path.concat(key)) updatedChilds[key] = transform(updated[key], callback, path.concat(key))
changed = changed || (updatedObj[key] !== updated[key]) changed = changed || (updatedChilds[key] !== updated[key])
} }
} }
updatedObj[META] = updated[META] updatedChilds[META] = updated[META]
return changed ? updatedObj : updated return changed ? updatedChilds : updated
} }
else if (updated[META].type === 'Array') { else if (updated[META].type === 'Array') {
let changed = false let changed = false
let updatedArr = [] let updatedChilds = []
for (let i = 0; i < updated.length; i++) { for (let i = 0; i < updated.length; i++) {
updatedArr[i] = transform(updated[i], callback, path.concat(String(i))) updatedChilds[i] = transform(updated[i], callback, path.concat(String(i)))
changed = changed || (updatedArr[i] !== updated[i]) changed = changed || (updatedChilds[i] !== updated[i])
} }
updatedArr[META] = updated[META] updatedChilds[META] = updated[META]
return changed ? updatedArr : updated return changed ? updatedChilds : updated
} }
else { // eson[META].type === 'value' else { // eson[META].type === 'value'
return updated return updated
@ -135,10 +135,7 @@ export function transform (eson, callback, path = []) {
export function updatePaths(eson, path = []) { export function updatePaths(eson, path = []) {
return transform(eson, function (value, path) { return transform(eson, function (value, path) {
if (!isEqual(value[META].path, path)) { if (!isEqual(value[META].path, path)) {
// TODO: extend setIn to support symbols return setIn(value, [META, 'path'], path)
let updatedValue = cloneWithSymbols(value)
updatedValue[META] = setIn(value[META], ['path'], path)
return updatedValue
} }
else { else {
return value return value

View File

@ -136,8 +136,7 @@ export function replace (data, path, value) {
const oldValue = getIn(data, path) const oldValue = getIn(data, path)
// keep the original id // keep the original id
let newValue = value let newValue = setIn(value, [META, 'id'], oldValue[META].id)
newValue = setIn(newValue, [META, 'id'], oldValue[META].id)
// FIXME: get the original expanded state of the copied value from JSON-Patch // FIXME: get the original expanded state of the copied value from JSON-Patch
if (newValue[META].type === 'Object' || newValue[META].type === 'Array') { 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 index = parent[META].props.indexOf(prop)
const nextProp = parent[META].props[index + 1] || null const nextProp = parent[META].props[index + 1] || null
let updatedParent = deleteIn(parent, [prop]) let updatedParent = deleteIn(parent, [prop]) // delete property itself
updatedParent[META] = deleteIn(parent[META], ['props', index], parent[META].props) updatedParent = deleteIn(updatedParent, [META, 'props', index]) // delete property from the props list
return { return {
data: setIn(data, parentPath, updatePaths(updatedParent, parentPath)), data: setIn(data, parentPath, updatePaths(updatedParent, parentPath)),
@ -235,14 +234,9 @@ export function add (data, path, value, options) {
const existingIndex = props.indexOf(prop) const existingIndex = props.indexOf(prop)
if (existingIndex !== -1) { if (existingIndex !== -1) {
// replace existing item // replace existing item, keep existing id
// update path const newValue = setIn(value, [META, 'id'], oldValue[META].id)
// FIXME: also update value's id return setIn(parent, [prop], updatePaths(newValue, path))
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)
} }
else { else {
// insert new item // insert new item