From 0e197f5b942b1e0016e56187bbf843c833ef76b1 Mon Sep 17 00:00:00 2001 From: Austin McDaniel Date: Mon, 24 Oct 2016 09:25:15 -0400 Subject: [PATCH] (bug): add support for undefined date (#41) --- src/date-format.pipe.spec.ts | 13 +++++++++++++ src/date-format.pipe.ts | 3 ++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/date-format.pipe.spec.ts b/src/date-format.pipe.spec.ts index c8d3200..d7b42cd 100644 --- a/src/date-format.pipe.spec.ts +++ b/src/date-format.pipe.spec.ts @@ -10,5 +10,18 @@ describe('DateFormatPipe', () => { const result = pipe.transform(moment('2016-01-24 01:23:45'), 'MMMM Do YYYY, h:mm:ss a'); expect(result).toBe('January 24th 2016, 1:23:45 am'); }); + + it('should not format empty dates', () => { + const pipe = new DateFormatPipe(); + + const result1 = pipe.transform('', 'MMMM Do YYYY, h:mm:ss a'); + expect(result1).toBe(''); + + const result2 = pipe.transform(null); + expect(result2).toBe(''); + + const result3 = pipe.transform(undefined); + expect(result3).toBe(''); + }); }); }); diff --git a/src/date-format.pipe.ts b/src/date-format.pipe.ts index 028d6f2..059768d 100644 --- a/src/date-format.pipe.ts +++ b/src/date-format.pipe.ts @@ -8,7 +8,8 @@ const momentConstructor: (value?: any) => moment.Moment = (moment).default @Pipe({ name: 'amDateFormat' }) export class DateFormatPipe implements PipeTransform { - transform(value: Date | moment.Moment, ...args: any[]): string { + transform(value: Date | moment.Moment | string | number, ...args: any[]): string { + if (!value) return ''; return momentConstructor(value).format(args[0]); } }