Ensure property key to be unique

This commit is contained in:
Jos de Jong 2020-07-08 13:19:40 +02:00
parent 2f393e5948
commit 16d3092670
2 changed files with 5 additions and 1 deletions

View File

@ -16,6 +16,7 @@
import Icon from 'svelte-awesome'
import { faCaretDown, faCaretRight } from '@fortawesome/free-solid-svg-icons'
import classnames from 'classnames'
import { findUniqueName } from './utils/stringUtils.js'
import { isUrl, stringConvert, valueType } from './utils/typeUtils'
import { compileJSONPointer } from './utils/jsonPointer'
@ -107,12 +108,14 @@
const updateKeyDebounced = debounce(updateKey, DEBOUNCE_DELAY)
function handleUpdateKey (oldKey, newKey) {
const newKeyUnique = findUniqueName(newKey, value)
const index = props.findIndex(prop => prop.key === oldKey)
const nextKeys = (index !== -1)
? props.slice(index + 1).map(prop => prop.key)
: []
onPatch(rename(path, oldKey, newKey, nextKeys))
onPatch(rename(path, oldKey, newKeyUnique, nextKeys))
}
function handleKeyInput (event) {

View File

@ -99,6 +99,7 @@ export function append (json, path, values) { // TODO: find a better name and d
/**
* Rename an object key
* Not applicable to arrays
*
* @param {Path} parentPath
* @param {string} oldKey