import { identity } from './identity'; import { UnaryFunction } from '../types'; export function pipe(): typeof identity; export function pipe(fn1: UnaryFunction): UnaryFunction; export function pipe(fn1: UnaryFunction, fn2: UnaryFunction): UnaryFunction; export function pipe(fn1: UnaryFunction, fn2: UnaryFunction, fn3: UnaryFunction): UnaryFunction; export function pipe( fn1: UnaryFunction, fn2: UnaryFunction, fn3: UnaryFunction, fn4: UnaryFunction ): UnaryFunction; export function pipe( fn1: UnaryFunction, fn2: UnaryFunction, fn3: UnaryFunction, fn4: UnaryFunction, fn5: UnaryFunction ): UnaryFunction; export function pipe( fn1: UnaryFunction, fn2: UnaryFunction, fn3: UnaryFunction, fn4: UnaryFunction, fn5: UnaryFunction, fn6: UnaryFunction ): UnaryFunction; export function pipe( fn1: UnaryFunction, fn2: UnaryFunction, fn3: UnaryFunction, fn4: UnaryFunction, fn5: UnaryFunction, fn6: UnaryFunction, fn7: UnaryFunction ): UnaryFunction; export function pipe( fn1: UnaryFunction, fn2: UnaryFunction, fn3: UnaryFunction, fn4: UnaryFunction, fn5: UnaryFunction, fn6: UnaryFunction, fn7: UnaryFunction, fn8: UnaryFunction ): UnaryFunction; export function pipe( fn1: UnaryFunction, fn2: UnaryFunction, fn3: UnaryFunction, fn4: UnaryFunction, fn5: UnaryFunction, fn6: UnaryFunction, fn7: UnaryFunction, fn8: UnaryFunction, fn9: UnaryFunction ): UnaryFunction; export function pipe( fn1: UnaryFunction, fn2: UnaryFunction, fn3: UnaryFunction, fn4: UnaryFunction, fn5: UnaryFunction, fn6: UnaryFunction, fn7: UnaryFunction, fn8: UnaryFunction, fn9: UnaryFunction, ...fns: UnaryFunction[] ): UnaryFunction; /** * pipe() can be called on one or more functions, each of which can take one argument ("UnaryFunction") * and uses it to return a value. * It returns a function that takes one argument, passes it to the first UnaryFunction, and then * passes the result to the next one, passes that result to the next one, and so on. */ export function pipe(...fns: Array>): UnaryFunction { return pipeFromArray(fns); } /** @internal */ export function pipeFromArray(fns: Array>): UnaryFunction { if (fns.length === 0) { return identity as UnaryFunction; } if (fns.length === 1) { return fns[0]; } return function piped(input: T): R { return fns.reduce((prev: any, fn: UnaryFunction) => fn(prev), input as any); }; }