Skip to content

Commit

Permalink
fix(reduce): reduce operator now accepts undefined itself as a vali…
Browse files Browse the repository at this point in the history
…d seed value

Array#reduce supports `undefined` as a valid seed value, so it seems
natural that we would too.

```js
of(1, 2, 3).reduce((acc, x) => acc + ' ' + x, undefined);
// "undefined 1 2 3"
```
  • Loading branch information
jayphelps committed Oct 19, 2016
1 parent 4324bb5 commit a34a76d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
30 changes: 30 additions & 0 deletions spec/operators/reduce-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,36 @@ describe('Observable.prototype.reduce', () => {
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should reduce with a seed of undefined', () => {
const e1 = hot('--a--^--b--c--d--e--f--g--|');
const e1subs = '^ !';
const expected = '---------------------(x|)';

const values = {
x: 'undefined b c d e f g'
};

const source = e1.reduce((acc: any, x: string) => acc + ' ' + x, undefined);

expectObservable(source).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should reduce without a seed', () => {
const e1 = hot('--a--^--b--c--d--e--f--g--|');
const e1subs = '^ !';
const expected = '---------------------(x|)';

const values = {
x: 'b c d e f g'
};

const source = e1.reduce((acc: any, x: string) => acc + ' ' + x);

expectObservable(source).toBe(expected, values);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

it('should reduce with seed if source is empty', () => {
const e1 = hot('--a--^-------|');
const e1subs = '^ !';
Expand Down
25 changes: 15 additions & 10 deletions src/operator/reduce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,24 @@ export function reduce<T>(this: Observable<T>, accumulator: (acc: T[], value: T,
export function reduce<T, R>(this: Observable<T>, accumulator: (acc: R, value: T, index: number) => R, seed?: R): Observable<R>;
/* tslint:disable:max-line-length */
export function reduce<T, R>(this: Observable<T>, accumulator: (acc: R, value: T) => R, seed?: R): Observable<R> {
return this.lift(new ReduceOperator(accumulator, seed));
let hasSeed = false;
// providing a seed of `undefined` *should* be valid and trigger
// hasSeed! so don't use `seed !== undefined` checks!
// For this reason, we have to check it here at the original call site
// otherwise inside Operator/Subscriber we won't know if `undefined`
// means they didn't provide anything or if they literally provided `undefined`
if (arguments.length >= 2) {
hasSeed = true;
}

return this.lift(new ReduceOperator(accumulator, seed, hasSeed));
}

export class ReduceOperator<T, R> implements Operator<T, R> {

constructor(private accumulator: (acc: R, value: T) => R, private seed?: R) {
}
constructor(private accumulator: (acc: R, value: T) => R, private seed?: R, private hasSeed: boolean = false) {}

call(subscriber: Subscriber<R>, source: any): any {
return source._subscribe(new ReduceSubscriber(subscriber, this.accumulator, this.seed));
return source._subscribe(new ReduceSubscriber(subscriber, this.accumulator, this.seed, this.hasSeed));
}
}

Expand All @@ -72,18 +80,15 @@ export class ReduceOperator<T, R> implements Operator<T, R> {
* @extends {Ignored}
*/
export class ReduceSubscriber<T, R> extends Subscriber<T> {

acc: T | R;
hasSeed: boolean;
hasValue: boolean = false;

constructor(destination: Subscriber<R>,
private accumulator: (acc: R, value: T) => R,
seed?: R) {
seed: R,
private hasSeed: boolean) {
super(destination);
this.acc = seed;
this.accumulator = accumulator;
this.hasSeed = typeof seed !== 'undefined';
}

protected _next(value: T) {
Expand Down

0 comments on commit a34a76d

Please sign in to comment.