Skip to content

Commit 3b37f7b

Browse files
committed
fix: 🐛 Make all adapter constructor parameters optional. Make enUS locale default when no locales provided.
1 parent c167d1d commit 3b37f7b

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

projects/ngx-mat-datefns-date-adapter/src/lib/ngx-mat-datefns-date-adapter.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ describe('NgxDateFnsDateAdapter', () => {
316316

317317
it('should throw when attempting to set locale via string without providing NGX_MAT_DATEFNS_LOCALES token', () => {
318318
expect(() => adapter.setLocale('invalid')).toThrowError(
319-
/locales array does not provided or is empty/
319+
/locale 'invalid' does not exist in locales array. Add it to the NGX_MAT_DATEFNS_LOCALES token./
320320
);
321321
});
322322

@@ -643,7 +643,7 @@ describe('NgxDateFnsDateAdapter with NGX_MAT_DATEFNS_LOCALES set', () => {
643643

644644
it('should throw when attempting to set locale without providing it in the NGX_MAT_DATEFNS_LOCALES token', () => {
645645
expect(() => adapter.setLocale('ru')).toThrowError(
646-
/locale \'ru\' does not exist/
646+
/locale \'ru\' does not exist in locales array. Add it to the NGX_MAT_DATEFNS_LOCALES token./
647647
);
648648
});
649649
});

projects/ngx-mat-datefns-date-adapter/src/lib/ngx-mat-datefns-date-adapter.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,26 +60,30 @@ export class NgxDateFnsDateAdapter extends DateAdapter<Date> {
6060
return localeCodeOrLocale as Locale;
6161
}
6262
if (!this.locales || !this.locales.length) {
63-
throw new Error('locales array does not provided or is empty');
63+
throw new Error('locales array does not provided or is empty. Provide it via the NGX_MAT_DATEFNS_LOCALES token.');
6464
}
6565
const locale = this.locales.find(
6666
(item) => item.code === localeCodeOrLocale
6767
);
6868
if (!locale) {
69-
throw new Error(`locale '${localeCodeOrLocale}' does not exist`);
69+
throw new Error(`locale '${localeCodeOrLocale}' does not exist in locales array. Add it to the NGX_MAT_DATEFNS_LOCALES token.`);
7070
}
7171
return locale;
7272
};
7373

7474
constructor(
75-
@Optional() @Inject(MAT_DATE_LOCALE) dateLocale: string,
76-
@Inject(NGX_MAT_DATEFNS_LOCALES) private locales: Locale[],
75+
@Optional() @Inject(MAT_DATE_LOCALE) dateLocale: string | null,
76+
@Optional() @Inject(NGX_MAT_DATEFNS_LOCALES) private locales: Locale[] | null,
7777
@Optional()
7878
@Inject(NGX_MAT_DATEFNS_DATE_ADAPTER_OPTIONS)
7979
private options?: NgxDateFnsDateAdapterOptions
8080
) {
8181
super();
8282

83+
if (!this.locales || this.locales.length === 0) {
84+
this.locales = [enUS];
85+
}
86+
8387
try {
8488
this.setLocale(dateLocale || enUS);
8589
} catch (err) {

0 commit comments

Comments
 (0)