Skip to content

Commit b2cf897

Browse files
committed
Update formatBirthplaceCountryOrState logic
1 parent d56e138 commit b2cf897

3 files changed

Lines changed: 9 additions & 6 deletions

File tree

web/src/constants/jurisdictions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ export const JURISDICTIONS = {
7979
/** Lowercase state/territory abbreviation, matching content collection jurisdiction IDs. */
8080
export type JurisdictionId = keyof typeof JURISDICTIONS;
8181

82+
export const isJurisdictionId = (id: string): id is JurisdictionId =>
83+
id in JURISDICTIONS;
84+
8285
/** `{ label, value }` options for jurisdiction selects/comboboxes. */
8386
export const JURISDICTION_OPTIONS = Object.entries(JURISDICTIONS).map(
8487
([id, jurisdiction]) => ({

web/src/lib/utils/__tests__/formatBirthplaceCountryOrState.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { formatBirthplaceCountryOrState } from "../formatBirthplaceCountryOrStat
55

66
describe("formatBirthplaceCountryOrState", () => {
77
it("returns state name when valid state is provided", () => {
8-
expect(formatBirthplaceCountryOrState(undefined, "CA")).toBe(
8+
expect(formatBirthplaceCountryOrState(undefined, "ca")).toBe(
99
JURISDICTIONS.ca.name,
1010
);
1111
});
@@ -19,7 +19,7 @@ describe("formatBirthplaceCountryOrState", () => {
1919
});
2020

2121
it("returns state name when both country and state are provided and country is the US", () => {
22-
expect(formatBirthplaceCountryOrState("US", "NY")).toBe(
22+
expect(formatBirthplaceCountryOrState("US", "ny")).toBe(
2323
JURISDICTIONS.ny.name,
2424
);
2525
});
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { COUNTRIES } from "#constants/countries";
2-
import { JURISDICTIONS, type JurisdictionId } from "#constants/jurisdictions";
2+
import { isJurisdictionId, JURISDICTIONS } from "#constants/jurisdictions";
33

44
/**
55
* Given a birthplace country and/or state, return the country name
@@ -8,7 +8,7 @@ import { JURISDICTIONS, type JurisdictionId } from "#constants/jurisdictions";
88
* @example
99
* formatBirthplaceCountryOrState("MX")
1010
* // "Mexico"
11-
* formatBirthplaceCountryOrState("US", "MA")
11+
* formatBirthplaceCountryOrState("US", "ma")
1212
* // "Massachusetts"
1313
*/
1414
export const formatBirthplaceCountryOrState = (
@@ -18,8 +18,8 @@ export const formatBirthplaceCountryOrState = (
1818
if (birthplaceCountry && birthplaceCountry !== "US") {
1919
return COUNTRIES[birthplaceCountry];
2020
}
21-
if (birthplaceState) {
22-
return JURISDICTIONS[birthplaceState.toLowerCase() as JurisdictionId]?.name;
21+
if (birthplaceState && isJurisdictionId(birthplaceState)) {
22+
return JURISDICTIONS[birthplaceState].name;
2323
}
2424
return "";
2525
};

0 commit comments

Comments
 (0)