Skip to content

Commit 3fefdfd

Browse files
committed
refactor: use new IntlError methods
1 parent 4356444 commit 3fefdfd

File tree

6 files changed

+21
-12
lines changed

6 files changed

+21
-12
lines changed

src/cldr/currency.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,27 @@ import { cldr, getLocaleInfo } from './info';
22
import localeTerritory from './territory';
33
import { errors } from '../errors';
44

5+
const {
6+
NoCurrency,
7+
NoCurrencyDisplay,
8+
NoSupplementalCurrency,
9+
NoCurrencyRegion
10+
} = errors;
11+
512
const DEFAULT_CURRENCY_FRACTIONS = 2;
613
const SYMBOL = "symbol";
714

815
function getCurrencyInfo(locale, currency) {
916
const info = getLocaleInfo(locale);
1017
const currencies = info.numbers.currencies;
1118
if (!currencies) {
12-
throw new Error(errors.NoCurrencyError.formatMessage());
19+
throw NoCurrency.error();
1320
}
1421

1522
const currencyDisplayInfo = currencies[currency];
1623

1724
if (!currencyDisplayInfo) {
18-
throw new Error(errors.NoCurrencyDisplayError.formatMessage());
25+
throw NoCurrencyDisplay.error();
1926
}
2027

2128
return currencyDisplayInfo;
@@ -81,12 +88,12 @@ export function currencyFractionOptions(code) {
8188
export function territoryCurrencyCode(territory) {
8289
const currencyData = cldr.supplemental.currencyData;
8390
if (!currencyData) {
84-
throw new Error(errors.NoSupplementalCurrencyError.formatMessage());
91+
throw NoSupplementalCurrency.error();
8592
}
8693

8794
const regionCurrencies = currencyData.region[territory];
8895
if (!regionCurrencies) {
89-
throw new Error(errors.NoCurrencyRegionError.formatMessage(territory));
96+
throw NoCurrencyRegion.error();
9097
}
9198
const currencyCode = Object.keys(regionCurrencies[regionCurrencies.length - 1])[0];
9299

src/cldr/first-day.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@ import localeTerritory from './territory';
33

44
import { errors } from '../errors';
55

6+
const { NoWeekData, NoFirstDay } = errors;
7+
68
const DAYS = [ "sun", "mon", "tue", "wed", "thu", "fri", "sat" ];
79

810
export default function firstDay(locale) {
911
const weekData = cldr.supplemental.weekData;
1012
if (!weekData) {
11-
throw new Error(errors.NoWeekDataError.formatMessage());
13+
throw NoWeekData.error();
1214
}
1315

1416
const info = getLocaleInfo(locale);
1517
const firstDay = weekData.firstDay[localeTerritory(info)];
1618

1719
if (!firstDay) {
18-
throw new Error(errors.NoFirstDayError.formatMessage());
20+
throw NoFirstDay.error();
1921
}
2022

2123
return DAYS.indexOf(firstDay);

src/cldr/info.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,5 @@ export function localeInfo(locale) {
5454
}
5555
}
5656

57-
throw new Error(errors.NoLocaleError.formatMessage(locale));
57+
throw errors.NoLocale.error(locale);
5858
}

src/dates/parse-date.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ function checkLiteral(state) {
113113
function calendarGmtFormats(calendar) {
114114
const { gmtFormat, gmtZeroFormat } = calendar;
115115
if (!gmtFormat) {
116-
throw new Error(errors.NoGMTInfoError.formatMessage());
116+
throw errors.NoGMTInfo.error();
117117
}
118118

119119
return [ gmtFormat.replace(PLACEHOLDER, "").toLowerCase(), gmtZeroFormat.replace(PLACEHOLDER, "").toLowerCase() ];

test/cldr-default-data.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ describe('currencyDisplay', () => {
77
currencyDisplay('de', {
88
currency: 'USD'
99
});
10-
}).toThrowError(new RegExp(errors.NoLocaleError.name));
10+
}).toThrowError(new RegExp(errors.NoLocale.name));
1111
});
1212
});
1313

@@ -17,14 +17,14 @@ describe('currencyDisplays', () => {
1717
currencyDisplays('en', {
1818
currency: 'GBP'
1919
});
20-
}).toThrowError(new RegExp(errors.NoCurrencyDisplayError.name));
20+
}).toThrowError(new RegExp(errors.NoCurrencyDisplay.name));
2121
});
2222
});
2323

2424
describe('territoryCurrencyCode', () => {
2525
it('should throw an exception when no region currencies info', () => {
2626
expect(() => {
2727
territoryCurrencyCode('en');
28-
}).toThrowError(new RegExp(errors.NoCurrencyRegionError.name));
28+
}).toThrowError(new RegExp(errors.NoCurrencyRegion.name));
2929
});
3030
});

test/cldr.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ describe('localeInfo', () => {
203203
it('should throw an error if matching locale is no available', () => {
204204
expect(() => {
205205
localeInfo('fr');
206-
}).toThrowError(new RegExp(errors.NoLocaleError.name));
206+
}).toThrowError(new RegExp(errors.NoLocale.name));
207207
});
208208
});
209209

0 commit comments

Comments
 (0)