From da2f912d6d70cf0b7f82fb9aba2a9c77de2f4237 Mon Sep 17 00:00:00 2001 From: Jos de Jong Date: Wed, 22 Jul 2020 16:57:30 +0200 Subject: [PATCH] Rename file to `stateUtils.js` --- src/JSONEditor.svelte | 10 ++++---- src/selection.test.js | 4 ++-- src/utils/{syncState.js => stateUtils.js} | 24 ++++++++++--------- .../{syncState.test.js => stateUtils.test.js} | 6 ++--- 4 files changed, 23 insertions(+), 21 deletions(-) rename src/utils/{syncState.js => stateUtils.js} (66%) rename src/utils/{syncState.test.js => stateUtils.test.js} (89%) diff --git a/src/JSONEditor.svelte b/src/JSONEditor.svelte index 4065493..0326984 100644 --- a/src/JSONEditor.svelte +++ b/src/JSONEditor.svelte @@ -36,7 +36,7 @@ import { immutableJSONPatch } from './utils/immutableJSONPatch' import { isNumber, initial, last, cloneDeep } from 'lodash-es' import jump from './assets/jump.js/src/jump.js' - import { syncState } from './utils/syncState.js' + import { stateUtils } from './utils/stateUtils.js' import { getNextKeys, patchProps } from './utils/updateProps.js' let divContents @@ -53,7 +53,7 @@ $: hasSelectionContents = selection != null && selection.paths != null $: hasClipboardContents = clipboard != null && selection != null - $: state = syncState(doc, state, [], (path) => path.length < 1) + $: state = stateUtils(doc, state, [], (path) => path.length < 1) let showSearch = false let searchText = '' @@ -66,11 +66,11 @@ let historyState = history.getState() export function expand (callback = () => true) { - state = syncState(doc, state, [], callback, true) + state = stateUtils(doc, state, [], callback, true) } export function collapse (callback = () => false) { - state = syncState(doc, state, [], callback, true) + state = stateUtils(doc, state, [], callback, true) } export function get() { @@ -329,7 +329,7 @@ function handleExpand (path, expanded, recursive = false) { if (recursive) { state = updateIn(state, path, (childState) => { - return syncState(getIn(doc, path), childState, [], () => expanded, true) + return stateUtils(getIn(doc, path), childState, [], () => expanded, true) }) } else { state = setIn(state, path.concat(STATE_EXPANDED), expanded) diff --git a/src/selection.test.js b/src/selection.test.js index 6b1037a..7d6a699 100644 --- a/src/selection.test.js +++ b/src/selection.test.js @@ -1,6 +1,6 @@ import assert from 'assert' import { expandSelection } from './selection.js' -import { syncState } from './utils/syncState.js' +import { stateUtils } from './utils/stateUtils.js' describe ('selection', () => { const doc = { @@ -11,7 +11,7 @@ describe ('selection', () => { "nill": null, "bool": false } - const state = syncState(doc, undefined, [], () => true) + const state = stateUtils(doc, undefined, [], () => true) it('should expand a selection (object)', () => { const start = ['obj', 'arr', '2', 'last'] diff --git a/src/utils/syncState.js b/src/utils/stateUtils.js similarity index 66% rename from src/utils/syncState.js rename to src/utils/stateUtils.js index f995e54..efed752 100644 --- a/src/utils/syncState.js +++ b/src/utils/stateUtils.js @@ -8,32 +8,34 @@ import { isObject, isObjectOrArray } from './typeUtils.js' import { updateProps } from './updateProps.js' /** - * @param {JSON} document + * Sync a state object with the doc it belongs to: update props, limit, and expanded state + * + * @param {JSON} doc * @param {JSON | undefined} state * @param {Path} path * @param {function (path: Path) : boolean} expand * @param {boolean} [forceRefresh=false] if true, force refreshing the expanded state * @returns {JSON | undefined} */ -export function syncState (document, state = undefined, path, expand, forceRefresh = false) { +export function stateUtils (doc, state = undefined, path, expand, forceRefresh = false) { // TODO: this function can be made way more efficient if we pass prevState: // when immutable, we can simply be done already when the state === prevState - if (isObject(document)) { + if (isObject(doc)) { const updatedState = {} - updatedState[STATE_PROPS] = updateProps(document, state && state[STATE_PROPS]) + updatedState[STATE_PROPS] = updateProps(doc, state && state[STATE_PROPS]) updatedState[STATE_EXPANDED] = (state && !forceRefresh) ? state[STATE_EXPANDED] : expand(path) if (updatedState[STATE_EXPANDED]) { - Object.keys(document).forEach(key => { - const childDocument = document[key] + Object.keys(doc).forEach(key => { + const childDocument = doc[key] if (isObjectOrArray(childDocument)) { const childState = state && state[key] - updatedState[key] = syncState(childDocument, childState, path.concat(key), expand, forceRefresh) + updatedState[key] = stateUtils(childDocument, childState, path.concat(key), expand, forceRefresh) } }) } @@ -41,7 +43,7 @@ export function syncState (document, state = undefined, path, expand, forceRefre return updatedState } - if (Array.isArray(document)) { + if (Array.isArray(doc)) { const updatedState = [] updatedState[STATE_EXPANDED] = (state && !forceRefresh) @@ -54,11 +56,11 @@ export function syncState (document, state = undefined, path, expand, forceRefre : DEFAULT_LIMIT if (updatedState[STATE_EXPANDED]) { - for (let i = 0; i < Math.min(document.length, updatedState[STATE_LIMIT]); i++) { - const childDocument = document[i] + for (let i = 0; i < Math.min(doc.length, updatedState[STATE_LIMIT]); i++) { + const childDocument = doc[i] if (isObjectOrArray(childDocument)) { const childState = state && state[i] - updatedState[i] = syncState(childDocument, childState, path.concat(i), expand, forceRefresh) + updatedState[i] = stateUtils(childDocument, childState, path.concat(i), expand, forceRefresh) } } } diff --git a/src/utils/syncState.test.js b/src/utils/stateUtils.test.js similarity index 89% rename from src/utils/syncState.test.js rename to src/utils/stateUtils.test.js index 0bccd01..7d8a297 100644 --- a/src/utils/syncState.test.js +++ b/src/utils/stateUtils.test.js @@ -5,7 +5,7 @@ import { STATE_LIMIT, STATE_PROPS } from '../constants.js' -import { syncState } from './syncState.js' +import { stateUtils } from './stateUtils.js' describe('syncState', () => { it('syncState', () => { @@ -19,7 +19,7 @@ describe('syncState', () => { return path.length <= 1 } - const state = syncState(document, undefined, [], expand) + const state = stateUtils(document, undefined, [], expand) const expectedState = {} expectedState[STATE_EXPANDED] = true @@ -46,5 +46,5 @@ describe('syncState', () => { assert.deepStrictEqual(state, expectedState) }) - // TODO: write more unit tests for syncState + // TODO: write more unit tests for stateUtils })