b083933b9236ebc5d16a9a18edd93aaee0536506ac9b5d271d07260a5b6e9a6ac6ddd4e8594ed3c0c6cab97a4e7bc3660e4fe7b2927a53527eebff6e777bf3 3.4 KB

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