Skip to content

Commit 543628d

Browse files
danielkaradachkitsvetomir
authored andcommitted
fix: dateFieldName should throw error if locale dateFields are not loaded
1 parent d7f3567 commit 543628d

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/cldr/date-field-name.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import { localeInfo } from './info';
2+
import { errors } from '../errors';
23

34
export default function dateFieldName(options, locale = "en") {
45
const info = localeInfo(locale);
5-
const dateFields = info.calendar.dateFields || {};
6+
const dateFields = info.calendar.dateFields;
7+
if (!dateFields) {
8+
throw errors.NoDateFieldNames.error();
9+
}
10+
611
const fieldNameInfo = dateFields[options.type] || {};
712

813
return fieldNameInfo[options.nameType] || fieldNameInfo['wide'];

src/error-details.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ export default {
1010
"NoGMTInfo": "Cannot determine locale GMT format. Please load the locale timeZoneNames data.",
1111
"NoWeekData": "Cannot determine locale first day of week. Please load the supplemental weekData.",
1212
"NoFirstDay": "Cannot determine locale first day of week. Please load the supplemental weekData. The default culture includes only the 'en-US' first day info.",
13-
"NoValidCurrency": "Cannot determine a default currency for the {0} locale. Please specify explicitly the currency with the format options."
13+
"NoValidCurrency": "Cannot determine a default currency for the {0} locale. Please specify explicitly the currency with the format options.",
14+
"NoDateFieldNames": "Cannot determine the locale date field names. Please load the locale dateFields data."
1415
};

test/cldr.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,20 @@ describe('dateFormatNames', () => {
379379
});
380380

381381
describe('dateFieldName', () => {
382+
it('should throw error if locale dateFields are not available', () => {
383+
const calendar = cldr.en.calendar;
384+
const dateFields = calendar.dateFields;
385+
delete calendar.dateFields;
386+
try {
387+
expect(() => {
388+
dateFieldName({ type: 'day', nameType: 'wide' });
389+
}).toThrowError(/Cannot determine the locale date field names/);
390+
} finally {
391+
calendar.dateFields = dateFields;
392+
}
393+
394+
});
395+
382396
it('should return placeholder for the era type', () => {
383397
expect(dateFieldName({ type: 'era', nameType: 'wide' })).toEqual("era");
384398
expect(dateFieldName({ type: 'era', nameType: 'narrow' })).toEqual("era");

0 commit comments

Comments
 (0)