Skip to main content
Module

x/evt/lib/util/compose.ts

­čĺžEventEmitter's typesafe replacement
Go to Latest
File
import { convertOperatorToStatelessFλ } from "./convertOperatorToStatelessFLambda.ts";import { id } from "https://deno.land/x/tsafe@v1.4.1/id.ts";import type { Operator } from "../types/Operator.ts";
function f_o_g<A, B, C>( op1: Operator<A, B>, op2: Operator<B, C>): Operator.f╬╗.Stateless<A, C> {
const opAtoB = convertOperatorToStatelessF╬╗(op1);
const opBtoC = convertOperatorToStatelessF╬╗(op2);
return id<Operator.f╬╗.Stateless<A, C>>( (dataA, registerSideEffect) => {
const resultB = opAtoB(dataA, registerSideEffect);
if( !resultB ){ return null; }
const [dataB] = resultB;
const resultC = opBtoC(dataB, registerSideEffect);
if( !resultC ){ return resultC; }
return [resultC[0]];
} );}

export function compose<A, B, C>( op1: Operator.f╬╗<A, B>, op2: Operator.f╬╗<B, C>): Operator.f╬╗.Stateless<A, C>;export function compose<A, B, C extends B>( op1: Operator.f╬╗<A, B>, op2: (data: B) => data is C,): Operator.f╬╗.Stateless<A, C>;export function compose<A, B>( op1: Operator.f╬╗<A, B>, op2: (data: B) => boolean,): Operator.f╬╗.Stateless<A, B>;export function compose<A, B extends A, C>( op1: (data: A) => data is B, op2: Operator.f╬╗<B, C>): Operator.f╬╗.Stateless<A, B>;export function compose<A, B>( op1: (data: A) => boolean, op2: Operator.f╬╗<A, B>): Operator.f╬╗.Stateless<A, B>;export function compose<A, B extends A, C extends B>( op1: (data: A) => data is B, op2: (data: B) => data is C,): Operator.f╬╗.Stateless<A, C>;export function compose<A, B extends A>( op1: (data: A) => data is B, op2: (data: B) => boolean,): Operator.f╬╗.Stateless<A, B>;export function compose<A, B extends A>( op1: (data: A) => boolean, op2: (data: A) => data is B): Operator.f╬╗.Stateless<A, B>;export function compose<A>( op1: (data: A) => boolean, op2: (data: A) => boolean,): Operator.f╬╗.Stateless<A, A>;

export function compose<A, B, C, D>( op1: Operator.f╬╗<A, B>, op2: Operator.f╬╗<B, C>, op3: Operator.f╬╗<C, D>): Operator.f╬╗.Stateless<A, D>;
export function compose<A, B, C, D, E>( op1: Operator.f╬╗<A, B>, op2: Operator.f╬╗<B, C>, op3: Operator.f╬╗<C, D>, op4: Operator.f╬╗<D, E>): Operator.f╬╗.Stateless<A, E>;
export function compose<A, B, C, D, E>( op1: Operator.f╬╗<A, B>, op2: Operator.f╬╗<B, C>, op3: Operator.f╬╗<C, D>, op4: Operator.f╬╗<D, E>): Operator.f╬╗.Stateless<A, E>;
export function compose<A, B, C, D, E, F>( op1: Operator.f╬╗<A, B>, op2: Operator.f╬╗<B, C>, op3: Operator.f╬╗<C, D>, op4: Operator.f╬╗<D, E>, op5: Operator.f╬╗<E, F>): Operator.f╬╗.Stateless<A, F>;

export function compose<A, B, C>( op1: Operator<A, B>, op2: Operator<B, C>): Operator.f╬╗.Stateless<A, C>;
export function compose<A, B, C, D>( op1: Operator<A, B>, op2: Operator<B, C>, op3: Operator<C, D>): Operator.f╬╗.Stateless<A, D>;
export function compose<A, B, C, D, E>( op1: Operator<A, B>, op2: Operator<B, C>, op3: Operator<C, D>, op4: Operator<D, E>,): Operator.f╬╗.Stateless<A, E>;
export function compose<A, B, C, D, E, F>( op1: Operator<A, B>, op2: Operator<B, C>, op3: Operator<C, D>, op4: Operator<D, E>, op5: Operator<E, F>): Operator.f╬╗.Stateless<A, F>;

export function compose<T>( ...ops: [ Operator<T, any>, ...Operator<any, any>[] ]): Operator.f╬╗.Stateless<T, any>;
export function compose<T>( ...ops: [ Operator<T, any>, ...Operator<any, any>[] ]): Operator.f╬╗.Stateless<T, any> {
if (ops.length === 1) {
const [op] = ops;
return convertOperatorToStatelessF╬╗(op);
}
const [op1, op2, ...rest] = ops;
const op1_o_op2 = f_o_g(op1, op2);
if (rest.length === 0) { return op1_o_op2; }
return compose(op1_o_op2, ...rest);

}