map-transform-cjs
Version:
MapTransform with CJS support
68 lines (65 loc) • 2.12 kB
JavaScript
;
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/filter.ts
var filter_exports = {};
__export(filter_exports, {
default: () => filter
});
module.exports = __toCommonJS(filter_exports);
// 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)
);
};
};
}
//# sourceMappingURL=filter.cjs.map