-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
splitBy.ts
28 lines (24 loc) · 999 Bytes
/
splitBy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* @license Use of this source code is governed by an MIT-style license that
* can be found in the LICENSE file at https://github.com/cartant/rxjs-etc
*/
import { Observable, OperatorFunction, Subject } from "rxjs";
import { bucketBy } from "./bucketBy";
export function splitBy<T, S extends T>(
predicate: (value: T, index: number) => value is S,
subjectSelector?: () => Subject<T>
): OperatorFunction<T, [Observable<S>, Observable<Exclude<T, S>>]>;
export function splitBy<T>(
predicate: (value: T, index: number) => boolean,
subjectSelector?: () => Subject<T>
): OperatorFunction<T, [Observable<T>, Observable<T>]>;
export function splitBy<T>(
predicate: (value: T, index: number) => boolean,
subjectSelector: () => Subject<T> = () => new Subject<T>()
): OperatorFunction<T, [Observable<T>, Observable<T>]> {
return bucketBy(
2,
(value, index) => (predicate(value, index) ? 0 : 1),
subjectSelector
) as OperatorFunction<T, [Observable<T>, Observable<T>]>;
}