Skip to content

Commit

Permalink
fix: 🐛 Make all adapter constructor parameters optional. Make enUS lo…
Browse files Browse the repository at this point in the history
…cale default when no locales provided.
  • Loading branch information
sapozhnikovay committed Jul 1, 2021
1 parent c167d1d commit 3b37f7b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ describe('NgxDateFnsDateAdapter', () => {

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

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

it('should throw when attempting to set locale without providing it in the NGX_MAT_DATEFNS_LOCALES token', () => {
expect(() => adapter.setLocale('ru')).toThrowError(
/locale \'ru\' does not exist/
/locale \'ru\' does not exist in locales array. Add it to the NGX_MAT_DATEFNS_LOCALES token./
);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,30 @@ export class NgxDateFnsDateAdapter extends DateAdapter<Date> {
return localeCodeOrLocale as Locale;
}
if (!this.locales || !this.locales.length) {
throw new Error('locales array does not provided or is empty');
throw new Error('locales array does not provided or is empty. Provide it via the NGX_MAT_DATEFNS_LOCALES token.');
}
const locale = this.locales.find(
(item) => item.code === localeCodeOrLocale
);
if (!locale) {
throw new Error(`locale '${localeCodeOrLocale}' does not exist`);
throw new Error(`locale '${localeCodeOrLocale}' does not exist in locales array. Add it to the NGX_MAT_DATEFNS_LOCALES token.`);
}
return locale;
};

constructor(
@Optional() @Inject(MAT_DATE_LOCALE) dateLocale: string,
@Inject(NGX_MAT_DATEFNS_LOCALES) private locales: Locale[],
@Optional() @Inject(MAT_DATE_LOCALE) dateLocale: string | null,
@Optional() @Inject(NGX_MAT_DATEFNS_LOCALES) private locales: Locale[] | null,
@Optional()
@Inject(NGX_MAT_DATEFNS_DATE_ADAPTER_OPTIONS)
private options?: NgxDateFnsDateAdapterOptions
) {
super();

if (!this.locales || this.locales.length === 0) {
this.locales = [enUS];
}

try {
this.setLocale(dateLocale || enUS);
} catch (err) {
Expand Down

0 comments on commit 3b37f7b

Please sign in to comment.