Skip to content

Commit

Permalink
Merge pull request #105 from swimlane/master
Browse files Browse the repository at this point in the history
(bug): add support for undefined date (#41)
  • Loading branch information
urish authored Oct 24, 2016
2 parents eb60960 + 0e197f5 commit 58b1f5c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/date-format.pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
});
});
});
3 changes: 2 additions & 1 deletion src/date-format.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const momentConstructor: (value?: any) => moment.Moment = (<any>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]);
}
}

0 comments on commit 58b1f5c

Please sign in to comment.