Skip to content

Commit 6f8c37f

Browse files
committed
chore: validate currency data region
1 parent 253d3b4 commit 6f8c37f

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/cldr/currency.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ export function territoryCurrencyCode(territory) {
7676
if (!currencyData) {
7777
throw new Error("Cannot determine currency. Please load the supplemental currencyData.");
7878
}
79+
7980
const regionCurrencies = currencyData.region[territory];
81+
if (!regionCurrencies) {
82+
throw new Error("No currency data for region " + territory);
83+
}
8084
const currencyCode = Object.keys(regionCurrencies[regionCurrencies.length - 1])[0];
8185

8286
return currencyCode;
@@ -89,4 +93,4 @@ export function localeCurrency(locale) {
8993
numbers.localeCurrency = territoryCurrencyCode(localeTerritory(info));
9094
}
9195
return numbers.localeCurrency;
92-
}
96+
}

test/numbers.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ const likelySubtags = require("cldr-data/supplemental/likelySubtags.json");
55
const numbers = require("cldr-data/main/bg/numbers.json");
66
const currencies = require("cldr-data/main/bg/currencies.json");
77
const currencyData = require("cldr-data/supplemental/currencyData.json");
8+
9+
// CUSTOM region is used in tests below
10+
currencyData.supplemental.currencyData.region.CUSTOM = [{}];
11+
812
load(likelySubtags, currencyData, numbers, currencies);
913

1014
function loadCustom(options) {
@@ -239,6 +243,14 @@ describe('standard currency formatting', () => {
239243
expect(formatNumber(123, "c")).toEqual("$123.00");
240244
});
241245

246+
it("should apply format when passing language", () => {
247+
expect(formatNumber(10, "c", "bg")).toEqual("10,00 лв.");
248+
});
249+
250+
it("should apply format when passing language and territory", () => {
251+
expect(formatNumber(10, "c", "bg-BG")).toEqual("10,00 лв.");
252+
});
253+
242254
it("should apply format when passing object", () => {
243255
expect(formatNumber(10, { style: "currency" })).toEqual("$10.00");
244256
});

0 commit comments

Comments
 (0)