Skip to content

Commit

Permalink
tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
emon5122 committed Oct 16, 2023
1 parent f3da825 commit 452c3ea
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 11 deletions.
112 changes: 105 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
[![Commit](https://img.shields.io/github/last-commit/nexisltd/country.svg)](https://github.com/nexisltd/country/commits/main)
[![jsDeliver](https://data.jsdelivr.com/v1/package/npm/@nexisltd/country/badge)](https://www.jsdelivr.com/package/npm/@nexisltd/country)

Coming Soon
A versatile npm package for accessing country-related data, including country details, currencies, flags, and more.

## Installation

To install the `country` library, you can use npm or yarn or pnpm:
To install the `country` library, you can use npm, yarn, or pnpm:

```bash
npm install @nexisltd/country
Expand All @@ -25,22 +25,120 @@ pnpm install @nexisltd/country

## Usage

Here's how you can use the library in your JavaScript or TypeScript code:
### Country Details

You can retrieve details about a specific country using the `country` function. This function takes an identifier, which can be either the country name or country code.

```javascript
import { country } from '@nexisltd/country';

const countryDetails = country("Bangladesh");

console.log(countryDetails);
```

### List of Countries

To get a list of all countries with essential information, you can use the `countries` function.

```javascript
import { countries } from '@nexisltd/country';

const allCountries = countries();

console.log(allCountries);
```

### Currencies

To access information about a specific country's currency, use the `currency` function with the country name or code.

```javascript
import { currency } from '@nexisltd/country';

const currencyDetails = currency("Bangladesh");

console.log(currencyDetails);
```

### List of Currencies

To retrieve a list of all available currencies, you can use the `currencies` function.

```javascript
import { currencies } from '@nexisltd/country';

const allCurrencies = currencies();

console.log(allCurrencies);
```

### Flags

You can obtain the flag of a specific country using the `flag` function with the country name or code.

```javascript
import {findNationality} from '@nexisltd/country';
import { flag } from '@nexisltd/country';

const nationality = findNationality("United States");
const countryFlag = flag("Bangladesh");

console.log(countryFlag);
```

### List of Flags

To get a list of flags for all countries, use the `flags` function.

```javascript
import { flags } from '@nexisltd/country';

const allFlags = flags();

console.log(allFlags);
```

### Nationality

Find the nationality associated with a specific country using the `findNationality` function.

```javascript
import { findNationality } from '@nexisltd/country';

const nationality = findNationality("Bangladesh");

console.log(nationality);
```

### Timezones

Retrieve information about the time zones associated with a specific country using the `timezone` function with the country name or code.

```javascript
import { timezone } from '@nexisltd/country';

const countryTimezone = timezone("Bangladesh");

console.log(countryTimezone);
```

### List of Timezones

To get a list of time zones for all countries, use the `timezones` function.

```javascript
import { timezones } from '@nexisltd/country';

const allTimezones = timezones();

console.log(allTimezones);
```

## License

This library is open-source and available under the [MIT License](LICENSE).

---

Feel free to use this library to enhance the date formatting in your projects, and if you have any questions or suggestions, don't hesitate to [open an issue](https://github.com/nexisltd/country/issues) or [contribute](https://github.com/nexisltd/country/pulls) to its development.
Feel free to use this library to enhance the data related to countries in your projects. If you have any questions, suggestions, or issues, please don't hesitate to [open an issue](https://github.com/nexisltd/country/issues) or [contribute](https://github.com/nexisltd/country/pulls) to its development.

Happy coding! 📅🚀
Happy coding! 🌎🚀
135 changes: 131 additions & 4 deletions index.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,135 @@
import { describe, expect, it } from "vitest";
import { findNationality } from "./src";
import { country, currency, findNationality, flag, timezone } from "./src";

describe("Nationality", () => {
it("Checks with an example country and matches if it's right.", () => {
expect(findNationality("United States")).toBe("American");
describe("Country Package", () => {
it("should retrieve details for a country by name", () => {
const countryDetails = country("Afghanistan");
expect(countryDetails).toEqual({
name: "Afghanistan",
code: "AF",
flag: "https://flagcdn.com/af.svg",
nationality: "Afghan",
currency: "AFN",
tz: ["Asia/Kabul"],
});
});

it("should retrieve details for a country by code", () => {
const countryDetails = country("US");
expect(countryDetails).toEqual({
name: "United States",
code: "US",
flag: "https://flagcdn.com/us.svg",
nationality: "American",
currency: "USD",
tz: [
"America/Adak",
"America/Anchorage",
"America/Boise",
"America/Chicago",
"America/Denver",
"America/Detroit",
"America/Indiana/Indianapolis",
"America/Indiana/Knox",
"America/Indiana/Marengo",
"America/Indiana/Petersburg",
"America/Indiana/Tell_City",
"America/Indiana/Vevay",
"America/Indiana/Vincennes",
"America/Indiana/Winamac",
"America/Juneau",
"America/Kentucky/Louisville",
"America/Kentucky/Monticello",
"America/Los_Angeles",
"America/Menominee",
"America/Metlakatla",
"America/New_York",
"America/Nome",
"America/North_Dakota/Beulah",
"America/North_Dakota/Center",
"America/North_Dakota/New_Salem",
"America/Phoenix",
"America/Sitka",
"America/Yakutat",
"Pacific/Honolulu",
],
});
});

it("should return undefined when a country is not found", () => {
const countryDetails = country("Nonexistent Country");
expect(countryDetails).toBeUndefined();
});

it("should retrieve currency details by name", () => {
const currencyDetails = currency("United States");
expect(currencyDetails).toEqual({
name: "United States",
currency: "USD",
});
});

it("should retrieve currency details by code", () => {
const currencyDetails = currency("US");
expect(currencyDetails).toEqual({
name: "United States",
currency: "USD",
});
});

it("should return undefined when a currency is not found", () => {
const currencyDetails = currency("Nonexistent Currency");
expect(currencyDetails).toBeNull();
});
it("should retrieve a flag by name", () => {
const countryFlag = flag("United States");
expect(countryFlag).toEqual({
name: "United States",
flag: "https://flagcdn.com/us.svg",
});
});

it("should retrieve a flag by code", () => {
const countryFlag = flag("US");
expect(countryFlag).toEqual({
name: "United States",
flag: "https://flagcdn.com/us.svg",
});
});

it("should return null when a flag is not found", () => {
const countryFlag = flag("Nonexistent Country");
expect(countryFlag).toBeNull();
});

it("should find the nationality of a country", () => {
const nationality = findNationality("United States");
expect(nationality).toBe("American");
});

it("should return null when nationality is not found", () => {
const nationality = findNationality("Nonexistent Country");
expect(nationality).toBeNull();
});

it("should retrieve timezones by name", () => {
const countryTimezone = timezone("Bangladesh");
expect(countryTimezone).toEqual({
name: "Bangladesh",
tz: ["Asia/Dhaka"],
});
});

it("should retrieve timezones by code", () => {
const countryTimezone = timezone("BD");
expect(countryTimezone).toEqual({
name: "Bangladesh",
tz: ["Asia/Dhaka"],
});
});

it("should return undefined when timezones are not found", () => {
const countryTimezone = timezone("Nonexistent Country");
expect(countryTimezone).toBeNull();
});
});
1 change: 1 addition & 0 deletions src/currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ export const currency = (identifier: string) => {
const country = countryArray.find(
(country) => country.code === identifier || country.name === identifier
);
if (!country) return null;
return { name: country?.name, currency: country?.currency };
};
3 changes: 3 additions & 0 deletions src/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ export const flag = (identifier: string) => {
const country = countryArray.find(
(country) => country.code === identifier || country.name === identifier
);
if (!country) {
return null;
}
return { name: country?.name, flag: country?.flag };
};
3 changes: 3 additions & 0 deletions src/timezone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ export const timezone = (identifier: string) => {
const country = countryArray.find(
(country) => country.code === identifier || country.name === identifier
);
if (!country) {
return null;
}
return { name: country?.name, tz: country?.tz };
};

0 comments on commit 452c3ea

Please sign in to comment.