map-transform
Version:
Map and transform objects with mapping definitions
29 lines • 956 B
JavaScript
import { getStateValue, setStateValue } from '../utils/stateHelpers.js';
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 : undefined;
}
}
export default 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));
};
};
}
//# sourceMappingURL=filter.js.map