map-transform-cjs
Version:
MapTransform with CJS support
39 lines (37 loc) • 1.25 kB
JavaScript
// src/utils/xor.ts
function xor(a = false, b = false) {
return a ? !b : b;
}
// src/utils/stateHelpers.ts
var pushContext = (state, value) => ({
...state,
context: [...state.context, value]
});
var setStateValue = ({ untouched, ...state }, value, shouldPushContext = false) => shouldPushContext ? {
...pushContext(state, state.value),
value
} : { ...state, value };
var getStateValue = (state) => state.value;
var revFromState = (state, flip = false) => flip ? xor(state.rev, !state.flip) : xor(state.rev, state.flip);
// src/operations/transform.ts
function transform(fn, revFn) {
return (options) => {
if (typeof fn !== "function") {
throw new Error(
"Transform operation was called without a valid transformer function"
);
}
const fwdPipeline = fn(options);
const revPipeline = typeof revFn === "function" ? revFn(options) : fwdPipeline;
return (next) => async (state) => {
const nextState = await next(state);
const fn2 = revFromState(nextState) ? revPipeline : fwdPipeline;
const value = await fn2(getStateValue(nextState), nextState);
return setStateValue(nextState, value);
};
};
}
export {
transform as default
};
//# sourceMappingURL=transform.js.map