Skip to content

Commit

Permalink
feat: added tapLog()
Browse files Browse the repository at this point in the history
  • Loading branch information
Derek Burgman committed Feb 1, 2022
1 parent 823259d commit 1d2fd64
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/rxjs/src/lib/rxjs/misc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { MonoTypeOperatorFunction, tap } from 'rxjs';

/**
* Used to log a message to the console.
*
* @param messageOrFunction
* @returns
*/
export function tapLog<T = any>(message: string | number, consoleLogFn?: 'log' | 'warn' | 'error'): MonoTypeOperatorFunction<T>;
export function tapLog<T = any>(messageFunction: (value: T) => any[], consoleLogFn?: 'log' | 'warn' | 'error'): MonoTypeOperatorFunction<T>;
export function tapLog<T = any>(messageOrFunction: (string | number) | ((value: T) => any[]), consoleLogFn: 'log' | 'warn' | 'error' = 'log'): MonoTypeOperatorFunction<T> {
let operator: MonoTypeOperatorFunction<T>;

if (typeof messageOrFunction === 'function') {
operator = tap((x) => console[consoleLogFn].apply(undefined, messageOrFunction(x)));
} else {
operator = tap((x) => console[consoleLogFn](`${messageOrFunction}`, x));
}

return operator;
}

0 comments on commit 1d2fd64

Please sign in to comment.