npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@workmate/country-and-currency

v1.2.2

Published

A lightweight library for countries' flags (country flag, unicode, location), capitals, their dail codes, currencies as well as location related calculations

Downloads

673

Readme

Country flags, capitals and currency library

NPM npm bundle size npm Libraries.io dependency status for latest release, scoped npm package GitHub last commit

Installation

CDN

Add the js script to the head of the html file

<script src="https://cdn.jsdelivr.net/npm/@workmate/country-and-currency/dist/bundle.js" defer></script>

Then you can access the country and currency instance using

<script defer>
  var CountryAndCurrency = CountryAndCurrencyLib.setup()
</script>

Package Manager

# using npm
npm install --save @workmate/country-and-currency

# using yarn
yarn add @workmate/country-and-currency

Then you can access the country and currency instance using

import CountryAndCurrency from "@workmate/country-and-currency";

Useful types

interface Currency {
  name: string;
  code: string;
  symbol: string;
}

interface Country {
  name: string;
  capital: string;
  continent: string;
  flag: string;
  iso2: string;
  iso3: string;
  dail_code: string;
  latitude: number;
  longitude: number;
  currency: {
    unicode: string;
    code: string;
    name: string;
    symbol: string;
  };
}

interface CountryWithDistance extends Country {
  distance: number;
}

interface LocationInterface {
  long: number;
  lat: number;
}

Methods

  • getCurrencies(): Array<Currency>
  • getCurrencyBy(field: "name" | "code" | "symbol",value: string): Currency | undefined
  • getCountries(): Array<Country>
  • getCountriesBy(field: "name" | "capital" | "continent" | "flag" | "iso2" | "iso3" | "dail_code", value: string): Array<Country>
  • distanceBetweenLocations(firstLocation: LocationInterface, secondLocation: LocationInterface): number (result in kilometers)
  • countriesWithinRadius(country: Country, radius: number): Array<CountryWithDistance> (radius in kilometers)
  • topXClosestCountries(country: Country, x: number): Array<CountryWithDistance>
  • topXFarthestCountries(country: Country, x: number): Array<CountryWithDistance>
  • distanceOfCountryToOtherCountries(country: Country, order: "asc" | "desc" = "asc"): Array<CountryWithDistance>

Usage

const currencies = CountryAndCurrency.getCurrencies()
console.log(currencies)
/*
Expected result
[
  {
    name: "Afghan Afghani",
    code: "AFA",
    symbol: "؋",
  },
  ...
]
*/

const currency = CountryAndCurrency.getCurrencyBy("code", "AFA")
console.log(currency)
/*
Expected result
{
  name: "Afghan Afghani",
  code: "AFA",
  symbol: "؋",
},
*/

const countries = CountryAndCurrency.getCountries()
console.log(countries)
/*
Expected result
[
  {
    name: "Mexico",
    capital: "Mexico City",
    continent: "NA",
    flag: "https://upload.wikimedia.org/wikipedia/commons/f/fc/Flag_of_Mexico.svg",
    iso2: "MX",
    iso3: "MEX",
    dail_code: "+52",
    latitude: 23,
    longitude: -102,
    currency: {
      unicode: "🇲🇽",
      code: "MXN",
      name: "Mexican Peso",
      symbol: "$",
    },
  },
  ...
]
*/

const countriesBy = CountryAndCurrency.getCountriesBy("iso3", "MEX")
// if the field selected (in this case iso3) is unique, the result would be an array of length 0 or 1
console.log(countriesBy)

/*
[
  {
    name: "Mexico",
    capital: "Mexico City",
    continent: "NA",
    flag: "https://upload.wikimedia.org/wikipedia/commons/f/fc/Flag_of_Mexico.svg",
    iso2: "MX",
    iso3: "MEX",
    dail_code: "+52",
    latitude: 23,
    longitude: -102,
    currency: {
      unicode: "🇲🇽",
      code: "MXN",
      name: "Mexican Peso",
      symbol: "$",
    },
  }
]
*/

const firstLocation = {
  lat: 10,
  long: 15,
};

const secondLocation = {
  lat: 20,
  long: -17,
};

const distanceBetweenLocations = CountryAndCurrency.distanceBetweenLocations(firstLocation, secondLocation)
console.log(distanceBetweenLocations);
/*
Expected result
3604.3678036992624 // in kilometers
*/

const mexico = CountryAndCurrency.getCountriesBy("iso3", "MEX")[0];
const countriesWithinRadius = CountryAndCurrency.countriesWithinRadius(
  mexico,
  2000
);
console.log(countriesWithinRadius);
/*
Expected result
[
  {
    name: 'Guatemala',
    capital: 'Guatemala City',
    continent: 'NA',
    flag: 'https://upload.wikimedia.org/wikipedia/commons/e/ec/Flag_of_Guatemala.svg',
    iso2: 'GT',
    iso3: 'GTM',
    dail_code: '+502',
    latitude: 15.5,
    longitude: -90.25,
    currency: {
      unicode: '🇬🇹',
      code: 'GTQ',
      name: 'Guatemalan Quetzal',
      symbol: 'Q'
    },
    distance: 1487.7626308065874
  },
  ...
]
*/


const mexico = CountryAndCurrency.getCountriesBy("iso3", "MEX")[0];
const closestCountries = CountryAndCurrency.topXClosestCountries(mexico, 2);
console.log(closestCountries);
/*
Expected result
[
  {
    name: 'Guatemala',
    capital: 'Guatemala City',
    continent: 'NA',
    flag: 'https://upload.wikimedia.org/wikipedia/commons/e/ec/Flag_of_Guatemala.svg',
    iso2: 'GT',
    iso3: 'GTM',
    dail_code: '+502',
    latitude: 15.5,
    longitude: -90.25,
    currency: {
      unicode: '🇬🇹',
      code: 'GTQ',
      name: 'Guatemalan Quetzal',
      symbol: 'Q'
    },
    distance: 1487.7626308065874
  },
  ...
]
*/


const mexico = CountryAndCurrency.getCountriesBy("iso3", "MEX")[0];
const farthestCountries = CountryAndCurrency.topXFarthestCountries(mexico, 2);
console.log(farthestCountries);

/*
Expected result
[
  {
    name: 'British Indian Ocean Territory',
    capital: 'Diego Garcia',
    continent: 'AS',
    flag: 'https://upload.wikimedia.org/wikipedia/commons/6/6e/Flag_of_the_British_Indian_Ocean_Territory.svg',   
    iso2: 'IO',
    iso3: 'IOT',
    dail_code: '+246',
    latitude: -6,
    longitude: 71.5,
    currency: { unicode: '🇮🇴', code: 'USD', name: 'US Dollar', symbol: '$' },
    distance: 18000.497060769732
  },
  ...
]
*/

const mexico = CountryAndCurrency.getCountriesBy("iso3", "MEX")[0];
const distanceFromOthers = CountryAndCurrency.distanceOfCountryToOtherCountries(
  mexico,
  "asc" // from closest to farthest
);
console.log(distanceFromOthers);
/*
Expected result
[
  {
    name: 'British Indian Ocean Territory',
    capital: 'Diego Garcia',
    continent: 'AS',
    flag: 'https://upload.wikimedia.org/wikipedia/commons/6/6e/Flag_of_the_British_Indian_Ocean_Territory.svg',   
    iso2: 'IO',
    iso3: 'IOT',
    dail_code: '+246',
    latitude: -6,
    longitude: 71.5,
    currency: { unicode: '🇮🇴', code: 'USD', name: 'US Dollar', symbol: '$' },
    distance: 18000.497060769732
  },
  ...
]
*/