aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/app.ts22
-rw-r--r--src/countries.ts52
-rw-r--r--src/util/cache.ts10
-rw-r--r--src/util/logger.ts2
4 files changed, 41 insertions, 45 deletions
diff --git a/src/app.ts b/src/app.ts
index 1994462..24e5b58 100644
--- a/src/app.ts
+++ b/src/app.ts
@@ -2,14 +2,10 @@ import { Countries } from "./countries.ts";
import { Logger } from "./util/logger.ts";
export class App {
- constructor(
- // private cache: Cache,
- private logger: Logger,
- private countries: Countries
- ) {}
+ constructor(private logger: Logger, private countries: Countries) {}
- async run() {
- const command = Deno.args[0];
+ async run(): Promise<void> {
+ const [command, ...args] = Deno.args;
await this.countries.sync();
switch (command) {
@@ -22,22 +18,20 @@ export class App {
case "sync":
await this.countries.sync({
force: true,
- flagAscii: Deno.args[1] === "flags",
+ flagAscii: args[0] === "flags",
});
break;
case "random":
- await this.countries.print(this.countries.random());
+ this.countries.print(this.countries.random());
break;
case "capital":
- const [, ...args] = Deno.args;
- const capital = args.join(" ");
- this.countries.capitalOf(capital);
+ this.countries.capitalOf(args.join(" "));
break;
case "raw":
- this.logger.log(this.countries.find(Deno.args[1]));
+ this.logger.log(this.countries.find(args[0]));
break;
default:
- await this.countries.print(Deno.args.join(" "), true);
+ this.countries.print(Deno.args.join(" "));
break;
}
}
diff --git a/src/countries.ts b/src/countries.ts
index e2230dc..27e765e 100644
--- a/src/countries.ts
+++ b/src/countries.ts
@@ -61,11 +61,7 @@ export class Countries {
return this.list;
}
- getAll() {
- return this.list;
- }
-
- public find(name: string) {
+ public find(name: string): Country {
name = name.toLowerCase();
// Find exact match first
@@ -89,31 +85,11 @@ export class Countries {
return country;
}
- public findByCapital(capital: string) {
- const country = this.list.find((c) => {
- const capitalsLowercase = c.capital.map((capital) =>
- capital.toLowerCase()
- );
- return capitalsLowercase.includes(capital);
- });
-
- if (!country) {
- throw `Could not find the country of capital: ${capital}`;
- }
-
- return country;
- }
-
- capitalOf(capital: string) {
- const country = this.findByCapital(capital);
- this.logger.capitalOf(capital, country.name.common);
- }
-
public filterByRegion(region: Region) {
return this.list.filter((country) => country.region === region);
}
- public async print(name: string, flag?: boolean) {
+ public print(name: string) {
const country = this.find(name);
const currencies = this.extractCurrencies(country.currencies);
const languages = this.extractLanguages(country.languages);
@@ -146,6 +122,30 @@ export class Countries {
return this.names[randomNum];
}
+ public capitalOf(capital: string): void {
+ if (!capital) {
+ this.logger.error("Must provide a capital name.");
+ return;
+ }
+ const country = this.findByCapital(capital);
+ this.logger.capitalOf(capital, country.name.common);
+ }
+
+ private findByCapital(capital: string): Country {
+ const country = this.list.find((c) => {
+ const capitalsLowercase = c.capital.map((capital) =>
+ capital.toLowerCase()
+ );
+ return capitalsLowercase.includes(capital);
+ });
+
+ if (!country) {
+ throw `Could not find the country of capital: ${capital}`;
+ }
+
+ return country;
+ }
+
private shouldSync() {
const lastSynced = this.cache.readTxt("last-synced");
const cacheExists = this.cache.exists("countries", ".json");
diff --git a/src/util/cache.ts b/src/util/cache.ts
index df52f89..75a91b9 100644
--- a/src/util/cache.ts
+++ b/src/util/cache.ts
@@ -4,11 +4,13 @@ import {
existsSync,
} from "https://deno.land/[email protected]/fs/mod.ts";
import { environment } from "../environment/environment.ts";
+import { Country } from "../models/country.model.ts";
+import { FlagAscii } from "../models/flag-ascii.model.ts";
export class Cache {
path = environment.cacheDir;
- public saveJson(name: string, data: {}) {
+ public saveJson(name: string, data: Country[] | FlagAscii[]) {
ensureDirSync(this.path);
Deno.writeTextFileSync(
@@ -22,11 +24,11 @@ export class Cache {
Deno.writeTextFileSync(join(this.path, `${name}.txt`), value);
}
- public readJson(name: string): {} | [] | undefined {
+ public readJson(name: string): any {
let data;
try {
data = Deno.readTextFileSync(join(this.path, `${name}.json`));
- } catch (err) {
+ } catch {
return undefined;
}
return JSON.parse(data);
@@ -35,7 +37,7 @@ export class Cache {
public readTxt(name: string) {
try {
return Deno.readTextFileSync(join(this.path, `${name}.txt`));
- } catch (err) {
+ } catch {
return undefined;
}
}
diff --git a/src/util/logger.ts b/src/util/logger.ts
index 49dcd86..34de473 100644
--- a/src/util/logger.ts
+++ b/src/util/logger.ts
@@ -21,7 +21,7 @@ export class Logger {
public progress(
current: number,
total: number,
- config?: { title: string; description: string }
+ config?: { title?: string; description: string }
) {
console.clear();
if (config?.title) {