UNPKG

wed

Version:

Wed is a schema-aware editor for XML documents.

66 lines (63 loc) 2.67 kB
define(function(require,exports,module){ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var ArgumentOutOfRangeError_1 = require("../util/ArgumentOutOfRangeError"); var filter_1 = require("./filter"); var throwIfEmpty_1 = require("./throwIfEmpty"); var defaultIfEmpty_1 = require("./defaultIfEmpty"); var take_1 = require("./take"); /** * Emits the single value at the specified `index` in a sequence of emissions * from the source Observable. * * <span class="informal">Emits only the i-th value, then completes.</span> * * <img src="./img/elementAt.png" width="100%"> * * `elementAt` returns an Observable that emits the item at the specified * `index` in the source Observable, or a default value if that `index` is out * of range and the `default` argument is provided. If the `default` argument is * not given and the `index` is out of range, the output Observable will emit an * `ArgumentOutOfRangeError` error. * * @example <caption>Emit only the third click event</caption> * var clicks = Rx.Observable.fromEvent(document, 'click'); * var result = clicks.elementAt(2); * result.subscribe(x => console.log(x)); * * // Results in: * // click 1 = nothing * // click 2 = nothing * // click 3 = MouseEvent object logged to console * * @see {@link first} * @see {@link last} * @see {@link skip} * @see {@link single} * @see {@link take} * * @throws {ArgumentOutOfRangeError} When using `elementAt(i)`, it delivers an * ArgumentOutOrRangeError to the Observer's `error` callback if `i < 0` or the * Observable has completed before emitting the i-th `next` notification. * * @param {number} index Is the number `i` for the i-th source emission that has * happened since the subscription, starting from the number `0`. * @param {T} [defaultValue] The default value returned for missing indices. * @return {Observable} An Observable that emits a single item, if it is found. * Otherwise, will emit the default value if given. If not, then emits an error. * @method elementAt * @owner Observable */ function elementAt(index, defaultValue) { if (index < 0) { throw new ArgumentOutOfRangeError_1.ArgumentOutOfRangeError(); } var hasDefaultValue = arguments.length >= 2; return function (source) { return source.pipe(filter_1.filter(function (v, i) { return i === index; }), take_1.take(1), hasDefaultValue ? defaultIfEmpty_1.defaultIfEmpty(defaultValue) : throwIfEmpty_1.throwIfEmpty(function () { return new ArgumentOutOfRangeError_1.ArgumentOutOfRangeError(); })); }; } exports.elementAt = elementAt; //# sourceMappingURL=elementAt.js.map return module.exports; });