From 456f1912fd60144b074910557e6435781215b593 Mon Sep 17 00:00:00 2001 From: Jos de Jong Date: Wed, 19 Aug 2020 21:37:15 +0200 Subject: [PATCH] Implement some logic to sort object keys --- src/components/modals/SortModal.svelte | 59 +++++++++++++++++++------- 1 file changed, 44 insertions(+), 15 deletions(-) diff --git a/src/components/modals/SortModal.svelte b/src/components/modals/SortModal.svelte index 33d73c7..1e25fc2 100644 --- a/src/components/modals/SortModal.svelte +++ b/src/components/modals/SortModal.svelte @@ -7,6 +7,7 @@ import Header from './Header.svelte' import { getNestedPaths } from '../../utils/arrayUtils.js' import { getIn, setIn } from '../../utils/immutabilityHelpers.js' + import { isObject } from '../../utils/typeUtils.js' export let json export let path @@ -39,18 +40,7 @@ } } - function handleSort () { - if (!selectedProperty || !selectedDirection) { - return - } - - // TODO: create a sortBy which returns a JSONPatch document - - // TODO: sort object keys when root is an object - - const property = selectedProperty.value - const direction = selectedDirection.value - + function sortArray (array, property, direction) { function comparator (a, b) { const valueA = getIn(a, property) const valueB = getIn(b, property) @@ -75,10 +65,49 @@ } // TODO: use lodash orderBy, split comparator and direction? - const sorted = root.slice() - sorted.sort(comparator) + const sortedArray = array.slice() + sortedArray.sort(comparator) + + return sortedArray + } + + function sortObjectKeys (object, direction) { + const keys = Object.keys(object) + keys.sort((keyA, keyB) => { + return direction * naturalSort(keyA, keyB) + }) + + const sortedObject = {} + keys.forEach(key => sortedObject[key] = object[key]) + + return sortedObject + } + + function handleSort () { + // TODO: create a sortBy which returns a JSONPatch document containing move operations + + if (Array.isArray(root)) { + if (!selectedProperty) { + return + } + + const property = selectedProperty.value + const direction = selectedDirection.value + const sorted = sortArray(root, property, direction) + + onSort(setIn(json, path, sorted)) + } else if (isObject(root)) { + const direction = selectedDirection.value + const sorted = sortObjectKeys(root, direction) + + // FIXME: the keys are now sorted, but the JSONEditor refuses to reorder when already rendered -> need to do a JSONPatch + console.log('sorted object keys:', Object.keys(sorted)) + + onSort(setIn(json, path, sorted)) + } else { + console.error('Cannot sort: no array or object') + } - onSort(setIn(json, path, sorted)) close() }