diff --git a/src/develop.html b/src/develop.html index 58a9a14..723da7b 100644 --- a/src/develop.html +++ b/src/develop.html @@ -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)); }; diff --git a/src/index.js b/src/index.js index 7a2ecbc..132ad68 100644 --- a/src/index.js +++ b/src/index.js @@ -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 diff --git a/src/jsonData.js b/src/jsonData.js index 3c5bdde..a28987e 100644 --- a/src/jsonData.js +++ b/src/jsonData.js @@ -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) diff --git a/src/utils/stringUtils.js b/src/utils/stringUtils.js index d448688..378f4ad 100644 --- a/src/utils/stringUtils.js +++ b/src/utils/stringUtils.js @@ -90,4 +90,23 @@ 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.} 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 } \ No newline at end of file diff --git a/test/stringUtils.test.js b/test/stringUtils.test.js new file mode 100644 index 0000000..859bef6 --- /dev/null +++ b/test/stringUtils.test.js @@ -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)') +}) \ No newline at end of file