Skip to content

Commit

Permalink
fix(scan): scan operator now accepts undefined itself as a valid se…
Browse files Browse the repository at this point in the history
…ed 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 ReactiveX#2047
  • Loading branch information
jayphelps committed Oct 19, 2016
1 parent 5460e77 commit 4324bb5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
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
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 4324bb5

Please sign in to comment.