ChemPal Documentation - v1.18.0
    Preparing search index...

    Function findCountryByName

    • Resolves a country's ISO 3166-1 alpha-2 code from its full English name. The input is title-cased first, and the match is exact against the table's names, so short aliases like "USA" are unknown (callers handle those separately).

      Parameters

      • name: string

        A country name (any casing), e.g. "germany", "United States"

      Returns any

      The matching ISO alpha-2 code, or undefined if the name is unknown

      findCountryByName("germany") // "DE"
      findCountryByName("United States") // "US"
      findCountryByName("Narnia") // undefined
      export function findCountryByName(name: string): CountryCode | undefined {
      const titleCased = name
      .trim()
      .toLowerCase()
      .replace(/\b[a-z]/g, (c) => c.toUpperCase());
      isoByName ??= new Map(
      Object.entries(COUNTRY_DATA).map(([iso2, [countryName]]) => [countryName, iso2]),
      );
      const iso2 = isoByName.get(titleCased);
      return iso2 !== undefined && isKnownCountryCode(iso2) ? iso2 : undefined;
      }