map-transform-cjs
Version:
MapTransform with CJS support
45 lines (44 loc) • 1.2 kB
JavaScript
// 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;
// src/operations/filter.ts
async function filterValue(values, filterFn, state) {
if (Array.isArray(values)) {
const results = [];
for (const value of values) {
if (await filterFn(value, state)) {
results.push(value);
}
}
return results;
} else {
const result = await filterFn(values, state);
return result ? values : void 0;
}
}
function filter(fn) {
return (options) => (next) => {
if (typeof fn !== "function") {
return async (state) => await next(state);
}
const fnWithOptions = fn(options);
return async (state) => {
const nextState = await next(state);
return setStateValue(
nextState,
await filterValue(getStateValue(nextState), fnWithOptions, nextState)
);
};
};
}
export {
filter as default
};
//# sourceMappingURL=filter.js.map