Debian Codegraph @

node/rxjs/src/internal/operators/findIndex.ts

raw
1 import { Observable } from '../Observable';
2 import { Falsy, OperatorFunction } from '../types';
3 import { operate } from '../util/lift';
4 import { createFind } from './find';
5
6 export function findIndex<T>(predicate: BooleanConstructor): OperatorFunction<T, T extends Falsy ? -1 : number>;
7 /** @deprecated Use a closure instead of a `thisArg`. Signatures accepting a `thisArg` will be removed in v8. */
8 export function findIndex<T>(predicate: BooleanConstructor, thisArg: any): OperatorFunction<T, T extends Falsy ? -1 : number>;
9 /** @deprecated Use a closure instead of a `thisArg`. Signatures accepting a `thisArg` will be removed in v8. */
10 export function findIndex<T, A>(
11 predicate: (this: A, value: T, index: number, source: Observable<T>) => boolean,
12 thisArg: A
13 ): OperatorFunction<T, number>;
14 export function findIndex<T>(predicate: (value: T, index: number, source: Observable<T>) => boolean): OperatorFunction<T, number>;
15
16 /**
17 * Emits only the index of the first value emitted by the source Observable that
18 * meets some condition.
19 *
20 * <span class="informal">It's like {@link find}, but emits the index of the
21 * found value, not the value itself.</span>
22 *
23 * ![](findIndex.png)
24 *
25 * `findIndex` searches for the first item in the source Observable that matches
26 * the specified condition embodied by the `predicate`, and returns the
27 * (zero-based) index of the first occurrence in the source. Unlike
28 * {@link first}, the `predicate` is required in `findIndex`, and does not emit
29 * an error if a valid value is not found.
30 *
31 * ## Example
32 *
33 * Emit the index of first click that happens on a DIV element
34 *
35 * ```ts
36 * import { fromEvent, findIndex } from 'rxjs';
37 *
38 * const div = document.createElement('div');
39 * div.style.cssText = 'width: 200px; height: 200px; background: #09c;';
40 * document.body.appendChild(div);
41 *
42 * const clicks = fromEvent(document, 'click');
43 * const result = clicks.pipe(findIndex(ev => (<HTMLElement>ev.target).tagName === 'DIV'));
44 * result.subscribe(x => console.log(x));
45 * ```
46 *
47 * @see {@link filter}
48 * @see {@link find}
49 * @see {@link first}
50 * @see {@link take}
51 *
52 * @param predicate A function called with each item to test for condition matching.
53 * @param thisArg An optional argument to determine the value of `this` in the
54 * `predicate` function.
55 * @return A function that returns an Observable that emits the index of the
56 * first item that matches the condition.
57 */
58 export function findIndex<T>(
59 predicate: (value: T, index: number, source: Observable<T>) => boolean,
60 thisArg?: any
61 ): OperatorFunction<T, number> {
62 return operate(createFind(predicate, thisArg, 'index'));
63 }