@@ -3,6 +3,8 @@ import { errors } from '../errors';
33import localeTerritory from './territory' ;
44import parseRangeDate from './parse-range-date' ;
55
6+ /* eslint-disable consistent-return */
7+
68const {
79 NoCurrency,
810 NoCurrencyDisplay,
@@ -21,17 +23,25 @@ const GLOBAL_CURRENCIES = {
2123
2224} ;
2325
24- function getCurrencyInfo ( locale , currency ) {
26+ function getCurrencyInfo ( locale , currency , throwIfNoValid ) {
2527 const info = getLocaleInfo ( locale ) ;
2628 const currencies = info . numbers . currencies ;
2729 if ( ! currencies ) {
28- throw NoCurrency . error ( ) ;
30+ if ( throwIfNoValid ) {
31+ throw NoCurrency . error ( ) ;
32+ }
33+
34+ return ;
2935 }
3036
3137 const currencyDisplayInfo = currencies [ currency ] ;
3238
3339 if ( ! currencyDisplayInfo ) {
34- throw NoCurrencyDisplay . error ( ) ;
40+ if ( throwIfNoValid ) {
41+ throw NoCurrencyDisplay . error ( ) ;
42+ }
43+
44+ return ;
3545 }
3646
3747 return currencyDisplayInfo ;
@@ -73,8 +83,12 @@ function regionCurrency(regionCurrencies) {
7383 return latestStillValid || latestValidUntil ;
7484}
7585
76- export function currencyDisplays ( locale , currency ) {
77- const currencyInfo = getCurrencyInfo ( locale , currency ) ;
86+ export function currencyDisplays ( locale , currency , throwIfNoValid = true ) {
87+ const currencyInfo = getCurrencyInfo ( locale , currency , throwIfNoValid ) ;
88+ if ( ! currencyInfo ) {
89+ return ;
90+ }
91+
7892 if ( ! currencyInfo . displays ) {
7993 const displays = [ currency ] ;
8094 for ( let field in currencyInfo ) {
@@ -94,7 +108,7 @@ export function currencyDisplay(locale, options) {
94108 return currency ;
95109 }
96110
97- const currencyInfo = getCurrencyInfo ( locale , currency ) ;
111+ const currencyInfo = getCurrencyInfo ( locale , currency , true ) ;
98112 let result ;
99113
100114 if ( currencyDisplay === SYMBOL ) {
@@ -126,20 +140,28 @@ export function currencyFractionOptions(code) {
126140 } ;
127141}
128142
129- export function territoryCurrencyCode ( territory ) {
143+ export function territoryCurrencyCode ( territory , throwIfNoValid = true ) {
130144 if ( GLOBAL_CURRENCIES [ territory ] ) {
131145 return GLOBAL_CURRENCIES [ territory ] ;
132146 }
133147
134148 const currencyData = cldr . supplemental . currencyData ;
135149 if ( ! currencyData ) {
136- throw NoSupplementalCurrency . error ( ) ;
150+ if ( throwIfNoValid ) {
151+ throw NoSupplementalCurrency . error ( ) ;
152+ }
153+
154+ return ;
137155 }
138156
139157 const regionCurrencies = currencyData . region [ territory ] ;
140158
141159 if ( ! regionCurrencies ) {
142- throw NoCurrencyRegion . error ( territory ) ;
160+ if ( throwIfNoValid ) {
161+ throw NoCurrencyRegion . error ( territory ) ;
162+ }
163+
164+ return ;
143165 }
144166
145167 const currencyCode = regionCurrency ( regionCurrencies ) ;
@@ -152,7 +174,7 @@ export function localeCurrency(locale, throwIfNoValid) {
152174 const numbers = info . numbers ;
153175
154176 if ( ! numbers . localeCurrency ) {
155- const currency = territoryCurrencyCode ( localeTerritory ( info ) ) ;
177+ const currency = territoryCurrencyCode ( localeTerritory ( info ) , throwIfNoValid ) ;
156178
157179 if ( ! currency && throwIfNoValid ) {
158180 throw NoValidCurrency . error ( info . name ) ;
0 commit comments