Fix appending `... (copy)` suffix when replacing keys with new keys having the same name(s)
This commit is contained in:
parent
fb3a9cdf36
commit
89fc4070a2
|
@ -1,6 +1,4 @@
|
|||
import first from 'lodash/first'
|
||||
import initial from 'lodash/initial'
|
||||
import last from 'lodash/last'
|
||||
import { first, initial, last, pickBy } from 'lodash-es'
|
||||
import { getIn } from './utils/immutabilityHelpers'
|
||||
import { compileJSONPointer } from './utils/jsonPointer'
|
||||
import { findUniqueName } from './utils/stringUtils'
|
||||
|
@ -149,13 +147,18 @@ export function replace (json, paths, values, nextKeys) { // TODO: find a bette
|
|||
]
|
||||
}
|
||||
else { // parent is Object
|
||||
// if we're going to replace an existing object with key "a" with a new
|
||||
// key "a", we must not create a new unique name "a (copy)".
|
||||
const removeKeys = new Set(paths.map(path => last(path)))
|
||||
const parentWithoutRemovedKeys = pickBy(parent, (value, key) => !removeKeys.has(key))
|
||||
|
||||
return [
|
||||
// remove operations
|
||||
...removeAll(paths),
|
||||
|
||||
// insert operations
|
||||
...values.map(entry => {
|
||||
const newProp = findUniqueName(entry.key, parent)
|
||||
const newProp = findUniqueName(entry.key, parentWithoutRemovedKeys)
|
||||
return {
|
||||
op: 'add',
|
||||
path: compileJSONPointer(parentPath.concat(newProp)),
|
||||
|
|
|
@ -78,17 +78,21 @@ export function patchProps (state, operations) {
|
|||
}
|
||||
|
||||
if (operation.op === 'add') {
|
||||
const path = parseJSONPointer(operation.path)
|
||||
const parentPath = initial(path)
|
||||
const pathTo = parseJSONPointer(operation.path)
|
||||
const parentPath = initial(pathTo)
|
||||
const key = last(pathTo)
|
||||
const props = getIn(updatedState, parentPath.concat(STATE_PROPS))
|
||||
if (props) {
|
||||
const newProp = {
|
||||
id: uniqueId(),
|
||||
key: last(path)
|
||||
}
|
||||
const updatedProps = insertAt(props, [props.length], newProp)
|
||||
const index = props.findIndex(item => item.key === key)
|
||||
if (index === -1) {
|
||||
const newProp = {
|
||||
id: uniqueId(),
|
||||
key
|
||||
}
|
||||
const updatedProps = insertAt(props, [props.length], newProp)
|
||||
|
||||
updatedState = setIn(updatedState, parentPath.concat([STATE_PROPS]), updatedProps)
|
||||
updatedState = setIn(updatedState, parentPath.concat([STATE_PROPS]), updatedProps)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue