Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Last active February 10, 2020 02:13
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/edd6af8f1a297fe1fd71b352ab526f95 to your computer and use it in GitHub Desktop.
Save mattpodwysocki/edd6af8f1a297fe1fd71b352ab526f95 to your computer and use it in GitHub Desktop.
export interface GroupByOptions<TSource, TKey, TValue = TSource> {
keySelector: (value: TSource) => TKey | Promise<TKey>;
elementSelector?: (value: TSource) => TValue | Promise<TValue>;
signal?: AbortSignal;
}
export interface GroupByOptionsWithSelector<TSource, TKey, TValue, TResult> {
keySelector: (value: TSource) => TKey | Promise<TKey>;
elementSelector?: (value: TSource) => TValue | Promise<TValue>;
resultSelector?: (
key: TKey,
values: Iterable<TValue>
) => TResult | Promise<TResult>;
signal?: AbortSignal;
}
export function groupBy<TSource, TKey>(
options: GroupByOptions<TSource, TKey, TSource>
): OperatorAsyncFunction<TSource, GroupedAsyncIterable<TKey, TSource>>;
export function groupBy<TSource, TKey, TValue>(
options: GroupByOptions<TSource, TKey, TValue>
): OperatorAsyncFunction<TSource, GroupedAsyncIterable<TKey, TValue>>;
export function groupBy<TSource, TKey, TValue, TResult>(
options: GroupByOptionsWithSelector<TSource, TKey, TValue, TResult>
): OperatorAsyncFunction<TSource, TResult> {
return function groupByOperatorFunction(source: AsyncIterable<TSource>): AsyncIterableX<TResult> {
return new GroupByAsyncIterable<TSource, TKey, TValue, TResult>(
source,
options?.keySelector,
options?.elementSelector || identityAsync,
options?.resultSelector || groupByResultIdentityAsync
);
};
}
export function groupBy<TSource, TKey>(
keySelector: (value: TSource) => TKey | Promise<TKey>
): OperatorAsyncFunction<TSource, GroupedAsyncIterable<TKey, TSource>>;
export function groupBy<TSource, TKey, TValue>(
keySelector: (value: TSource) => TKey | Promise<TKey>,
elementSelector?: (value: TSource) => TValue | Promise<TValue>
): OperatorAsyncFunction<TSource, GroupedAsyncIterable<TKey, TValue>>;
export function groupBy<TSource, TKey, TValue, TResult>(
keySelector: (value: TSource) => TKey | Promise<TKey>,
elementSelector?: (value: TSource) => TValue | Promise<TValue>,
resultSelector?: (key: TKey, values: Iterable<TValue>) => TResult | Promise<TResult>
): OperatorAsyncFunction<TSource, TResult>;
export function groupBy<TSource, TKey, TValue, TResult>(
keySelector: (value: TSource) => TKey | Promise<TKey>,
elementSelector: (value: TSource) => TValue | Promise<TValue> = identityAsync,
resultSelector: (
key: TKey,
values: Iterable<TValue>
) => TResult | Promise<TResult> = groupByResultIdentityAsync
): OperatorAsyncFunction<TSource, TResult> {
return function groupByOperatorFunction(source: AsyncIterable<TSource>): AsyncIterableX<TResult> {
return new GroupByAsyncIterable<TSource, TKey, TValue, TResult>(
source,
keySelector,
elementSelector,
resultSelector
);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment