Fix crash on third-party details when country is GLOBAL
Intl.DisplayNames rejects the GLOBAL pseudo-region, so label resolution now handles it explicitly before rendering the picker. Signed-off-by: Sacha Al Himdani <sacha@probo.com>
This commit is contained in:
committed by
Bryan Frimin
parent
302175617f
commit
2ebf3c180f
43
packages/helpers/src/countries.test.ts
Normal file
43
packages/helpers/src/countries.test.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
// Copyright (c) 2025-2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { getCountryName, getCountryOptions } from "./countries";
|
||||
|
||||
describe("getCountryName", () => {
|
||||
it("returns a label for the GLOBAL pseudo-region", () => {
|
||||
expect(getCountryName("en", "GLOBAL")).toBe("Global");
|
||||
});
|
||||
|
||||
it("returns localized names for ISO country codes", () => {
|
||||
expect(getCountryName("en", "US")).toBe("United States");
|
||||
expect(getCountryName("en", "EU")).toBe("European Union");
|
||||
});
|
||||
});
|
||||
|
||||
describe("getCountryOptions", () => {
|
||||
it("includes GLOBAL without throwing", () => {
|
||||
const options = getCountryOptions("en");
|
||||
expect(options.find(option => option.value === "GLOBAL")).toEqual({
|
||||
value: "GLOBAL",
|
||||
label: "Global",
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -50,8 +50,22 @@ export const countries = [
|
||||
|
||||
export type CountryCode = typeof countries[number];
|
||||
|
||||
// Pseudo-regions accepted by our CountryCode enum but not by Intl.DisplayNames.
|
||||
const pseudoRegionNames: Partial<Record<CountryCode, string>> = {
|
||||
GLOBAL: "Global",
|
||||
};
|
||||
|
||||
export function getCountryName(language: string, code: CountryCode): string {
|
||||
return new Intl.DisplayNames(language, { type: "region" }).of(code) ?? code;
|
||||
const pseudoName = pseudoRegionNames[code];
|
||||
if (pseudoName) {
|
||||
return pseudoName;
|
||||
}
|
||||
|
||||
try {
|
||||
return new Intl.DisplayNames(language, { type: "region" }).of(code) ?? code;
|
||||
} catch {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
|
||||
export function getCountryOptions(lang: string) {
|
||||
|
||||
Reference in New Issue
Block a user