Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Created October 10, 2017 04:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattpodwysocki/3cad90db7c86e53354c16317e7c2c52e to your computer and use it in GitHub Desktop.
Save mattpodwysocki/3cad90db7c86e53354c16317e7c2c52e to your computer and use it in GitHub Desktop.
import { OperatorAsyncFunction } from '../interfaces';
import { AsyncIterableX } from '../asynciterable';
/* tslint:disable:max-line-length */
export function pipe<T>(source: AsyncIterable<T>): AsyncIterableX<T>;
export function pipe<T, A>(source: AsyncIterable<T>, op1: OperatorAsyncFunction<T, A>): AsyncIterableX<A>;
export function pipe<T, A, B>(source: AsyncIterable<T>, op1: OperatorAsyncFunction<T, A>, op2: OperatorAsyncFunction<A, B>): AsyncIterableX<B>;
export function pipe<T, A, B, C>(source: AsyncIterable<T>, op1: OperatorAsyncFunction<T, A>, op2: OperatorAsyncFunction<A, B>, op3: OperatorAsyncFunction<B, C>): AsyncIterableX<C>;
export function pipe<T, A, B, C, D>(source: AsyncIterable<T>, op1: OperatorAsyncFunction<T, A>, op2: OperatorAsyncFunction<A, B>, op3: OperatorAsyncFunction<B, C>, op4: OperatorAsyncFunction<C, D>): AsyncIterableX<D>;
export function pipe<T, A, B, C, D, E>(source: AsyncIterable<T>, op1: OperatorAsyncFunction<T, A>, op2: OperatorAsyncFunction<A, B>, op3: OperatorAsyncFunction<B, C>, op4: OperatorAsyncFunction<C, D>, op5: OperatorAsyncFunction<D, E>): AsyncIterableX<E>;
export function pipe<T, A, B, C, D, E, F>(source: AsyncIterable<T>, op1: OperatorAsyncFunction<T, A>, op2: OperatorAsyncFunction<A, B>, op3: OperatorAsyncFunction<B, C>, op4: OperatorAsyncFunction<C, D>, op5: OperatorAsyncFunction<D, E>, op6: OperatorAsyncFunction<E, F>): AsyncIterableX<F>;
export function pipe<T, A, B, C, D, E, F, G>(source: AsyncIterable<T>, op1: OperatorAsyncFunction<T, A>, op2: OperatorAsyncFunction<A, B>, op3: OperatorAsyncFunction<B, C>, op4: OperatorAsyncFunction<C, D>, op5: OperatorAsyncFunction<D, E>, op6: OperatorAsyncFunction<E, F>, op7: OperatorAsyncFunction<F, G>): AsyncIterableX<G>;
export function pipe<T, A, B, C, D, E, F, G, H>(source: AsyncIterable<T>, op1: OperatorAsyncFunction<T, A>, op2: OperatorAsyncFunction<A, B>, op3: OperatorAsyncFunction<B, C>, op4: OperatorAsyncFunction<C, D>, op5: OperatorAsyncFunction<D, E>, op6: OperatorAsyncFunction<E, F>, op7: OperatorAsyncFunction<F, G>, op8: OperatorAsyncFunction<G, H>): AsyncIterableX<H>;
export function pipe<T, A, B, C, D, E, F, G, H, I>(source: AsyncIterable<T>, op1: OperatorAsyncFunction<T, A>, op2: OperatorAsyncFunction<A, B>, op3: OperatorAsyncFunction<B, C>, op4: OperatorAsyncFunction<C, D>, op5: OperatorAsyncFunction<D, E>, op6: OperatorAsyncFunction<E, F>, op7: OperatorAsyncFunction<F, G>, op8: OperatorAsyncFunction<G, H>, op9: OperatorAsyncFunction<H, I>): AsyncIterableX<I>;
/* tslint:enable:max-line-length */
export function pipe<TSource, TResult>(
source: AsyncIterable<TSource>,
...operations: OperatorAsyncFunction<TSource, TResult>[]): AsyncIterableX<TResult> {
if (operations.length === 0) {
return source as any;
}
if (operations.length === 1) {
return operations[0] as any;
}
const piped = (input: AsyncIterable<TSource>): AsyncIterableX<TResult> => {
return operations.reduce((prev: any, fn: OperatorAsyncFunction<TSource, TResult>) => fn(prev), input);
};
return piped(source);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment