A simple library for converting country codes between ISO, name and numeric formats.
View Demo
·
Report Bug
·
Request Feature
You can install the package using npm or yarn:
npm install country-codes-converter
yarn add country-codes-converter
country-codes-converter
is fully TypeScript-supported, ensuring type safety and improved development experience. Additionally, it boasts 100% code test coverage, which guarantees the reliability and stability of the library.
import { iso3ToIso2, nameToIso3 } from "country-codes-converter";
// Convert ISO3 to ISO2
iso3ToIso2("USA").then((iso2) => {
console.log(`ISO2 Code: ${iso2}`);
});
// Convert country name to ISO3
nameToIso3("Canada").then((iso3) => {
console.log(`ISO3 Code: ${iso3}`);
});
The library exposes the following functions:
-
iso3ToIso2(iso3: string): Promise<string | null>
- Converts an ISO3 code to an ISO2 code.
-
iso2ToIso3(iso2: string): Promise<string | null>
- Converts an ISO2 code to an ISO3 code.
-
iso3ToNumeric(iso3: string): Promise<string | null>
- Converts an ISO3 code to its numeric representation.
-
numericToIso3(numeric: string): Promise<string | null>
- Converts a numeric code to an ISO3 code.
-
iso3ToName(iso3: string): Promise<string | null>
- Converts an ISO3 code to the country's name.
-
nameToIso3(name: string): Promise<string | null>
- Converts a country name to its ISO3 code.
-
iso2ToNumeric(iso2: string): Promise<string | null>
- Converts an ISO2 code to its numeric representation.
-
numericToIso2(numeric: string): Promise<string | null>
- Converts a numeric code to an ISO2 code.
-
nameToIso2(name: string): Promise<string | null>
- Converts a country name to its ISO2 code.
-
iso2ToName(iso2: string): Promise<string | null>
- Converts an ISO2 code to the country's name.
-
numericToName(numeric: string): Promise<string | null>
- Converts a numeric code to the country's name.
-
nameToNumeric(name: string): Promise<string | null>
- Converts a country name to its numeric representation.
- All functions return a promise that resolves to the corresponding code or name, or
null
if the provided input is invalid or not found in the data. - The data is cached after the first fetch, making subsequent calls faster.
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue. Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch:
git checkout -b feature/AmazingFeature
- Commit your Changes:
git commit -m 'Add some AmazingFeature'
- Push to the Branch:
git push origin feature/AmazingFeature
- Open a Pull Request
Distributed under the MIT License. See LICENSE.txt
for more information.
Marius Adam - @Marius Adam LinkedIn
Project Link: https://github.com/Marius-Adam/country-codes-converter