diff --git a/src/operator/findIndex.ts b/src/operator/findIndex.ts index fedf4857d6..0195ee3785 100644 --- a/src/operator/findIndex.ts +++ b/src/operator/findIndex.ts @@ -1,6 +1,5 @@ import { Observable } from '../Observable'; -import { FindValueOperator } from '../operators/find'; - +import { findIndex as higherOrder } from '../operators'; /** * Emits only the index of the first value emitted by the source Observable that * meets some condition. @@ -37,5 +36,5 @@ import { FindValueOperator } from '../operators/find'; */ export function findIndex(this: Observable, predicate: (value: T, index: number, source: Observable) => boolean, thisArg?: any): Observable { - return this.lift(new FindValueOperator(predicate, this, true, thisArg)); + return higherOrder(predicate, thisArg)(this); } diff --git a/src/operators/findIndex.ts b/src/operators/findIndex.ts new file mode 100644 index 0000000000..b33739a064 --- /dev/null +++ b/src/operators/findIndex.ts @@ -0,0 +1,41 @@ +import { Observable } from '../Observable'; +import { FindValueOperator } from '../operators/find'; +import { OperatorFunction } from '../interfaces'; +/** + * Emits only the index of the first value emitted by the source Observable that + * meets some condition. + * + * It's like {@link find}, but emits the index of the + * found value, not the value itself. + * + * + * + * `findIndex` searches for the first item in the source Observable that matches + * the specified condition embodied by the `predicate`, and returns the + * (zero-based) index of the first occurrence in the source. Unlike + * {@link first}, the `predicate` is required in `findIndex`, and does not emit + * an error if a valid value is not found. + * + * @example Emit the index of first click that happens on a DIV element + * var clicks = Rx.Observable.fromEvent(document, 'click'); + * var result = clicks.findIndex(ev => ev.target.tagName === 'DIV'); + * result.subscribe(x => console.log(x)); + * + * @see {@link filter} + * @see {@link find} + * @see {@link first} + * @see {@link take} + * + * @param {function(value: T, index: number, source: Observable): boolean} predicate + * A function called with each item to test for condition matching. + * @param {any} [thisArg] An optional argument to determine the value of `this` + * in the `predicate` function. + * @return {Observable} An Observable of the index of the first item that + * matches the condition. + * @method find + * @owner Observable + */ +export function findIndex(predicate: (value: T, index: number, source: Observable) => boolean, + thisArg?: any): OperatorFunction { + return (source: Observable) => source.lift(new FindValueOperator(predicate, source, true, thisArg)) as Observable; +} diff --git a/src/operators/index.ts b/src/operators/index.ts index fc06cbe2b0..e13926fee0 100644 --- a/src/operators/index.ts +++ b/src/operators/index.ts @@ -27,6 +27,7 @@ export { expand } from './expand'; export { filter } from './filter'; export { finalize } from './finalize'; export { find } from './find'; +export { findIndex } from './findIndex'; export { ignoreElements } from './ignoreElements'; export { map } from './map'; export { materialize } from './materialize';