UNPKG

map-transform-cjs

Version:
62 lines (58 loc) 2.18 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/operations/transform.ts var transform_exports = {}; __export(transform_exports, { default: () => transform }); module.exports = __toCommonJS(transform_exports); // 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); }; }; } //# sourceMappingURL=transform.cjs.map