Skip to content

Commit

Permalink
feat(findIndex): add higher-order lettable findIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Aug 9, 2017
1 parent ff6d5af commit 40e680e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/operator/findIndex.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -37,5 +36,5 @@ import { FindValueOperator } from '../operators/find';
*/
export function findIndex<T>(this: Observable<T>, predicate: (value: T, index: number, source: Observable<T>) => boolean,
thisArg?: any): Observable<number> {
return <any>this.lift<any>(new FindValueOperator(predicate, this, true, thisArg));
return higherOrder(predicate, thisArg)(this);
}
41 changes: 41 additions & 0 deletions src/operators/findIndex.ts
Original file line number Diff line number Diff line change
@@ -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.
*
* <span class="informal">It's like {@link find}, but emits the index of the
* found value, not the value itself.</span>
*
* <img src="./img/findIndex.png" width="100%">
*
* `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 <caption>Emit the index of first click that happens on a DIV element</caption>
* 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<T>): 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<T>(predicate: (value: T, index: number, source: Observable<T>) => boolean,
thisArg?: any): OperatorFunction<T, number> {
return (source: Observable<T>) => source.lift(new FindValueOperator(predicate, source, true, thisArg)) as Observable<any>;
}
1 change: 1 addition & 0 deletions src/operators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 40e680e

Please sign in to comment.