58 lines
2.1 KiB
JavaScript
58 lines
2.1 KiB
JavaScript
"use strict";
|
|
var __extends = (this && this.__extends) || function (d, b) {
|
|
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
|
|
function __() { this.constructor = d; }
|
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
};
|
|
var Subscriber_1 = require('../Subscriber');
|
|
/**
|
|
* Similar to the well known `Array.prototype.map` function, this operator
|
|
* applies a projection to each value and emits that projection in the returned observable
|
|
*
|
|
* <img src="./img/map.png" width="100%">
|
|
*
|
|
* @param {Function} project the function to create projection
|
|
* @param {any} [thisArg] an optional argument to define what `this` is in the project function
|
|
* @returns {Observable} a observable of projected values
|
|
*/
|
|
function map(project, thisArg) {
|
|
if (typeof project !== 'function') {
|
|
throw new TypeError('argument is not a function. Are you looking for `mapTo()`?');
|
|
}
|
|
return this.lift(new MapOperator(project, thisArg));
|
|
}
|
|
exports.map = map;
|
|
var MapOperator = (function () {
|
|
function MapOperator(project, thisArg) {
|
|
this.project = project;
|
|
this.thisArg = thisArg;
|
|
}
|
|
MapOperator.prototype.call = function (subscriber) {
|
|
return new MapSubscriber(subscriber, this.project, this.thisArg);
|
|
};
|
|
return MapOperator;
|
|
}());
|
|
var MapSubscriber = (function (_super) {
|
|
__extends(MapSubscriber, _super);
|
|
function MapSubscriber(destination, project, thisArg) {
|
|
_super.call(this, destination);
|
|
this.project = project;
|
|
this.count = 0;
|
|
this.thisArg = thisArg || this;
|
|
}
|
|
// NOTE: This looks unoptimized, but it's actually purposefully NOT
|
|
// using try/catch optimizations.
|
|
MapSubscriber.prototype._next = function (value) {
|
|
var result;
|
|
try {
|
|
result = this.project.call(this.thisArg, value, this.count++);
|
|
}
|
|
catch (err) {
|
|
this.destination.error(err);
|
|
return;
|
|
}
|
|
this.destination.next(result);
|
|
};
|
|
return MapSubscriber;
|
|
}(Subscriber_1.Subscriber));
|
|
//# sourceMappingURL=map.js.map
|