1 | import { noop } from './noop';
|
---|
2 | import { identity } from './identity';
|
---|
3 | import { UnaryFunction } from '../types';
|
---|
4 |
|
---|
5 | /* tslint:disable:max-line-length */
|
---|
6 | export function pipe<T>(): UnaryFunction<T, T>;
|
---|
7 | export function pipe<T, A>(fn1: UnaryFunction<T, A>): UnaryFunction<T, A>;
|
---|
8 | export function pipe<T, A, B>(fn1: UnaryFunction<T, A>, fn2: UnaryFunction<A, B>): UnaryFunction<T, B>;
|
---|
9 | export function pipe<T, A, B, C>(fn1: UnaryFunction<T, A>, fn2: UnaryFunction<A, B>, fn3: UnaryFunction<B, C>): UnaryFunction<T, C>;
|
---|
10 | export function pipe<T, A, B, C, D>(fn1: UnaryFunction<T, A>, fn2: UnaryFunction<A, B>, fn3: UnaryFunction<B, C>, fn4: UnaryFunction<C, D>): UnaryFunction<T, D>;
|
---|
11 | export function pipe<T, A, B, C, D, E>(fn1: UnaryFunction<T, A>, fn2: UnaryFunction<A, B>, fn3: UnaryFunction<B, C>, fn4: UnaryFunction<C, D>, fn5: UnaryFunction<D, E>): UnaryFunction<T, E>;
|
---|
12 | export function pipe<T, A, B, C, D, E, F>(fn1: UnaryFunction<T, A>, fn2: UnaryFunction<A, B>, fn3: UnaryFunction<B, C>, fn4: UnaryFunction<C, D>, fn5: UnaryFunction<D, E>, fn6: UnaryFunction<E, F>): UnaryFunction<T, F>;
|
---|
13 | export function pipe<T, A, B, C, D, E, F, G>(fn1: UnaryFunction<T, A>, fn2: UnaryFunction<A, B>, fn3: UnaryFunction<B, C>, fn4: UnaryFunction<C, D>, fn5: UnaryFunction<D, E>, fn6: UnaryFunction<E, F>, fn7: UnaryFunction<F, G>): UnaryFunction<T, G>;
|
---|
14 | export function pipe<T, A, B, C, D, E, F, G, H>(fn1: UnaryFunction<T, A>, fn2: UnaryFunction<A, B>, fn3: UnaryFunction<B, C>, fn4: UnaryFunction<C, D>, fn5: UnaryFunction<D, E>, fn6: UnaryFunction<E, F>, fn7: UnaryFunction<F, G>, fn8: UnaryFunction<G, H>): UnaryFunction<T, H>;
|
---|
15 | export function pipe<T, A, B, C, D, E, F, G, H, I>(fn1: UnaryFunction<T, A>, fn2: UnaryFunction<A, B>, fn3: UnaryFunction<B, C>, fn4: UnaryFunction<C, D>, fn5: UnaryFunction<D, E>, fn6: UnaryFunction<E, F>, fn7: UnaryFunction<F, G>, fn8: UnaryFunction<G, H>, fn9: UnaryFunction<H, I>): UnaryFunction<T, I>;
|
---|
16 | export function pipe<T, A, B, C, D, E, F, G, H, I>(fn1: UnaryFunction<T, A>, fn2: UnaryFunction<A, B>, fn3: UnaryFunction<B, C>, fn4: UnaryFunction<C, D>, fn5: UnaryFunction<D, E>, fn6: UnaryFunction<E, F>, fn7: UnaryFunction<F, G>, fn8: UnaryFunction<G, H>, fn9: UnaryFunction<H, I>, ...fns: UnaryFunction<any, any>[]): UnaryFunction<T, {}>;
|
---|
17 | /* tslint:enable:max-line-length */
|
---|
18 |
|
---|
19 | export function pipe(...fns: Array<UnaryFunction<any, any>>): UnaryFunction<any, any> {
|
---|
20 | return pipeFromArray(fns);
|
---|
21 | }
|
---|
22 |
|
---|
23 | /** @internal */
|
---|
24 | export function pipeFromArray<T, R>(fns: Array<UnaryFunction<T, R>>): UnaryFunction<T, R> {
|
---|
25 | if (fns.length === 0) {
|
---|
26 | return identity as UnaryFunction<any, any>;
|
---|
27 | }
|
---|
28 |
|
---|
29 | if (fns.length === 1) {
|
---|
30 | return fns[0];
|
---|
31 | }
|
---|
32 |
|
---|
33 | return function piped(input: T): R {
|
---|
34 | return fns.reduce((prev: any, fn: UnaryFunction<T, R>) => fn(prev), input as any);
|
---|
35 | };
|
---|
36 | }
|
---|