import type { Operator } from "./types/Operator.ts"import { id } from "https://deno.land/x/tsafe@v1.5.2/id.ts";import { compose } from "./util/compose.ts";import { typeGuard } from "https://deno.land/x/tsafe@v1.5.2/typeGuard.ts"import type { CtxLike, Handler } from "./types/index.ts";
export function matchAll() { return true; }
const canBeOperator = (p: undefined | CtxLike<any> | Operator<any, any>): boolean => { return ( p !== undefined && typeGuard<Operator<any, any>>(p, true) && ( typeof p === "function" || typeof p[0] === "function" ) );};
const defaultParams: Handler.PropsFromArgs<any, any> = { "op": matchAll, "ctx": undefined, "timeout": undefined, "callback": undefined};
export function parsePropsFromArgs<T>( inputs: readonly any[], methodName: "waitFor" | "attach*" | "pipe"): Handler.PropsFromArgs<T, any> {
type Out = Handler.PropsFromArgs<T, any>;
switch (methodName) { case "pipe": {
const getOpWrap = (ops: [Operator<T, any>, ...Operator<any, any>[]]) => ops.length === 0 ? {} : { "op": ops.length === 1 ? ops[0] : compose(...ops) } ;
if (canBeOperator(inputs[0])) {
return id<Out>({ ...defaultParams, ...getOpWrap(inputs as any) });
} else {
const [ctx, ...rest] = inputs;
return id<Out>({ ...defaultParams, ...(ctx !== undefined ? { ctx } : {}), ...getOpWrap(rest as any) });
}
} break;
case "waitFor": {
return parsePropsFromArgs( [ ...inputs.filter( (value, index) => !( index === inputs.length - 1 && value === undefined ) ), defaultParams.callback ], "attach*" );
} break; case "attach*": {
const n = inputs.length as 4 | 3 | 2 | 1 | 0;
switch (n) { case 4: {
const [p1, p2, p3, p4] = inputs;
return id<Out>({ ...defaultParams, "op": p1, "ctx": p2, "timeout": p3, "callback": p4 });
} case 3: {
const [p1, p2, p3] = inputs; if (typeof p2 === "number") {
const timeout: Out["timeout"] = p2; const callback: Out["callback"] = p3;
if (canBeOperator(p1)) { return id<Out>({ ...defaultParams, timeout, callback, "op": p1 });
} else {
return id<Out>({ ...defaultParams, timeout, callback, "ctx": p1 });
} } else { return id<Out>({ ...defaultParams, "op": p1, "ctx": p2, "callback": p3 });
}
} case 2: {
const [p1, p2] = inputs; if (typeof p1 === "number") { return id<Out>({ ...defaultParams, "timeout": p1, "callback": p2 }); } else { const callback: Out["callback"] = p2; if (canBeOperator(p1)) {
return id<Out>({ ...defaultParams, callback, "op": p1 });
} else {
return id<Out>({ ...defaultParams, callback, "ctx": p1 });
} }
} case 1: {
const [p] = inputs;
return id<Out>({ ...defaultParams, "callback": p });
} case 0: { return id<Out>({ ...defaultParams }); }
}
} break;
}
}