Created helper function to find a unique name (not yet used)
This commit is contained in:
parent
123b0c7cb4
commit
71c38f07af
|
@ -41,20 +41,16 @@
|
|||
|
||||
// set json
|
||||
document.getElementById('setJSON').onclick = function () {
|
||||
console.time('set')
|
||||
editor.set(largeJSON, {
|
||||
expand: function (path) {
|
||||
return true
|
||||
}
|
||||
});
|
||||
console.timeEnd('set')
|
||||
};
|
||||
|
||||
// get json
|
||||
document.getElementById('getJSON').onclick = function () {
|
||||
console.time('get')
|
||||
const json = editor.get();
|
||||
console.timeEnd('get')
|
||||
alert(JSON.stringify(json, null, 2));
|
||||
};
|
||||
|
||||
|
|
|
@ -21,6 +21,8 @@ function jsoneditor (container, options) {
|
|||
_options: options,
|
||||
_component: component,
|
||||
|
||||
// TODO: implement setMode
|
||||
|
||||
/**
|
||||
* Set JSON object in editor
|
||||
* @param {Object | Array | string | number | boolean | null} json JSON data
|
||||
|
@ -80,8 +82,9 @@ function jsoneditor (container, options) {
|
|||
component.collapse(callback)
|
||||
},
|
||||
|
||||
// TODO: implement destroy
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: use export default jsoneditor, doesn't work out of the box in webpack
|
||||
module.exports = jsoneditor
|
||||
|
|
|
@ -3,11 +3,14 @@
|
|||
* All functions are pure and don't mutate the JSONData.
|
||||
*/
|
||||
|
||||
import { cloneDeep } from './utils/objectUtils'
|
||||
import { cloneDeep, isObject } from './utils/objectUtils'
|
||||
import { setIn, updateIn, getIn, deleteIn } from './utils/immutabilityHelpers'
|
||||
import { compareAsc, compareDesc } from './utils/arrayUtils'
|
||||
import { stringConvert } from './utils/typeUtils'
|
||||
import { isObject } from './utils/objectUtils'
|
||||
import { findUniqueName } from './utils/stringUtils'
|
||||
|
||||
// TODO: rewrite the functions into jsonpatch functions, including a function `patch`
|
||||
|
||||
|
||||
/**
|
||||
* Change the value of a property or item
|
||||
|
@ -35,6 +38,10 @@ export function changeValue (data, path, value) {
|
|||
export function changeProperty (data, path, oldProp, newProp) {
|
||||
// console.log('changeProperty', path, oldProp, newProp)
|
||||
|
||||
if (oldProp === newProp) {
|
||||
return data
|
||||
}
|
||||
|
||||
const dataPath = toDataPath(data, path)
|
||||
const object = getIn(data, dataPath)
|
||||
const index = object.props.findIndex(p => p.name === oldProp)
|
||||
|
|
|
@ -91,3 +91,22 @@ export function escapeJSON (text) {
|
|||
|
||||
return escaped
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a unique name. Suffix the name with ' (copy)', '(copy 2)', etc
|
||||
* until a unique name is found
|
||||
* @param {string} name
|
||||
* @param {Array.<string>} invalidNames
|
||||
*/
|
||||
export function findUniqueName (name, invalidNames) {
|
||||
let validName = name
|
||||
let i = 1
|
||||
|
||||
while (invalidNames.includes(validName)) {
|
||||
const copy = 'copy' + (i > 1 ? (' ' + i) : '')
|
||||
validName = `${name} (${copy})`
|
||||
i++
|
||||
}
|
||||
|
||||
return validName
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
import test from 'ava';
|
||||
import { findUniqueName } from '../src/utils/stringUtils'
|
||||
|
||||
test('findUniqueName', t => {
|
||||
|
||||
t.is(findUniqueName('other', ['a', 'b', 'c']), 'other')
|
||||
t.is(findUniqueName('b', ['a', 'b', 'c']), 'b (copy)')
|
||||
t.is(findUniqueName('b', ['a', 'b', 'c', 'b (copy)']), 'b (copy 2)')
|
||||
t.is(findUniqueName('b', ['a', 'b', 'c', 'b (copy)', 'b (copy 2)']), 'b (copy 3)')
|
||||
})
|
Loading…
Reference in New Issue