CountryData.js
收藏资源简介:
CountryData.js是一个全面的Node.js包,旨在提供对详细国家信息的轻松访问。它支持JavaScript和TypeScript,适用于各种项目需求。无论您是构建Web应用程序、API还是任何其他需要国家数据的项目,此包都提供了一个简单高效的解决方案。
CountryData.js is a comprehensive Node.js package designed to provide easy access to detailed country information. It supports both JavaScript and TypeScript, catering to various project requirements. Whether you are building web applications, APIs, or any other project that requires country data, this package offers a simple and efficient solution.
CountryData.js
CountryData.js 是一个全面的 Node.js 包,旨在提供对详细国家信息的轻松访问。它支持 JavaScript 和 TypeScript,适用于各种项目需求。
特性
- 全面的国家数据:访问包括
国家名称、短代码、电话代码、地区和国旗在内的详细信息。 - JavaScript 和 TypeScript 支持:在 JavaScript 和 TypeScript 项目中均可使用。
- 易于集成:使用 ES 模块无缝集成到任何 Node.js 项目中。
- 异步操作:所有方法返回 promises 以实现高效的数据处理。
- 轻量级:最小化依赖以保持项目精简。
安装
使用 npm 安装 CountryData.js:
sh npm install countrydata.js
使用
JavaScript 版本
javascript import { CountryHelper } from "countrydata.js";
const countryHelper = new CountryHelper();
// 获取所有国家 (async () => { const allCountries = await countryHelper.getCountries(); console.log(JSON.stringify(allCountries, null, 2)); })();
// 通过短代码获取国家 (async () => { const country = await countryHelper.getCountryByShortCode(US); console.log(country); })();
TypeScript 版本
typescript import { CountryHelper } from countrydata.js;
(async () => { const countryHelper = new CountryHelper(); const allCountries = await countryHelper.getCountries(); console.log(JSON.stringify(allCountries, null, 2)); })();
// 通过短代码获取国家 (async () => { const countryHelper = new CountryHelper(); const countryData = await countryHelper.getCountryByShortCode(US); console.log(countryData); })();
API
CountryHelper
提供访问国家数据的主要类,在 JavaScript 和 TypeScript 中使用方式相同。
方法
| 方法 | 描述 |
|---|---|
getCountries(): Promise<Country[]> |
获取并返回所有国家的数组。 |
| `getCountryByShortCode(shortCode: string): Promise<Country | null>` |
getRegionsByCountryShortCode(shortCode: string): Promise<Region[]> |
根据提供的国家短代码获取该国家的地区数组。 |
| `getCountryByPhoneCode(phoneCode: string): Promise<Country | null>` |
| `getCountryPhoneCodeByShortCode(shortCode: string): Promise<string | null>` |




