map-transform
Version:
Map and transform objects with mapping definitions
25 lines • 1.02 kB
JavaScript
import { defToOperation } from '../utils/definitionHelpers.js';
import { revFromState } from '../utils/stateHelpers.js';
const applyInDirection = (def, shouldRunRev) => (options) => (next) => {
const fn = defToOperation(def, options)(options)(next);
return async function applyFwdOrRev(state) {
return !!state.rev === shouldRunRev ? await fn(state) : await next(state);
};
};
export function fwd(def) {
return applyInDirection(def, false);
}
export function rev(def) {
return applyInDirection(def, true);
}
export function divide(fwdDef, revDef, honorFlip = false) {
return (options) => (next) => {
const fwdFn = defToOperation(fwdDef, options)(options)(next);
const revFn = defToOperation(revDef, options)(options)(next);
return async function applyDivide(state) {
const isRev = honorFlip ? revFromState(state) : !!state.rev;
return isRev ? await revFn(state) : await fwdFn(state);
};
};
}
//# sourceMappingURL=directionals.js.map