aboutsummaryrefslogtreecommitdiff
path: root/countries.util.ts
blob: a565ea903ec1f188c2c9ae49ac4c23bf67f05361 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import * as nano from "https://deno.land/x/[email protected]/mod.ts";
import { environment } from "./environment/environment.ts";
import { Country } from "./models/country.model.ts";

let COUNTRIES: Country[];
let QUERY = environment.queries;

export async function syncCountries(force?: boolean) {
  const lastSynced = localStorage.getItem("lastSynced");
  const savedCountries = localStorage.getItem("countries");
  const week = environment.syncInterval * 24 * 60 * 60 * 1000;
  if (
    !savedCountries ||
    !lastSynced ||
    force ||
    Date.now() - Number(lastSynced) > week
  ) {
    console.log(
      nano.cyan("Syncronizing countries database..."),
      force
        ? ""
        : `\nThis will only happen every ${environment.syncInterval} days`
    );

    const response = await fetch(environment.baseUrl + QUERY);
    const countries = await response.json();
    COUNTRIES = countries;
    localStorage.setItem("countries", JSON.stringify(countries));
    localStorage.setItem("lastSynced", JSON.stringify(Date.now()));
    console.log("Synced successfully");
  } else {
    COUNTRIES = JSON.parse(savedCountries);
  }

  return COUNTRIES;
}

export function printCountry(country: Country) {
  let currencies = [];
  let iteration = 0;
  for (const currencyAbbr in country.currencies) {
    iteration++;
    const currency = country.currencies[currencyAbbr];
    currencies.push(
      `${iteration > 1 ? "\t\t " : ""}${currency.symbol} ${
        currency.name
      } (${currencyAbbr})\n`
    );
  }

  let languages = [];
  for (const langAbbr in country.languages) {
    languages.push(country.languages[langAbbr] + " ");
  }

  console.log(
    nano.cyan("\nCountry:\t"),
    country.flag,
    country.name.common,
    nano.green("\nLanguages:\t"),
    ...languages,
    nano.green("\nCapital:\t"),
    country.capital[0],
    nano.green("\nPopulation:\t"),
    country.population.toLocaleString(),
    nano.green("\nCurrencies:\t"),
    ...currencies
  );
}

export function findCountry(name: string): Country {
  // Replace snake case or kebab case with whitespaces
  name = name.toLowerCase().replace(/-|_/g, " ");

  // Find exact match first, then fall back to fuzzy match
  const country = COUNTRIES.find((c) => {
    const countryName = c.name.common.toLocaleLowerCase();
    return countryName === name || countryName.includes(name);
  });

  if (!country) {
    throw Error(`Cannot find country named ${name}`);
  }

  return country;
}

export function randomCountry() {
  const randomNum = Math.floor(Math.random() * COUNTRIES.length);
  return COUNTRIES[randomNum];
}