Skip to content

Commit

Permalink
Return empty string when value is null/undefined using MarkdownPipe (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jfcere authored Jan 26, 2019
1 parent 6a711e7 commit 466c129
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/src/markdown.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,19 @@ describe('MarkdownPipe', () => {
pipe = new MarkdownPipe(elementRef, markdownService, zone);
});

it('should return empty string when value is null/undefined', () => {

const markdowns: any[] = [undefined, null];

markdowns.forEach(markdown => {
const result = pipe.transform(markdown);
expect(result).toBe('');
});
});

it('should log error and return value when parameter is not a string', () => {

const markdowns: any[] = [undefined, null, 0, {}, [], /regex/];
const markdowns: any[] = [0, {}, [], /regex/];

spyOn(console, 'error');

Expand Down
4 changes: 4 additions & 0 deletions lib/src/markdown.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export class MarkdownPipe implements PipeTransform {
) { }

transform(value: string): string {
if (value == null) {
return '';
}

if (typeof value !== 'string') {
console.error(`MarkdownPipe has been invoked with an invalid value type [${value}]`);
return value;
Expand Down

0 comments on commit 466c129

Please sign in to comment.