@rxjs-ninja/rxjs-utility
Version:
Useful utilities for RxJS
40 lines (39 loc) • 1.53 kB
TypeScript
/**
* @packageDocumentation
* @module Utility
*/
import { MapFn, PredicateFn } from '../types/utility';
import { OperatorFunction, Subscribable } from 'rxjs';
/**
* Returns an Observable that emits the value from either the `trueResult` or `falseResult` based on the result from
* the source with a [[PredicateFn]].
*
* @category Mapping
*
* @remarks Each method can return it's own type which you should handle in later operators
*
* @typeParam I The type of value from the source
* @typeParam T The type returned from the Truthy result
* @typeParam F The type returned from the Falsy result
*
* @param predicate The method to check the value from the source Observable
* @param trueResult The method with return value for a truthy [[PredicateFn]]
* @param falseResult The method with return value for a falsy [[PredicateFn]]
*
* @example
* Returns a FizzBuzz based on the input value
* ```ts
* const input = [ 12, 5, 6, 1, 3, 10 ];
* from(input).pipe(
* switchMapIf<number, boolean>(
* (value) => value <= 6,
* (value) => of(true),
* (value) => of(false),
* ),
* ).subscribe();
* ```
* Output: `true, false, true, false, false, true`
*
* @returns Observable that emits a value based on the [[PredicateFn]] result
*/
export declare function switchMapIf<I extends unknown, T = I | unknown, F = I | unknown>(predicate: PredicateFn<I>, trueResult: MapFn<I, Subscribable<T>>, falseResult: MapFn<I, Subscribable<F>>): OperatorFunction<I, T | F>;