Skip to content

Commit

Permalink
fix(reduce/scan): both scan/reduce operators now accepts undefined
Browse files Browse the repository at this point in the history
…itself as a valid seed (#2050)

* fix(scan): scan operator now accepts `undefined` itself as a valid seed value

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

```js
of(1, 2, 3).scan((acc, x) => acc + ' ' + x, undefined);
// "undefined 1"
// "undefined 1 2"
// "undefined 1 2 3"
```

fixes #2047

* fix(reduce): reduce operator now accepts `undefined` itself as a valid 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 24, 2016
1 parent fea08e9 commit fee7585
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 21 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
20 changes: 20 additions & 0 deletions spec/operators/scan-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@ describe('Observable.prototype.scan', () => {
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});

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

const values = {
u: 'undefined b',
v: 'undefined b c',
w: 'undefined b c d',
x: 'undefined b c d e',
y: 'undefined b c d e f',
z: 'undefined b c d e f g'
};

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

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

it('should scan without seed', () => {
const e1 = hot('--a--^--b--c--d--|');
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
27 changes: 16 additions & 11 deletions src/operator/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,24 @@ export function scan<T>(this: Observable<T>, accumulator: (acc: T[], value: T, i
export function scan<T, R>(this: Observable<T>, accumulator: (acc: R, value: T, index: number) => R, seed?: R): Observable<R>;
/* tslint:disable:max-line-length */
export function scan<T, R>(this: Observable<T>, accumulator: (acc: R, value: T, index: number) => R, seed?: T | R): Observable<R> {
return this.lift(new ScanOperator(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 ScanOperator(accumulator, seed, hasSeed));
}

class ScanOperator<T, R> implements Operator<T, R> {
constructor(private accumulator: (acc: R, value: T, index: number) => R, private seed?: T | R) {
}
constructor(private accumulator: (acc: R, value: T, index: number) => R, private seed?: T | R, private hasSeed: boolean = false) {}

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

Expand All @@ -64,26 +73,22 @@ class ScanOperator<T, R> implements Operator<T, R> {
*/
class ScanSubscriber<T, R> extends Subscriber<T> {
private index: number = 0;
private accumulatorSet: boolean = false;
private _seed: T | R;

get seed(): T | R {
return this._seed;
}

set seed(value: T | R) {
this.accumulatorSet = true;
this.hasSeed = true;
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, private _seed: T | R, private hasSeed: boolean) {
super(destination);
this.seed = seed;
this.accumulatorSet = typeof seed !== 'undefined';
}

protected _next(value: T): void {
if (!this.accumulatorSet) {
if (!this.hasSeed) {
this.seed = value;
this.destination.next(value);
} else {
Expand Down

0 comments on commit fee7585

Please sign in to comment.