Last active
November 8, 2023 19:35
-
-
Save elgervb/2d3004e84831016d10123f6599ddff45 to your computer and use it in GitHub Desktop.
Angular debug pipe to debug values in templates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Pipe, PipeTransform } from '@angular/core'; | |
/** | |
* Debug pipe to debug template statements. | |
* It will always return the value it gets passed, so we can pipe it along with other pipes. | |
* | |
* @example | |
* ``` | |
<!-- only log to console --> | |
<compA [binding]= "someObj | smtDebug"></compA> | |
<!-- log to console and trigger breakpoint --> | |
<compB [binding]= "someObj | smtDebug:true"></compB> | |
<!-- also works with async pipe --> | |
<compC [binding]= "(someObs$ | async) | smtDebug:true"></compC> | |
<!-- skip null or undefined emits --> | |
<compD [binding]= "(someObs$ | async) | smtDebug:true:true"></compD> | |
<!-- is pipeable with other pipes --> | |
<compE [binding]= "(someObs$ | async) | smtDebug:true | json"></compE> | |
* ``` | |
*/ | |
@Pipe({ | |
name: 'appDebug' | |
}) | |
export class DebugPipe implements PipeTransform { | |
// tslint:disable-next-line:no-any | |
transform(value: any, isDebugger = false, skipNullOrUndefined = false) { | |
if (skipNullOrUndefined && (value === null || value === undefined)) { | |
return value; | |
} | |
console.dir(value); | |
if (isDebugger) { | |
// tslint:disable-next-line:no-debugger | |
debugger; | |
} | |
return value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment