Skip to content

Commit

Permalink
fix(partition): handles thisArg as expected (#2138)
Browse files Browse the repository at this point in the history
This fix ensures that the `partition` will call `predicate` with`thisArg` as expected if it is specified.
  • Loading branch information
jooyunghan authored and jayphelps committed Nov 17, 2016
1 parent 9ebc46b commit 6cf7296
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
24 changes: 24 additions & 0 deletions spec/operators/partition-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ describe('Observable.prototype.partition', () => {
expectSubscriptions(e1.subscriptions).toBe([e1subs, e1subs]);
});

it('should partition an observable into two using a predicate and thisArg', () => {
const e1 = hot('--a-b---a------d--a---c--|');
const e1subs = '^ !';
const expected = ['--a-----a---------a------|',
'----b----------d------c--|'];

function predicate(x) {
return x === this.value;
}

expectObservableArray(e1.partition(predicate, {value: 'a'}), expected);
expectSubscriptions(e1.subscriptions).toBe([e1subs, e1subs]);
});

it('should pass errors to both returned observables', () => {
const e1 = hot('--a-b---#');
const e1subs = '^ !';
Expand Down Expand Up @@ -225,4 +239,14 @@ describe('Observable.prototype.partition', () => {
const e1 = hot('--a-b---a------d----');
expect(e1.partition).to.throw();
});

it('should accept thisArg', () => {
const thisArg = {};

Observable.of(1).partition(function (value: number) {
expect(this).to.deep.equal(thisArg);
return true;
}, thisArg)
.forEach((observable: Rx.Observable<number>) => observable.subscribe());
});
});
2 changes: 1 addition & 1 deletion src/operator/partition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ import { Observable } from '../Observable';
*/
export function partition<T>(this: Observable<T>, predicate: (value: T) => boolean, thisArg?: any): [Observable<T>, Observable<T>] {
return [
filter.call(this, predicate),
filter.call(this, predicate, thisArg),
filter.call(this, not(predicate, thisArg))
];
}

0 comments on commit 6cf7296

Please sign in to comment.