UNPKG

map-transform

Version:

Map and transform objects with mapping definitions

58 lines 2.23 kB
import xor from './xor.js'; export const getLastContext = (state) => state.context[state.context.length - 1]; export const removeLastContext = (state) => ({ ...state, context: state.context.slice(0, -1), }); export const pushContext = (state, value) => ({ ...state, context: [...state.context, value], }); export const popContext = (state) => ({ ...state, context: state.context.slice(0, -1), value: state.context[state.context.length - 1], }); export const getRootFromState = (state) => state.context.length === 0 ? state.value : state.context[0]; export const getTargetFromState = (state) => state.target; export function setTargetOnState(state, target) { return { ...state, target, }; } export const setStateValue = ({ untouched, ...state }, value, shouldPushContext = false) => shouldPushContext ? { ...pushContext(state, state.value), value, } : { ...state, value }; export const getStateValue = (state) => state.value; export const setValueFromState = (state, { value }, shouldPushContext = false) => setStateValue(state, value, shouldPushContext); export const isNonvalue = (value, nonvalues = [undefined]) => nonvalues.includes(value); export const isNonvalueState = (state, nonvalues = [undefined]) => isNonvalue(state.value, nonvalues); export const markAsUntouched = (state) => ({ ...state, untouched: true }); export const clearUntouched = ({ untouched, ...state }) => state; export const isUntouched = ({ untouched }) => !!untouched; export const populateState = (data, { rev = false, noDefaults = false, target = undefined }) => ({ context: [], value: data, target, rev, noDefaults, }); export const goForward = (state) => state.rev || state.flip ? { ...state, rev: false, flip: false, } : state; export const flipState = (state, flip = true) => ({ ...state, flip: xor(state.flip, flip), }); export const stopIteration = (state) => ({ ...state, iterate: false }); export const noopNext = async (state) => state; export const revFromState = (state, flip = false) => flip ? xor(state.rev, !state.flip) : xor(state.rev, state.flip); //# sourceMappingURL=stateHelpers.js.map