From d5d69809e39c69a68093a47e0e02abaf29f3e652 Mon Sep 17 00:00:00 2001 From: Dkosasih Date: Tue, 23 Apr 2019 12:35:13 +1000 Subject: [PATCH] feat(partition): partition static function. partition operator is deprecated (#4419) (#4685) --- package-lock.json | 13 ++-- spec-dtslint/observables/partition-spec.ts | 20 ++++++ .../operators/partition-spec.ts.disabled | 24 ------- .../partition-spec.ts} | 52 +++++++------- src/index.ts | 1 + src/internal/observable/partition.ts | 67 +++++++++++++++++++ src/internal/operators/partition.ts | 3 + 7 files changed, 124 insertions(+), 56 deletions(-) create mode 100644 spec-dtslint/observables/partition-spec.ts delete mode 100644 spec-dtslint/operators/partition-spec.ts.disabled rename spec/{operators/partition-spec.ts.disabled => observables/partition-spec.ts} (81%) create mode 100644 src/internal/observable/partition.ts diff --git a/package-lock.json b/package-lock.json index a3de77e4b8..3e26e6876c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4983,7 +4983,8 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "aproba": { "version": "1.2.0", @@ -5385,7 +5386,8 @@ "safe-buffer": { "version": "5.1.1", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "safer-buffer": { "version": "2.1.2", @@ -5440,6 +5442,7 @@ "version": "3.0.1", "bundled": true, "dev": true, + "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -5483,12 +5486,14 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true }, "yallist": { "version": "3.0.2", "bundled": true, - "dev": true + "dev": true, + "optional": true } } }, diff --git a/spec-dtslint/observables/partition-spec.ts b/spec-dtslint/observables/partition-spec.ts new file mode 100644 index 0000000000..1f3af18656 --- /dev/null +++ b/spec-dtslint/observables/partition-spec.ts @@ -0,0 +1,20 @@ +import { of, partition } from 'rxjs'; + +it('should infer correctly', () => { + const o = partition(of('a', 'b', 'c'), (value, index) => true); // $ExpectType [Observable, Observable] + const p = partition(of('a', 'b', 'c'), () => true); // $ExpectType [Observable, Observable] +}); + +it('should accept a thisArg parameter', () => { + const o = partition(of('a', 'b', 'c'), () => true, 5); // $ExpectType [Observable, Observable] +}); + +it('should enforce predicate', () => { + const o = partition(of('a', 'b', 'c')); // $ExpectError +}); + +it('should enforce predicate types', () => { + const o = partition(of('a', 'b', 'c'), 'nope'); // $ExpectError + const p = partition(of('a', 'b', 'c'), (value: number) => true); // $ExpectError + const q = partition(of('a', 'b', 'c'), (value, index: string) => true); // $ExpectError +}); diff --git a/spec-dtslint/operators/partition-spec.ts.disabled b/spec-dtslint/operators/partition-spec.ts.disabled deleted file mode 100644 index 84e69f9578..0000000000 --- a/spec-dtslint/operators/partition-spec.ts.disabled +++ /dev/null @@ -1,24 +0,0 @@ -import * as Rx from 'rxjs/Rx'; - -// TODO(benlesh): These tests and the imports are all wrong, because the operator is all wrong. -// this should not be an operator, rather, it should be a creation method. -const Observable = Rx.Observable; - -it('should infer correctly', () => { - const o = Observable.of('a', 'b', 'c').partition((value, index) => true); // $ExpectType [Observable, Observable] - const p = Observable.of('a', 'b', 'c').partition(() => true); // $ExpectType [Observable, Observable] -}); - -it('should accept a thisArg parameter', () => { - const o = Observable.of('a', 'b', 'c').partition(() => true, 5); // $ExpectType [Observable, Observable] -}); - -it('should enforce types', () => { - const o = Observable.of('a', 'b', 'c').partition(); // $ExpectError -}); - -it('should enforce predicate types', () => { - const o = Observable.of('a', 'b', 'c').partition('nope'); // $ExpectError - const p = Observable.of('a', 'b', 'c').partition((value: number) => true); // $ExpectError - const q = Observable.of('a', 'b', 'c').partition((value, index: string) => true); // $ExpectError -}); diff --git a/spec/operators/partition-spec.ts.disabled b/spec/observables/partition-spec.ts similarity index 81% rename from spec/operators/partition-spec.ts.disabled rename to spec/observables/partition-spec.ts index 2fd1c555b8..000d4fa57c 100644 --- a/spec/operators/partition-spec.ts.disabled +++ b/spec/observables/partition-spec.ts @@ -1,16 +1,13 @@ import { expect } from 'chai'; -import * as Rx from 'rxjs/Rx'; +import { Observable, partition, of } from 'rxjs'; +import { map, mergeMap } from 'rxjs/operators'; import { hot, cold, expectObservable, expectSubscriptions } from '../helpers/marble-testing'; -// TODO: The imports on these tests can't be modernized until we do away with -// the partition "operator" and make it a creation method. declare function asDiagram(arg: string): Function; -const Observable = Rx.Observable; - /** @test {partition} */ describe('Observable.prototype.partition', () => { - function expectObservableArray(result: Rx.Observable[], expected: string[]) { + function expectObservableArray(result: Observable[], expected: string[]) { for (let idx = 0; idx < result.length; idx++ ) { expectObservable(result[idx]).toBe(expected[idx]); } @@ -23,7 +20,7 @@ describe('Observable.prototype.partition', () => { const expected = ['--1-----3---------5------|', '----2----------4------6--|']; - const result = e1.partition((x: any) => x % 2 === 1); + const result = partition(e1, (x: any) => x % 2 === 1); expectObservableArray(result, expected); expectSubscriptions(e1.subscriptions).toBe([e1subs, e1subs]); @@ -39,7 +36,7 @@ describe('Observable.prototype.partition', () => { return x === 'a'; } - expectObservableArray(e1.partition(predicate), expected); + expectObservableArray(partition(e1, predicate), expected); expectSubscriptions(e1.subscriptions).toBe([e1subs, e1subs]); }); @@ -53,7 +50,7 @@ describe('Observable.prototype.partition', () => { return index % 2 === 0; } - expectObservableArray(e1.partition(predicate), expected); + expectObservableArray(partition(e1, predicate), expected); expectSubscriptions(e1.subscriptions).toBe([e1subs, e1subs]); }); @@ -67,7 +64,7 @@ describe('Observable.prototype.partition', () => { return x === this.value; } - expectObservableArray(e1.partition(predicate, {value: 'a'}), expected); + expectObservableArray(partition(e1, predicate, {value: 'a'}), expected); expectSubscriptions(e1.subscriptions).toBe([e1subs, e1subs]); }); @@ -81,7 +78,7 @@ describe('Observable.prototype.partition', () => { return x === 'a'; } - expectObservableArray(e1.partition(predicate), expected); + expectObservableArray(partition(e1, predicate), expected); expectSubscriptions(e1.subscriptions).toBe([e1subs, e1subs]); }); @@ -95,7 +92,7 @@ describe('Observable.prototype.partition', () => { return x === 'a'; } - expectObservableArray(e1.partition(predicate), expected); + expectObservableArray(partition(e1, predicate), expected); expectSubscriptions(e1.subscriptions).toBe([e1subs, e1subs]); }); @@ -115,7 +112,7 @@ describe('Observable.prototype.partition', () => { return match; } - expectObservableArray(e1.partition(predicate), expected); + expectObservableArray(partition(e1, predicate), expected); expectSubscriptions(e1.subscriptions).toBe([e1subs, e1subs]); }); @@ -129,7 +126,7 @@ describe('Observable.prototype.partition', () => { return x === 'x'; } - expectObservableArray(e1.partition(predicate), expected); + expectObservableArray(partition(e1, predicate), expected); expectSubscriptions(e1.subscriptions).toBe([e1subs, e1subs]); }); @@ -143,7 +140,7 @@ describe('Observable.prototype.partition', () => { return x === 'x'; } - expectObservableArray(e1.partition(predicate), expected); + expectObservableArray(partition(e1, predicate), expected); expectSubscriptions(e1.subscriptions).toBe([e1subs, e1subs]); }); @@ -157,7 +154,7 @@ describe('Observable.prototype.partition', () => { return x === 'a'; } - expectObservableArray(e1.partition(predicate), expected); + expectObservableArray(partition(e1, predicate), expected); expectSubscriptions(e1.subscriptions).toBe([e1subs, e1subs]); }); @@ -171,7 +168,7 @@ describe('Observable.prototype.partition', () => { return x === 'a'; } - expectObservableArray(e1.partition(predicate), expected); + expectObservableArray(partition(e1, predicate), expected); expectSubscriptions(e1.subscriptions).toBe([e1subs, e1subs]); }); @@ -185,7 +182,7 @@ describe('Observable.prototype.partition', () => { return x === 'a'; } - expectObservableArray(e1.partition(predicate), expected); + expectObservableArray(partition(e1, predicate), expected); expectSubscriptions(e1.subscriptions).toBe([e1subs, e1subs]); }); @@ -199,7 +196,7 @@ describe('Observable.prototype.partition', () => { return x === 'a'; } - expectObservableArray(e1.partition(predicate), expected); + expectObservableArray(partition(e1, predicate), expected); expectSubscriptions(e1.subscriptions).toBe([e1subs, e1subs]); }); @@ -213,7 +210,7 @@ describe('Observable.prototype.partition', () => { return x === 'a'; } - expectObservableArray(e1.partition(predicate), expected); + expectObservableArray(partition(e1, predicate), expected); expectSubscriptions(e1.subscriptions).toBe([e1subs, e1subs]); }); @@ -227,7 +224,7 @@ describe('Observable.prototype.partition', () => { function predicate(x: string) { return x === 'a'; } - const result = e1.partition(predicate); + const result = partition(e1, predicate); for (let idx = 0; idx < result.length; idx++ ) { expectObservable(result[idx], unsub).toBe(expected[idx]); @@ -242,11 +239,10 @@ describe('Observable.prototype.partition', () => { '----b--- ']; const unsub = ' ! '; - const result = e1 - .mergeMap((x: string) => Observable.of(x)) - .partition((x: string) => x === 'a') - .map((observable: Rx.Observable) => - observable.mergeMap((x: string) => Observable.of(x))); + const e1Pipe = e1.pipe( + mergeMap((x: string) => of(x)) + ); + const result = partition(e1Pipe, (x: string) => x === 'a'); expectObservable(result[0], unsub).toBe(expected[0]); expectObservable(result[1], unsub).toBe(expected[1]); @@ -256,10 +252,10 @@ describe('Observable.prototype.partition', () => { it('should accept thisArg', () => { const thisArg = {}; - Observable.of(1).partition(function (this: any, value: number) { + partition(of(1), function (this: any, value: number) { expect(this).to.deep.equal(thisArg); return true; }, thisArg) - .forEach((observable: Rx.Observable) => observable.subscribe()); + .forEach((observable: Observable) => observable.subscribe()); }); }); diff --git a/src/index.ts b/src/index.ts index 35b831c9b3..624a92be5c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -58,6 +58,7 @@ export { never } from './internal/observable/never'; export { of } from './internal/observable/of'; export { onErrorResumeNext } from './internal/observable/onErrorResumeNext'; export { pairs } from './internal/observable/pairs'; +export { partition } from './internal/observable/partition'; export { race } from './internal/observable/race'; export { range } from './internal/observable/range'; export { throwError } from './internal/observable/throwError'; diff --git a/src/internal/observable/partition.ts b/src/internal/observable/partition.ts new file mode 100644 index 0000000000..637172fc86 --- /dev/null +++ b/src/internal/observable/partition.ts @@ -0,0 +1,67 @@ +import { not } from '../util/not'; +import { subscribeTo } from '../util/subscribeTo'; +import { filter } from '../operators/filter'; +import { ObservableInput } from '../types'; +import { Observable } from '../Observable'; + +/** + * Splits the source Observable into two, one with values that satisfy a + * predicate, and another with values that don't satisfy the predicate. + * + * It's like {@link filter}, but returns two Observables: + * one like the output of {@link filter}, and the other with values that did not + * pass the condition. + * + * ![](partition.png) + * + * `partition` outputs an array with two Observables that partition the values + * from the source Observable through the given `predicate` function. The first + * Observable in that array emits source values for which the predicate argument + * returns true. The second Observable emits source values for which the + * predicate returns false. The first behaves like {@link filter} and the second + * behaves like {@link filter} with the predicate negated. + * + * ## Example + * Partition a set of numbers into odds and evens observables + * ```ts + * import { of, partition } from 'rxjs'; + * + * const observableValues = of(1, 2, 3, 4, 5, 6); + * const [evens$, odds$] = partition(observableValues, (value, index) => value % 2 === 0); + * + * odds$.subscribe(x => console.log('odds', x)); + * evens$.subscribe(x => console.log('evens', x)); + * + * // Logs: + * // odds 1 + * // odds 3 + * // odds 5 + * // evens 2 + * // evens 4 + * // evens 6 + * ``` + * + * @see {@link filter} + * + * @param {function(value: T, index: number): boolean} predicate A function that + * evaluates each value emitted by the source Observable. If it returns `true`, + * the value is emitted on the first Observable in the returned array, if + * `false` the value is emitted on the second Observable in the array. The + * `index` parameter is the number `i` for the i-th source emission that has + * happened since the subscription, starting from the number `0`. + * @param {any} [thisArg] An optional argument to determine the value of `this` + * in the `predicate` function. + * @return {[Observable, Observable]} An array with two Observables: one + * with values that passed the predicate, and another with values that did not + * pass the predicate. + */ +export function partition( + source: ObservableInput, + predicate: (value: T, index: number) => boolean, + thisArg?: any +): [Observable, Observable] { + return [ + filter(predicate, thisArg)(new Observable(subscribeTo(source))), + filter(not(predicate, thisArg) as any)(new Observable(subscribeTo(source))) + ] as [Observable, Observable]; +} diff --git a/src/internal/operators/partition.ts b/src/internal/operators/partition.ts index 355e32305e..41c00c4807 100644 --- a/src/internal/operators/partition.ts +++ b/src/internal/operators/partition.ts @@ -50,6 +50,9 @@ import { UnaryFunction } from '../types'; * @method partition * @owner Observable */ +/** + * @deprecated use `partition` static creation function instead + */ export function partition(predicate: (value: T, index: number) => boolean, thisArg?: any): UnaryFunction, [Observable, Observable]> { return (source: Observable) => [