@rxjs-ninja/rxjs-utility
Version:
Useful utilities for RxJS
40 lines (39 loc) • 1.63 kB
TypeScript
/**
* @packageDocumentation
* @module Utility
*/
import { MapFn, PredicateFn } from '../types/utility';
import { OperatorFunction } 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 = [ 1, 2, 3, 4, 5, 6, 10, 15, 16 ];
* from(input).pipe(
* mapIf<number, string, number>(
* (value) => value % 15 == 0 || value % 3 == 0 || value % 5 == 0,
* (value) => (value % 15 == 0 ? `FizzBuzz` : value % 3 === 0 ? 'Fizz' : 'Buzz'),
* (value) => value,
* ),
* ).subscribe();
* ```
* Output: `1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 10, 'FizzBuzz', 16`
*
* @returns Observable that emits a value from the truthy or falsy [[MapFn]] based on the [[PredicateFn]] result
*/
export declare function mapIf<I extends unknown, T = I | unknown, F = I | unknown>(predicate: PredicateFn<I>, trueResult: MapFn<I, T>, falseResult: MapFn<I, F>): OperatorFunction<I, T | F>;