From baed383f275272a7354ffcb09131c28de9c63522 Mon Sep 17 00:00:00 2001 From: Jason Aden Date: Thu, 7 Sep 2017 11:28:43 -0700 Subject: [PATCH] feat(skip): add higher-order lettable version of skip --- src/operator/skip.ts | 34 ++---------------------------- src/operators/index.ts | 1 + src/operators/skip.ts | 48 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 32 deletions(-) create mode 100644 src/operators/skip.ts diff --git a/src/operator/skip.ts b/src/operator/skip.ts index 7b1cb7d89b..9344370cec 100644 --- a/src/operator/skip.ts +++ b/src/operator/skip.ts @@ -1,7 +1,5 @@ -import { Operator } from '../Operator'; -import { Subscriber } from '../Subscriber'; import { Observable } from '../Observable'; -import { TeardownLogic } from '../Subscription'; +import { skip as higherOrder } from '../operators/skip'; /** * Returns an Observable that skips the first `count` items emitted by the source Observable. @@ -15,33 +13,5 @@ import { TeardownLogic } from '../Subscription'; * @owner Observable */ export function skip(this: Observable, count: number): Observable { - return this.lift(new SkipOperator(count)); -} - -class SkipOperator implements Operator { - constructor(private total: number) { - } - - call(subscriber: Subscriber, source: any): TeardownLogic { - return source.subscribe(new SkipSubscriber(subscriber, this.total)); - } -} - -/** - * We need this JSDoc comment for affecting ESDoc. - * @ignore - * @extends {Ignored} - */ -class SkipSubscriber extends Subscriber { - count: number = 0; - - constructor(destination: Subscriber, private total: number) { - super(destination); - } - - protected _next(x: T) { - if (++this.count > this.total) { - this.destination.next(x); - } - } + return higherOrder(count)(this); } diff --git a/src/operators/index.ts b/src/operators/index.ts index 457aeb5ee0..60b916b666 100644 --- a/src/operators/index.ts +++ b/src/operators/index.ts @@ -60,6 +60,7 @@ export { race } from './race'; export { reduce } from './reduce'; export { refCount } from './refCount'; export { scan } from './scan'; +export { skip } from './skip'; export { subscribeOn } from './subscribeOn'; export { switchAll } from './switchAll'; export { switchMap } from './switchMap'; diff --git a/src/operators/skip.ts b/src/operators/skip.ts new file mode 100644 index 0000000000..4e848e82f5 --- /dev/null +++ b/src/operators/skip.ts @@ -0,0 +1,48 @@ +import { Operator } from '../Operator'; +import { Subscriber } from '../Subscriber'; +import { Observable } from '../Observable'; +import { TeardownLogic } from '../Subscription'; +import { MonoTypeOperatorFunction } from '../interfaces'; + +/** + * Returns an Observable that skips the first `count` items emitted by the source Observable. + * + * + * + * @param {Number} count - The number of times, items emitted by source Observable should be skipped. + * @return {Observable} An Observable that skips values emitted by the source Observable. + * + * @method skip + * @owner Observable + */ +export function skip(count: number): MonoTypeOperatorFunction { + return (source: Observable) => source.lift(new SkipOperator(count)); +} + +class SkipOperator implements Operator { + constructor(private total: number) { + } + + call(subscriber: Subscriber, source: any): TeardownLogic { + return source.subscribe(new SkipSubscriber(subscriber, this.total)); + } +} + +/** + * We need this JSDoc comment for affecting ESDoc. + * @ignore + * @extends {Ignored} + */ +class SkipSubscriber extends Subscriber { + count: number = 0; + + constructor(destination: Subscriber, private total: number) { + super(destination); + } + + protected _next(x: T) { + if (++this.count > this.total) { + this.destination.next(x); + } + } +}