Skip to content

Instantly share code, notes, and snippets.

@deadcoder0904
Last active October 27, 2017 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deadcoder0904/03d9010a6eb03e7ebb9e2c37d7f9db76 to your computer and use it in GitHub Desktop.
Save deadcoder0904/03d9010a6eb03e7ebb9e2c37d7f9db76 to your computer and use it in GitHub Desktop.
Get Country Codes & Flags of Countires w.r.t Country Codes
// Get country codes & flag of countries with respect to country codes
const fetch = require("node-fetch");
const NEW_LINES = `\n\n`;
const EMOJIS = `${NEW_LINES}😀 😃 😄 😁 😆 😅 😂 🤣 ☺️ 😊 😇 🙂 🙃 😉 😌 😍 😘 😗 😙 😚 😋 😜 😝 😛 🤑 🤗 🤓 😎 🤡 🤠 😏 😒 😞 😔 😟 😕 🙁 ☹️ 😣 😖 😫 😩 😤 😠 😡 😶 😐 😑 😯 😦 😧 😮 😲 😵 😳 😱 😨 😰 😢 😥 🤤 😭 😓 😪 😴 🙄 🤔 🤥 😬 🤐 🤢 🤧 😷 🤒 🤕 😈 👿 👹 👺 💩 👻 💀 ☠️ 👽 👾 🤖 🎃 😺 😸 😹 😻 😼${NEW_LINES}`;
const FIXER_URI = "https://api.fixer.io/latest";
const REST_COUNTRIES_URI = "https://restcountries.eu/rest/v2/alpha/";
const COUNTRY_URI = "http://country.io/currency.json";
let currencyCodes = [];
let currencyOfCountry = [];
const flagsWithCurrencies = {};
const LOG = text => {
console.log(EMOJIS);
console.log(text);
console.log(EMOJIS);
};
// ["INR"];
const getCurrencyCodes = async () => {
const res = await fetch(FIXER_URI);
const json = await res.json();
currencyCodes = Object.keys(json.rates);
};
// ["IN"];
const getCountryFromCurrency = async () => {
const res = await fetch(COUNTRY_URI);
const json = await res.json();
const result = Object.keys(json)
.map(countryCode => {
const currencyCode = json[countryCode];
const index = currencyCodes.indexOf(currencyCode);
return {
countryCode,
currencyCode: currencyCodes[index]
};
})
.filter(item => item.currencyCode !== undefined);
currencyOfCountry = result;
};
// {"INR": "https://india_flag.png"}
const getFlagsWithCurrencies = async () => {
for (let i = 0; i < currencyOfCountry.length; i += 1) {
const { countryCode, currencyCode } = currencyOfCountry[i];
try {
const res = await fetch(REST_COUNTRIES_URI + countryCode);
const json = await res.json();
flagsWithCurrencies[currencyCode] = json.flag;
} catch (e) {}
}
console.log(flagsWithCurrencies);
};
const callAll = async () => {
LOG("FETCHING CURRENCY CODES");
await getCurrencyCodes();
console.log(JSON.stringify(currencyCodes, null, 2));
LOG("FETCHING COUNTRY CODES FROM CURRENCY CODES");
await getCountryFromCurrency();
console.log(currencyOfCountry);
LOG("FETCHING COUNTRY CODES FROM CURRENCY CODES");
await getFlagsWithCurrencies();
console.log(flagsWithCurrencies);
};
callAll();
[
'AUD',
'BGN',
'BRL',
'CAD',
'CHF',
'CNY',
'CZK',
'DKK',
'GBP',
'HKD',
'HRK',
'HUF',
'IDR',
'ILS',
'INR',
'JPY',
'KRW',
'MXN',
'MYR',
'NOK',
'NZD',
'PHP',
'PLN',
'RON',
'RUB',
'SEK',
'SGD',
'THB',
'TRY',
'ZAR',
'EUR',
]
[
{ countryCode: "BG", currencyCode: "BGN" },
{ countryCode: "BV", currencyCode: "NOK" },
{ countryCode: "BQ", currencyCode: "USD" },
{ countryCode: "BR", currencyCode: "BRL" },
{ countryCode: "JE", currencyCode: "GBP" },
{ countryCode: "RU", currencyCode: "RUB" },
{ countryCode: "TL", currencyCode: "USD" },
{ countryCode: "RO", currencyCode: "RON" },
{ countryCode: "TK", currencyCode: "NZD" },
{ countryCode: "GU", currencyCode: "USD" },
{ countryCode: "GS", currencyCode: "GBP" },
{ countryCode: "JP", currencyCode: "JPY" },
{ countryCode: "GG", currencyCode: "GBP" },
{ countryCode: "GB", currencyCode: "GBP" },
{ countryCode: "SV", currencyCode: "USD" },
{ countryCode: "GL", currencyCode: "DKK" },
{ countryCode: "HR", currencyCode: "HRK" },
{ countryCode: "HU", currencyCode: "HUF" },
{ countryCode: "HK", currencyCode: "HKD" },
{ countryCode: "HM", currencyCode: "AUD" },
{ countryCode: "PR", currencyCode: "USD" },
{ countryCode: "PS", currencyCode: "ILS" },
{ countryCode: "PW", currencyCode: "USD" },
{ countryCode: "SJ", currencyCode: "NOK" },
{ countryCode: "PH", currencyCode: "PHP" },
{ countryCode: "PN", currencyCode: "NZD" },
{ countryCode: "PL", currencyCode: "PLN" },
{ countryCode: "ZA", currencyCode: "ZAR" },
{ countryCode: "EC", currencyCode: "USD" },
{ countryCode: "MH", currencyCode: "USD" },
{ countryCode: "MP", currencyCode: "USD" },
{ countryCode: "IM", currencyCode: "GBP" },
{ countryCode: "MY", currencyCode: "MYR" },
{ countryCode: "MX", currencyCode: "MXN" },
{ countryCode: "IL", currencyCode: "ILS" },
{ countryCode: "IO", currencyCode: "USD" },
{ countryCode: "FM", currencyCode: "USD" },
{ countryCode: "FO", currencyCode: "DKK" },
{ countryCode: "NO", currencyCode: "NOK" },
{ countryCode: "NF", currencyCode: "AUD" },
{ countryCode: "NZ", currencyCode: "NZD" },
{ countryCode: "NR", currencyCode: "AUD" },
{ countryCode: "NU", currencyCode: "NZD" },
{ countryCode: "CK", currencyCode: "NZD" },
{ countryCode: "CH", currencyCode: "CHF" },
{ countryCode: "CN", currencyCode: "CNY" },
{ countryCode: "CC", currencyCode: "AUD" },
{ countryCode: "CA", currencyCode: "CAD" },
{ countryCode: "CZ", currencyCode: "CZK" },
{ countryCode: "CX", currencyCode: "AUD" },
{ countryCode: "KI", currencyCode: "AUD" },
{ countryCode: "KR", currencyCode: "KRW" },
{ countryCode: "SG", currencyCode: "SGD" },
{ countryCode: "SE", currencyCode: "SEK" },
{ countryCode: "DK", currencyCode: "DKK" },
{ countryCode: "VG", currencyCode: "USD" },
{ countryCode: "US", currencyCode: "USD" },
{ countryCode: "UM", currencyCode: "USD" },
{ countryCode: "TV", currencyCode: "AUD" },
{ countryCode: "TR", currencyCode: "TRY" },
{ countryCode: "LI", currencyCode: "CHF" },
{ countryCode: "TH", currencyCode: "THB" },
{ countryCode: "TC", currencyCode: "USD" },
{ countryCode: "VI", currencyCode: "USD" },
{ countryCode: "AS", currencyCode: "USD" },
{ countryCode: "AU", currencyCode: "AUD" },
{ countryCode: "IN", currencyCode: "INR" },
{ countryCode: "ID", currencyCode: "IDR" }
]
{
BGN: "https://restcountries.eu/data/bgr.svg",
NOK: "https://restcountries.eu/data/nor.svg",
USD: "https://restcountries.eu/data/asm.svg",
BRL: "https://restcountries.eu/data/bra.svg",
GBP: "https://restcountries.eu/data/imn.svg",
RUB: "https://restcountries.eu/data/rus.svg",
RON: "https://restcountries.eu/data/rou.svg",
NZD: "https://restcountries.eu/data/cok.svg",
JPY: "https://restcountries.eu/data/jpn.svg",
DKK: "https://restcountries.eu/data/dnk.svg",
HRK: "https://restcountries.eu/data/hrv.svg",
HUF: "https://restcountries.eu/data/hun.svg",
HKD: "https://restcountries.eu/data/hkg.svg",
AUD: "https://restcountries.eu/data/aus.svg",
ILS: "https://restcountries.eu/data/isr.svg",
PHP: "https://restcountries.eu/data/phl.svg",
PLN: "https://restcountries.eu/data/pol.svg",
ZAR: "https://restcountries.eu/data/zaf.svg",
MYR: "https://restcountries.eu/data/mys.svg",
MXN: "https://restcountries.eu/data/mex.svg",
CHF: "https://restcountries.eu/data/lie.svg",
CNY: "https://restcountries.eu/data/chn.svg",
CAD: "https://restcountries.eu/data/can.svg",
CZK: "https://restcountries.eu/data/cze.svg",
KRW: "https://restcountries.eu/data/kor.svg",
SGD: "https://restcountries.eu/data/sgp.svg",
SEK: "https://restcountries.eu/data/swe.svg",
TRY: "https://restcountries.eu/data/tur.svg",
THB: "https://restcountries.eu/data/tha.svg",
INR: "https://restcountries.eu/data/ind.svg",
IDR: "https://restcountries.eu/data/idn.svg"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment