diff --git a/src/operator/mapTo.ts b/src/operator/mapTo.ts index b3396c1a2c..0a4aab4ba3 100644 --- a/src/operator/mapTo.ts +++ b/src/operator/mapTo.ts @@ -1,6 +1,5 @@ -import { Operator } from '../Operator'; -import { Subscriber } from '../Subscriber'; import { Observable } from '../Observable'; +import { mapTo as higherOrder } from '../operators'; /** * Emits the given constant value on the output Observable every time the source @@ -29,37 +28,5 @@ import { Observable } from '../Observable'; * @owner Observable */ export function mapTo(this: Observable, value: R): Observable { - return this.lift(new MapToOperator(value)); -} - -class MapToOperator implements Operator { - - value: R; - - constructor(value: R) { - this.value = value; - } - - call(subscriber: Subscriber, source: any): any { - return source.subscribe(new MapToSubscriber(subscriber, this.value)); - } -} - -/** - * We need this JSDoc comment for affecting ESDoc. - * @ignore - * @extends {Ignored} - */ -class MapToSubscriber extends Subscriber { - - value: R; - - constructor(destination: Subscriber, value: R) { - super(destination); - this.value = value; - } - - protected _next(x: T) { - this.destination.next(this.value); - } + return higherOrder(value)(this); } diff --git a/src/operators/index.ts b/src/operators/index.ts index 8c86794819..73fd4477a6 100644 --- a/src/operators/index.ts +++ b/src/operators/index.ts @@ -34,6 +34,7 @@ export { ignoreElements } from './ignoreElements'; export { isEmpty } from './isEmpty'; export { last } from './last'; export { map } from './map'; +export { mapTo } from './mapTo'; export { materialize } from './materialize'; export { max } from './max'; export { merge } from './merge'; diff --git a/src/operators/mapTo.ts b/src/operators/mapTo.ts new file mode 100644 index 0000000000..2dfebd75c8 --- /dev/null +++ b/src/operators/mapTo.ts @@ -0,0 +1,66 @@ +import { Operator } from '../Operator'; +import { Subscriber } from '../Subscriber'; +import { Observable } from '../Observable'; +import { OperatorFunction } from '../interfaces'; + +/** + * Emits the given constant value on the output Observable every time the source + * Observable emits a value. + * + * Like {@link map}, but it maps every source value to + * the same output value every time. + * + * + * + * Takes a constant `value` as argument, and emits that whenever the source + * Observable emits a value. In other words, ignores the actual source value, + * and simply uses the emission moment to know when to emit the given `value`. + * + * @example Map every click to the string 'Hi' + * var clicks = Rx.Observable.fromEvent(document, 'click'); + * var greetings = clicks.mapTo('Hi'); + * greetings.subscribe(x => console.log(x)); + * + * @see {@link map} + * + * @param {any} value The value to map each source value to. + * @return {Observable} An Observable that emits the given `value` every time + * the source Observable emits something. + * @method mapTo + * @owner Observable + */ +export function mapTo(value: R): OperatorFunction { + return (source: Observable) => source.lift(new MapToOperator(value)); +} + +class MapToOperator implements Operator { + + value: R; + + constructor(value: R) { + this.value = value; + } + + call(subscriber: Subscriber, source: any): any { + return source.subscribe(new MapToSubscriber(subscriber, this.value)); + } +} + +/** + * We need this JSDoc comment for affecting ESDoc. + * @ignore + * @extends {Ignored} + */ +class MapToSubscriber extends Subscriber { + + value: R; + + constructor(destination: Subscriber, value: R) { + super(destination); + this.value = value; + } + + protected _next(x: T) { + this.destination.next(this.value); + } +} \ No newline at end of file