diff --git a/src/jsoneditor/actions.test.js b/src/jsoneditor/actions.test.js index 74ad044..07d3d0c 100644 --- a/src/jsoneditor/actions.test.js +++ b/src/jsoneditor/actions.test.js @@ -23,9 +23,6 @@ it('sort root Array (json)', () => { const sorted2 = immutableJSONPatch(json, sort(json, [], 'asc')).json const sorted3 = immutableJSONPatch(json, sort(json, [], 'desc')).json - console.log(json) - console.log(sorted1) - assertEqualEson(json, [1,3, 2]) // should be untouched assertEqualEson(sorted1, [1,2,3]) assertEqualEson(sorted2, [1,2,3]) diff --git a/src/jsoneditor/eson.js b/src/jsoneditor/eson.js index ede3680..86169f7 100644 --- a/src/jsoneditor/eson.js +++ b/src/jsoneditor/eson.js @@ -428,13 +428,14 @@ export function findSelectionIndices (root, rootPath, selection) { const end = (selection.after || selection.inside || selection.end)[rootPath.length] // if no object we assume it's an Array - const props = Object.keys(root).sort(naturalSort) // TODO: create a util function getSortedProps - const startIndex = root[TYPE] === 'object' ? props.indexOf(start) : parseInt(start, 10) - const endIndex = root[TYPE] === 'object' ? props.indexOf(end) : parseInt(end, 10) + // TODO: create a util function getSortedProps, cache results? + const rootIsObject = getType(root) === 'object' + const props = rootIsObject ? Object.keys(root).sort(naturalSort) : undefined + const startIndex = rootIsObject ? props.indexOf(start) : parseInt(start, 10) + const endIndex = rootIsObject ? props.indexOf(end) : parseInt(end, 10) const minIndex = Math.min(startIndex, endIndex) - const maxIndex = Math.max(startIndex, endIndex) + - ((selection.after || selection.inside) ? 0 : 1) // include max index itself + const maxIndex = Math.max(startIndex, endIndex) + ((selection.after || selection.inside) ? 0 : 1) // include max index itself return { minIndex, maxIndex } } @@ -519,8 +520,9 @@ export function pathsFromSelection (eson, selection) { const { minIndex, maxIndex } = findSelectionIndices(root, rootPath, selection) - if (root[TYPE] === 'object') { + if (getType(root) === 'object') { const props = Object.keys(root).sort(naturalSort) // TODO: create a util function getSortedProps + return times(maxIndex - minIndex, i => rootPath.concat(props[i + minIndex])) } else { // root[TYPE] === 'array' @@ -550,6 +552,10 @@ export function getType (any) { return 'undefined' } + if (any && any[TYPE]) { + return any[TYPE] + } + if (Array.isArray(any)) { return 'array' } diff --git a/src/jsoneditor/eson.test.js b/src/jsoneditor/eson.test.js index a4410b0..cb47f59 100644 --- a/src/jsoneditor/eson.test.js +++ b/src/jsoneditor/eson.test.js @@ -447,12 +447,12 @@ test('selection (node)', () => { test('pathsFromSelection (object)', () => { const json = { + "bool": false, + "nill": null, "obj": { "arr": [1,2, {"first":3,"last":4}] }, "str": "hello world", - "nill": null, - "bool": false } const selection = { start: ['obj', 'arr', '2', 'last'], @@ -460,9 +460,8 @@ test('pathsFromSelection (object)', () => { } expect(pathsFromSelection(json, selection)).toEqual([ - ['obj'], - ['str'], - ['nill'] + ['nill'], + ['obj'] ]) })