Skip to content

Commit

Permalink
fix(filter): consecutive filtering respects original order
Browse files Browse the repository at this point in the history
When using muliple filter() operators consecutively, their respective passes() functions should be
called in the same order as they were declared and composed, not in reverse order.

Closes issue #85.
  • Loading branch information
staltz committed Jul 20, 2016
1 parent 5829bf9 commit fdbd00a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1537,7 +1537,7 @@ export class Stream<T> implements InternalListener<T> {
const p = this._prod;
if (p instanceof FilterOperator) {
return new Stream<T>(new FilterOperator(
and(passes, (<FilterOperator<T>> p).passes),
and((<FilterOperator<T>> p).passes, passes),
(<FilterOperator<T>> p).ins
));
}
Expand Down
27 changes: 27 additions & 0 deletions tests/operator/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,33 @@ describe('Stream.prototype.filter', () => {
done();
});

it('should call functions in correct order for filter+filter fusion', (done) => {
const object$ = xs.of<any>(
{ foo: { a: 10 } },
{ foo: { bar: { b: 20 } } },
{ foo: true }
);

const filtered$ = object$
.filter(val => val.foo)
.filter(val => val.foo.bar)
.filter(val => val.foo.bar.b === 20)
.map(x => JSON.stringify(x));

const expected = ['{"foo":{"bar":{"b":20}}}'];

filtered$.addListener({
next(x: string) {
assert.strictEqual(x, expected.shift());
},
error: (err: any) => done(err),
complete() {
assert.strictEqual(expected.length, 0);
done();
}
});
});

it('should return a Stream if input stream is a Stream', (done) => {
const input = xs.of<number>(1, 2, 3);
assert.strictEqual(input instanceof Stream, true);
Expand Down

0 comments on commit fdbd00a

Please sign in to comment.