Debian Codegraph @

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

raw
1 import { Observable } from '../Observable';
2 import { EmptyError } from '../util/EmptyError';
3 import { OperatorFunction, TruthyTypesOf } from '../types';
4 import { filter } from './filter';
5 import { takeLast } from './takeLast';
6 import { throwIfEmpty } from './throwIfEmpty';
7 import { defaultIfEmpty } from './defaultIfEmpty';
8 import { identity } from '../util/identity';
9
10 export function last<T>(predicate: BooleanConstructor): OperatorFunction<T, TruthyTypesOf<T>>;
11 export function last<T, D>(predicate: BooleanConstructor, defaultValue: D): OperatorFunction<T, TruthyTypesOf<T> | D>;
12 export function last<T, D = T>(predicate?: null, defaultValue?: D): OperatorFunction<T, T | D>;
13 export function last<T, S extends T>(
14 predicate: (value: T, index: number, source: Observable<T>) => value is S,
15 defaultValue?: S
16 ): OperatorFunction<T, S>;
17 export function last<T, D = T>(
18 predicate: (value: T, index: number, source: Observable<T>) => boolean,
19 defaultValue?: D
20 ): OperatorFunction<T, T | D>;
21
22 /**
23 * Returns an Observable that emits only the last item emitted by the source Observable.
24 * It optionally takes a predicate function as a parameter, in which case, rather than emitting
25 * the last item from the source Observable, the resulting Observable will emit the last item
26 * from the source Observable that satisfies the predicate.
27 *
28 * ![](last.png)
29 *
30 * It will emit an error notification if the source completes without notification or one that matches
31 * the predicate. It returns the last value or if a predicate is provided last value that matches the
32 * predicate. It returns the given default value if no notification is emitted or matches the predicate.
33 *
34 * ## Examples
35 *
36 * Last alphabet from the sequence
37 *
38 * ```ts
39 * import { from, last } from 'rxjs';
40 *
41 * const source = from(['x', 'y', 'z']);
42 * const result = source.pipe(last());
43 *
44 * result.subscribe(value => console.log(`Last alphabet: ${ value }`));
45 *
46 * // Outputs
47 * // Last alphabet: z
48 * ```
49 *
50 * Default value when the value in the predicate is not matched
51 *
52 * ```ts
53 * import { from, last } from 'rxjs';
54 *
55 * const source = from(['x', 'y', 'z']);
56 * const result = source.pipe(last(char => char === 'a', 'not found'));
57 *
58 * result.subscribe(value => console.log(`'a' is ${ value }.`));
59 *
60 * // Outputs
61 * // 'a' is not found.
62 * ```
63 *
64 * @see {@link skip}
65 * @see {@link skipUntil}
66 * @see {@link skipLast}
67 * @see {@link skipWhile}
68 * @see {@link first}
69 *
70 * @throws {EmptyError} Delivers an `EmptyError` to the Observer's `error`
71 * callback if the Observable completes before any `next` notification was sent.
72 *
73 * @param predicate The condition any source emitted item has to satisfy.
74 * @param defaultValue An optional default value to provide if last `predicate`
75 * isn't met or no values were emitted.
76 * @return A function that returns an Observable that emits only the last item
77 * satisfying the given condition from the source, or an error notification
78 * with an `EmptyError` object if no such items are emitted.
79 */
80 export function last<T, D>(
81 predicate?: ((value: T, index: number, source: Observable<T>) => boolean) | null,
82 defaultValue?: D
83 ): OperatorFunction<T, T | D> {
84 const hasDefaultValue = arguments.length >= 2;
85 return (source: Observable<T>) =>
86 source.pipe(
87 predicate ? filter((v, i) => predicate(v, i, source)) : identity,
88 takeLast(1),
89 hasDefaultValue ? defaultIfEmpty(defaultValue!) : throwIfEmpty(() => new EmptyError())
90 );
91 }