Skip to main content
Module

x/evt/lib/util/convertOperatorToStatelessFLambda.ts

­čĺžEventEmitter's typesafe replacement
Go to Latest
File

import type { Operator } from "../types/Operator.ts";
function encapsulateOpState<T, U>( statefulF╬╗Op: Operator.f╬╗.Stateful<T, U>): Operator.f╬╗.Stateless<T, U> {
let state: U = statefulF╬╗Op[1];
return (data, registerSideEffect) => {
const opResult = statefulF╬╗Op[0](data, state, registerSideEffect);
if (opResult !== null) { registerSideEffect(() => state = opResult[0]) }
return opResult;
};
}
function statelessOpToStatelessF╬╗<T, U>(op: Operator.Stateless<T, U>): Operator.f╬╗.Stateless<T, U> { return (data, registerSideEffect) => {
/* NOTE: Here, if the user is using TypeScript we should have readonly [U] or boolean * but users using vanilla JS can very well provide operators like: text => text.match(/^error/) * and expect things to work event if String.prototype.match returns a RegExpMatch array instead * of boolean. * Long story short we do our best to guess what the user meant with he's operator, if it was * intended to be a filter or a fλ. */ const opResult: any = (op as Operator.fλ.Stateless<T, U>)(data, registerSideEffect);
return ( opResult instanceof Object && !("input" in opResult) && //exclude String.prototype.match opResult.length === 1 ) ? opResult : !!opResult ? [data] : null;
}};
export function convertOperatorToStatelessF╬╗<T, U>( op: Operator<T, U>): Operator.f╬╗.Stateless<T, U> { return typeof op !== "function" ? encapsulateOpState(op) : statelessOpToStatelessF╬╗(op);}