Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion spec/operators/reduce-spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Rx from '../../dist/cjs/Rx';
declare const {hot, cold, asDiagram, expectObservable, expectSubscriptions};
declare const {hot, cold, asDiagram, expectObservable, expectSubscriptions, type};

const Observable = Rx.Observable;

Expand Down Expand Up @@ -231,4 +231,33 @@ describe('Observable.prototype.reduce', () => {
expectObservable(e1.reduce(reduceFunction)).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should accept array typed reducers', () => {
type(() => {
let a: Rx.Observable<{ a: number; b: string }>;
a.reduce((acc, value) => acc.concat(value), []);
});
});

it('should accept T typed reducers', () => {
type(() => {
let a: Rx.Observable<{ a?: number; b?: string }>;
a.reduce((acc, value) => {
value.a = acc.a;
value.b = acc.b;
return acc;
}, {});
});
});

it('should accept R typed reducers', () => {
type(() => {
let a: Rx.Observable<{ a: number; b: string }>;
a.reduce<{ a?: number; b?: string }>((acc, value) => {
value.a = acc.a;
value.b = acc.b;
return acc;
}, {});
});
});
});
31 changes: 30 additions & 1 deletion spec/operators/scan-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {expect} from 'chai';
import * as Rx from '../../dist/cjs/Rx';
declare const {hot, cold, asDiagram, expectObservable, expectSubscriptions};
declare const {hot, cold, asDiagram, expectObservable, expectSubscriptions, type};

const Observable = Rx.Observable;

Expand Down Expand Up @@ -201,4 +201,33 @@ describe('Observable.prototype.scan', () => {
expectObservable(scan).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should accept array typed reducers', () => {
type(() => {
let a: Rx.Observable<{ a: number; b: string }>;
a.reduce((acc, value) => acc.concat(value), []);
});
});

it('should accept T typed reducers', () => {
type(() => {
let a: Rx.Observable<{ a?: number; b?: string }>;
a.reduce((acc, value) => {
value.a = acc.a;
value.b = acc.b;
return acc;
}, {});
});
});

it('should accept R typed reducers', () => {
type(() => {
let a: Rx.Observable<{ a: number; b: string }>;
a.reduce<{ a?: number; b?: string }>((acc, value) => {
value.a = acc.a;
value.b = acc.b;
return acc;
}, {});
});
});
});
5 changes: 3 additions & 2 deletions src/add/operator/scan.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@

import {Observable} from '../../Observable';
import {scan, ScanSignature} from '../../operator/scan';
import {ReduceSignature} from '../../operator/reduce';
import {scan} from '../../operator/scan';

Observable.prototype.scan = scan;

declare module '../../Observable' {
interface Observable<T> {
scan: ScanSignature<T>;
scan: ReduceSignature<T>;
}
}
4 changes: 3 additions & 1 deletion src/operator/reduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ export function reduce<T, R>(accumulator: (acc: R, value: T) => R, seed?: R): Ob
}

export interface ReduceSignature<T> {
<R>(accumulator: (acc: R, value: T) => R, seed?: R): Observable<R>;
(accumulator: (acc: T, value: T, index: number) => T, seed?: T): Observable<T>;
(accumulator: (acc: T[], value: T, index: number) => T[], seed?: T[]): Observable<T[]>;
<R>(accumulator: (acc: R, value: T, index: number) => R, seed?: R): Observable<R>;
}

export class ReduceOperator<T, R> implements Operator<T, R> {
Expand Down
6 changes: 1 addition & 5 deletions src/operator/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ export function scan<T, R>(accumulator: (acc: R, value: T, index: number) => R,
return this.lift(new ScanOperator(accumulator, seed));
}

export interface ScanSignature<T> {
<R>(accumulator: (acc: R, value: T, index: number) => R, seed?: T | R): Observable<R>;
}

class ScanOperator<T, R> implements Operator<T, R> {
constructor(private accumulator: (acc: R, value: T, index: number) => R, private seed?: T | R) {
}
Expand Down Expand Up @@ -75,7 +71,7 @@ class ScanSubscriber<T, R> extends Subscriber<T> {
this._seed = value;
}

constructor(destination: Subscriber<R>, private accumulator: (acc: R, value: T, index: number) => R, seed?: T|R) {
constructor(destination: Subscriber<R>, private accumulator: (acc: R, value: T, index: number) => R, seed?: T | R) {
super(destination);
this.seed = seed;
this.accumulatorSet = typeof seed !== 'undefined';
Expand Down