Skip to content

Commit 5f1fe72

Browse files
committed
fix: throw better error message on invalid date formatter
Closes #897
1 parent ae1949a commit 5f1fe72

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

projects/angular-calendar/src/modules/common/calendar-date.pipe.ts

+10
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ export class CalendarDatePipe implements PipeTransform {
2525
excludeDays: number[] = [],
2626
daysInWeek?: number
2727
): string {
28+
if (typeof this.dateFormatter[method] === 'undefined') {
29+
const allowedMethods = Object.getOwnPropertyNames(
30+
Object.getPrototypeOf(CalendarDateFormatter.prototype)
31+
).filter(iMethod => iMethod !== 'constructor');
32+
throw new Error(
33+
`${method} is not a valid date formatter. Can only be one of ${allowedMethods.join(
34+
', '
35+
)}`
36+
);
37+
}
2838
return this.dateFormatter[method]({
2939
date,
3040
locale,

projects/angular-calendar/test/calendar-date.pipe.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,15 @@ describe('calendarDate pipe', () => {
9595
weekStartsOn: 0
9696
});
9797
});
98+
99+
it('should throw when an invalid method is passed', () => {
100+
const fixture: ComponentFixture<TestComponent> = TestBed.createComponent(
101+
TestComponent
102+
);
103+
fixture.componentInstance.date = new Date('2016-01-01');
104+
fixture.componentInstance.method = 'invalid';
105+
expect(() => fixture.detectChanges()).to.throw(
106+
/^invalid is not a valid date formatter. Can only be one of/
107+
);
108+
});
98109
});

0 commit comments

Comments
 (0)