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#scan supports `undefined` as a valid seed value, so we should too.

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

fixes #2047
  • Loading branch information
jayphelps committed Oct 18, 2016
1 parent 5460e77 commit 1575a7e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 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
23 changes: 14 additions & 9 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 accumulatorSet = false;
// providing a seed of `undefined` *should* be valid and trigger
// accumulatorSet! 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) {
accumulatorSet = true;
}

return this.lift(new ScanOperator(accumulator, seed, accumulatorSet));
}

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 accumulatorSet: boolean) {}

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.accumulatorSet));
}
}

Expand All @@ -64,8 +73,6 @@ 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;
Expand All @@ -76,10 +83,8 @@ 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, private _seed: T | R, private accumulatorSet: boolean) {
super(destination);
this.seed = seed;
this.accumulatorSet = typeof seed !== 'undefined';
}

protected _next(value: T): void {
Expand Down

0 comments on commit 1575a7e

Please sign in to comment.