Skip to content

Commit 932f0aa

Browse files
committed
feat: Add Express.js and TypeScript samples for CountryData.js
1 parent bddd190 commit 932f0aa

16 files changed

+1272
-1437
lines changed

README.md

Lines changed: 42 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,93 @@
1-
# CountryData.js ![NPM Downloads](https://img.shields.io/npm/d18m/countrydata.js)
2-
3-
4-
| CI Status | Code Coverage | Quality Gate Status |
5-
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
6-
| [![CI](https://github.com/Clifftech123/CountryData.js/actions/workflows/main.yml/badge.svg)](https://github.com/Clifftech123/CountryData.js/actions/workflows/main.yml) | [![codecov](https://codecov.io/github/Clifftech123/CountryData.js/graph/badge.svg?token=42Y3GT9MKN)](https://codecov.io/github/Clifftech123/CountryData.js) | [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=Clifftech123_CountryData.js&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=Clifftech123_CountryData.js) |
7-
8-
1+
# CountryData.js
92

3+
## Overview
104

5+
`CountryData.js` is a comprehensive Node.js package designed to provide easy access to detailed country information. It supports both JavaScript and TypeScript, making it versatile for various project needs. Whether you're building a web application, API, or any other project that requires country data, this package offers a simple and efficient solution.
116

7+
## Features
128

13-
CountryData.js is a comprehensive Node.js package designed to provide easy access to detailed country information. It supports both JavaScript and TypeScript, making it versatile for various project needs. Whether you're building a web application, API, or any other project that requires country data, this package offers a simple and efficient solution.
14-
15-
Make sure to compile TypeScript files to JavaScript before running the code.
9+
- 🌍 **Comprehensive Country Data**: Access detailed information, including `country names`, `short codes`, `phone codes`, `regions`, and `flags`.
10+
-**JavaScript and TypeScript Support**: Use the package seamlessly in both JavaScript and TypeScript projects.
11+
- 🔄 **Asynchronous Operations**: All methods return promises for efficient data handling.
12+
- 📦 **Lightweight & Efficient**: Minimal dependencies to keep your project lean.
13+
- 🏗️ **Easy Integration**: Seamlessly integrates with any Node.js project using ES modules and commonJS
1614

15+
## Status & Quality
1716

18-
## Features
17+
| 🔄 CI Status | 📊 Code Coverage | 🏆 Quality Gate Status |
18+
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
19+
| [![CI](https://github.com/Clifftech123/CountryData.js/actions/workflows/main.yml/badge.svg)](https://github.com/Clifftech123/CountryData.js/actions/workflows/main.yml) | [![codecov](https://codecov.io/github/Clifftech123/CountryData.js/graph/badge.svg?token=42Y3GT9MKN)](https://codecov.io/github/Clifftech123/CountryData.js) | [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=Clifftech123_CountryData.js&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=Clifftech123_CountryData.js) |
1920

20-
- **Comprehensive Country Data**: Access detailed information including `country names`, `short codes`, `phone codes`, `regions`, and `flags`.
21-
- **JavaScript and TypeScript Support**: Use the package in both JavaScript and TypeScript projects.
22-
- **Easy Integration**: Seamlessly integrate with any Node.js project using ES modules.
23-
- **Asynchronous Operations**: All methods return promises for efficient data handling.
24-
- **Lightweight**: Minimal dependencies to keep your project lean.
21+
![NPM Downloads](https://img.shields.io/npm/d18m/countrydata.js)
2522

2623
## Installation
2724

28-
Install CountryData.js using npm:
29-
3025
```sh
3126
npm install countrydata.js
3227
```
3328

3429
## Usage
3530

36-
### JavaScript Version
31+
### JavaScript Example
3732

3833
```javascript
39-
import { CountryHelper } from "countrydata.js";
34+
import { CountryHelper } from 'countrydata.js';
4035

4136
const countryHelper = new CountryHelper();
4237

4338
// Get all countries
44-
(async () => {
45-
const allCountries = await countryHelper.getCountries();
46-
console.log(JSON.stringify(allCountries, null, 2));
47-
})();
39+
countryHelper.getCountries().then(console.log);
4840

4941
// Get country by short code
50-
(async () => {
51-
const country = await countryHelper.getCountryByShortCode('US');
52-
console.log(country);
53-
})();
54-
55-
42+
countryHelper.getCountryByShortCode('US').then(console.log);
5643
```
5744

58-
### TypeScript Version
45+
### TypeScript Example
5946

6047
```typescript
61-
6248
import { CountryHelper } from 'countrydata.js';
6349

64-
(async () => {
65-
const countryHelper = new CountryHelper();
66-
const allCountries = await countryHelper.getCountries();
67-
console.log(JSON.stringify(allCountries, null, 2));
68-
})();
50+
const countryHelper = new CountryHelper();
6951

70-
// Get country by short code
71-
(async () => {
72-
const countryHelper = new CountryHelper();
73-
const countryData = await countryHelper.getCountryByShortCode('US');
74-
console.log(countryData);
75-
})();
52+
// Get all countries
53+
const allCountries = await countryHelper.getCountries();
54+
console.log(allCountries);
7655

56+
// Get country by short code
57+
const countryByCode = await countryHelper.getCountryByShortCode('US');
58+
console.log(countryByCode);
7759
```
7860

79-
You can check the sample code in the [example folder](https://github.com/Clifftech123/CountryData.js/tree/main/Sample).
80-
With this sample code, you can see how to use the package in your project both in JavaScript and TypeScript.
61+
## Sample Implementation
8162

82-
Make sure to check how the Sample has been implemented .
63+
If you want to see a sample implementation of this package further in both javascript and typescript , you can check out the [Sample](https://github.com/Clifftech123/CountryData.js/tree/main/Sample) folder.
8364

84-
## API
65+
### Usage with Express.js
8566

86-
### `CountryHelper`
67+
This package can be used in an Express.js application to create a REST API for country data. Check the [example folder](https://github.com/Clifftech123/CountryData.js/tree/main/Sample) for sample implementations in both JavaScript and TypeScript.
8768

88-
The main class that provides access to country data. It works the same way in both JavaScript and TypeScript.
69+
## API Reference
8970

90-
#### Methods
71+
### `CountryHelper` Methods
9172

92-
93-
| Method | Description |
94-
|--------|-------------|
95-
| `getCountries(): Promise<Country[]>` | Fetches and returns a promise that resolves to an array of all countries. |
96-
| `getCountryByShortCode(shortCode: string): Promise<Country` | null>` | Fetches and returns a promise that resolves to a country object based on the provided country short code (e.g., "US"). Returns `null` if the country is not found. |
97-
| `getRegionsByCountryShortCode(shortCode: string): Promise<Region[]>` | Fetches and returns a promise that resolves to an array of regions for the specified country short code (e.g., "US"). |
98-
| `getCountryByPhoneCode(phoneCode: string): Promise<Country` | null> | Fetches and returns a promise that resolves to a country object based on the provided phone code (e.g., "1" for the US). Returns `null` if the country is not found. |
99-
| `getCountryPhoneCodeByShortCode(shortCode: string): Promise<string` | null>` | Fetches and returns a promise that resolves to the phone code of a country based on the provided country short code (e.g., "US"). Returns `null` if the country is not found. |
73+
| Method | Description |
74+
| -------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------- |
75+
| **`getCountries(): Promise<Country[]>`** | Returns an array of all countries. |
76+
| **`getCountryByShortCode(shortCode: string): Promise<Country \| null>`** | Returns a country object based on the short code (e.g., "US"), or `null` if not found. |
77+
| **`getRegionsByCountryShortCode(shortCode: string): Promise<Region[]>`** | Returns an array of regions for the specified country short code (e.g., "US"). |
78+
| **`getCountryByPhoneCode(phoneCode: string): Promise<Country \| null>`** | Returns a country object based on the phone code (e.g., "1" for the US), or `null` if not found. |
79+
| **`getCountryPhoneCodeByShortCode(shortCode: string): Promise<string \| null>`** | Returns the phone code of a country based on the short code (e.g., "US"), or `null` if not found. |
10080

10181
## Contributing
10282

103-
We welcome contributions to CountryData.js! Please read our [contributing guidelines](CONTRIBUTING.md) for details on how to submit pull requests, report issues, or request features.
83+
We welcome contributions! Please read our [contributing guidelines](CONTRIBUTING.md) for details on how to submit pull requests, report issues, or request features.
10484

10585
## License
10686

10787
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
10888

10989
## Support
11090

111-
If you encounter any issues or have questions about using CountryData.js, please open an issue on our GitHub repository or contact us directly through our support channels.
112-
91+
If you encounter any issues or have questions, please open an issue on our GitHub repository or contact us directly through our support channels.
11392

93+
🚀 Get started today with `CountryData.js` and simplify your country data management!

Sample/TypeScript/countrydata.js.d.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// types/countrydata.js.d.ts
22
declare module 'countrydata.js' {
3-
export class CountryHelper {
4-
getCountries(): Promise<any>;
5-
getCountryByShortCode(shortCode: string): Promise<any>;
6-
getRegionsByCountryShortCode(shortCode: string): Promise<any>;
7-
getCountryByPhoneCode(phoneCode: string): Promise<any>;
8-
getCountryPhoneCodeByShortCode(shortCode: string): Promise<any>;
9-
}
10-
}
3+
export class CountryHelper {
4+
getCountries(): Promise<any>;
5+
getCountryByShortCode(shortCode: string): Promise<any>;
6+
getRegionsByCountryShortCode(shortCode: string): Promise<any>;
7+
getCountryByPhoneCode(phoneCode: string): Promise<any>;
8+
getCountryPhoneCodeByShortCode(shortCode: string): Promise<any>;
9+
}
10+
}

Sample/TypeScript/index.js

Lines changed: 0 additions & 47 deletions
This file was deleted.

Sample/TypeScript/index.ts

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,35 @@
1-
import { CountryHelper } from 'countrydata.js';
1+
// Import from local source instead of npm package
2+
import { CountryHelper } from '../.././src/index.js';
23

3-
(async () => {
4-
const countryHelper = new CountryHelper();
5-
const allCountries = await countryHelper.getCountries();
6-
console.log(JSON.stringify(allCountries, null, 2));
7-
})();
4+
// Create a single instance to use for all operations
5+
const countryHelper = new CountryHelper();
6+
7+
// Log all countries
8+
console.log('All Countries:');
9+
const allCountries = countryHelper.getCountries();
10+
console.log(JSON.stringify(allCountries.slice(0, 2), null, 2));
811

912
// Get country by short code
10-
(async () => {
11-
const countryHelper = new CountryHelper();
12-
const countryData = await countryHelper.getCountryByShortCode('US');
13-
console.log(countryData);
14-
})();
13+
console.log('\nCountry by Short Code (US):');
14+
const countryByCode = countryHelper.getCountryByShortCode('US');
15+
console.log(countryByCode);
1516

1617
// Get regions in a particular country
17-
(async () => {
18-
const countryHelper = new CountryHelper();
19-
const regionsData = await countryHelper.getRegionsByCountryShortCode('GH');
20-
console.log(regionsData);
21-
})();
18+
console.log('\nRegions in Ghana (GH):');
19+
const regionsData = countryHelper.getRegionsByCountryShortCode('GH');
20+
console.log(regionsData);
2221

23-
// Get country Country Flag
24-
(async () => {
25-
const countryHelper = new CountryHelper();
26-
const countryData = await countryHelper.getCountryByShortCode('US');
27-
console.log(countryData?.countryFlag);
28-
})();
22+
// Get country flag
23+
console.log('\nUS Country Flag:');
24+
const countryWithFlag = countryHelper.getCountryByShortCode('US');
25+
console.log(countryWithFlag?.countryFlag);
2926

3027
// Get country by phone code
31-
(async () => {
32-
const countryHelper = new CountryHelper();
33-
const countryData = await countryHelper.getCountryByPhoneCode('+233');
34-
console.log(countryData);
35-
})();
28+
console.log('\nCountry by Phone Code (+233):');
29+
const countryByPhone = countryHelper.getCountryByPhoneCode('+233');
30+
console.log(countryByPhone);
3631

3732
// Get country phone code by short code
38-
(async () => {
39-
const countryHelper = new CountryHelper();
40-
const phoneCode = await countryHelper.getCountryPhoneCodeByShortCode('US');
41-
console.log(phoneCode);
42-
})();
33+
console.log('\nPhone Code for US:');
34+
const phoneCode = countryHelper.getCountryPhoneCodeByShortCode('US');
35+
console.log(phoneCode);

0 commit comments

Comments
 (0)