Rename file to `stateUtils.js`

This commit is contained in:
Jos de Jong 2020-07-22 16:57:30 +02:00
parent 7d67ecc4bc
commit da2f912d6d
4 changed files with 23 additions and 21 deletions

View File

@ -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)

View File

@ -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']

View File

@ -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)
}
}
}

View File

@ -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
})